# ValueError: Expected 2D array, got 1D array instead

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: And my implementation in the code is the following:

## X = features
## Y = labels
p = 0.7
n_perc = round(X.shape*X.shape*p) #70% of the data -> number of elements (height x width x 70%)
rand_idx = np.random.randint(X.shape*X.shape, 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*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?

Thanks.

• 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"? – Romain Reboulleau Nov 22 '19 at 22:55
• @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... – Arnau Nov 23 '19 at 11:33
• 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? – Romain Reboulleau Nov 23 '19 at 13:00
• @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. – Arnau Nov 23 '19 at 19:22
• Then you can feed the selector with initial X and Y without any reshape action and it should work. – Romain Reboulleau Nov 23 '19 at 22:25

    ValueError: Expected 2D array, got scalar array instead:

• 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. – Arnau Nov 23 '19 at 11:36