I would like to extract the 20 most informative features of a very large set of features $X$ coming from a dataset containing clinical data by using the RFE function from scikit-learn in Python.

$X$ is a 68 x 1140 matrix where

  • Each row represents a recorded session.
  • For each subject, there are 4 recorded sessions.
  • Then, there are 17 subjects in the dataset.

My idea is to use 70% of the dataset (i.e. 70% x 1140 random features from each recording) and extract 50 features out of the whole dataset.

$Y$ represents a ranking from 0 to 2.

In other words, my data looks like this:

enter image description here

And my implementation in the code is the following:

## 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

The idea is that I flatten all the values from X and I get only 70% random elements from $X$, i.e $X_{rnd}$ (and also adapt $Y$ accordingly, i.e. $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.

But some reason I'm getting this error, which I don't understand. It says that I should reshape the array if I have either one feature or one sample, but it's not my case.

Does anybody know what I should do? Is this how I should approach the problem? Should I reshape $X$ in another manner?


  • $\begingroup$ Hi, welcome to Data Science SE! You put a lot of effort in expressing your question and that is appreciated. However, what you are trying to achieve is still unclear to me! Apparently you are sampling 70% of the input matrix but without keeping any shape, how is that making sense? (I would have expected keeping 70% of rows, or columns) Also, why do you need to work on 70% of the dataset? What do you mean by "most informative features"? $\endgroup$ Nov 22, 2019 at 22:55
  • $\begingroup$ @RomainReboulleau Hi! The idea is that the RFE algorithm gets the best features (or the most informative features, if you will) and I am flattening $X_{rnd}$ because if I pass it without flattening, then it considers each column as a feature vector, which is not what I am interested in. I am not sure if I am making myself clear... $\endgroup$
    – Arnau
    Nov 23, 2019 at 11:33
  • $\begingroup$ Not quite sure :) I still can't get why you need the 70% split. Also, are we really talking about a dataset with 68 observations of 1140 features, or something else? $\endgroup$ Nov 23, 2019 at 13:00
  • $\begingroup$ @RomainReboulleau The fact that I am taking 70% of the data is superfluous; it is not really the problem right now :) And yes, we are talking about 68 observations of 1140 features, yes. $\endgroup$
    – Arnau
    Nov 23, 2019 at 19:22
  • $\begingroup$ Then you can feed the selector with initial X and Y without any reshape action and it should work. $\endgroup$ Nov 23, 2019 at 22:25

1 Answer 1


selector.fit(X_rnd.reshape,Y_rnd) #select best features

X_rnd should be a 2D matrix.

I believe this error came when "fit" method was executed with X_rnd but your code is showing X_rnd.reshape. In this case, error should be -

    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.
  • $\begingroup$ Hi! Thanks for replying to my question. The problem is that if I pass $X_{rnd}$ as a set of rows and columns, then it considers each column in the matrix as feature vector. But I would the algorithm to consider each element in $X_{rnd}$ as a feature. $\endgroup$
    – Arnau
    Nov 23, 2019 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.