我想从包含临床数据的数据集中提取出一组非常大的功能X中最具信息性的20个特性,方法是使用scikit中的RFE函数--在Python中学习。
X是一个68x1140矩阵,其中
我的想法是使用70%的数据集(即从每个记录中提取70% x 1140随机特征),并从整个数据集中提取50个特征。
Y表示从0到2的排序。
换句话说,我的数据如下所示:
我在代码中的实现如下:
## X = features
## Y = labels
p = 0.7
n_perc = round(X.shape[1]*X.shape[0]*p) #70% of the data -> number of elements (height x width x 70%)
rand_idx = np.random.randint(X.shape[1]*X.shape[0], size=n_perc) #random indices (70% of the data)
X_rnd = X.flatten()[rand_idx] #select that 70% in X
Y_rnd = np.repeat(Y,round(X.shape[1]*p)) #we match the dimensions for X_rnd - Y_rnd
selector = RFE(estimator=LogisticRegression(C=1),n_features_to_select=20) #run RFE
selector.fit(X_rnd.reshape,Y_rnd) #select best features
其思想是,我把X中的所有值都压平,从X中只得到70%的随机元素,即X_{rnd} (并相应地调整Y,即Y_{rnd})。
ValueError: Expected 2D array, got 1D array instead:
array=[-0.25367578 0.8069118 -0.63161352 ... 0.5500815 -0.37418711
0.2580666 ]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
但这是我搞错的原因,我不明白。它说,如果我有一个特性或一个样本,我应该重塑数组,但这不是我的情况。
有人知道我该怎么做吗?我应该这样处理这个问题吗?我应该以另一种方式重塑X吗?
谢谢。
发布于 2019-11-22 13:57:00
selector.fit(X_rnd.reshape,Y_rnd) #选择最佳特性
我相信这个错误是在使用X_rnd执行"fit“方法时出现的,但是您的代码显示的是X_rnd.reshape。在这种情况下,错误应该是-
ValueError: Expected 2D array, got scalar array instead:
array=<built-in method reshape of numpy.ndarray object at 0x000000000C28EAD0>.
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
https://datascience.stackexchange.com/questions/63554
复制相似问题