GauravBh1010tt / DeepLearn

Implementation of research papers on Deep Learning+ NLP+ CV in Python using Keras, Tensorflow and Scikit Learn.
MIT License
1.82k stars 353 forks source link

StandardScaler.transform is throwing error "ValueError: Expected 2D array, got 1D array instead:" #25

Open VishnuPy opened 5 years ago

VishnuPy commented 5 years ago

I just started learning DataScience and ML. Trying out code available online.

I have got PandaDataframes (Xtrain, Xtest) and Panda Series (ytrain and ytest) as output from Train_Test_Split function.

When Xtrain and Xtest are put thru normalization (SC.fit_transform and SC.tranform functions) both were successful.

But, i am not able to get past ytrain and ytest normalization. It gives below error

ValueError Traceback (most recent call last)

in () 1 #ytrain=StandardScaler.transform(ytrain[:,-1]) 2 ----> 3 ytrain=sc.transform(ytrain) C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\sklearn\preprocessing\data.py in transform(self, X, y, copy) 679 copy = copy if copy is not None else self.copy 680 X = check_array(X, accept_sparse='csr', copy=copy, warn_on_dtype=True, --> 681 estimator=self, dtype=FLOAT_DTYPES) 682 683 if sparse.issparse(X): C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator) 439 "Reshape your data either using array.reshape(-1, 1) if " 440 "your data has a single feature or array.reshape(1, -1) " --> 441 "if it contains a single sample.".format(array)) 442 array = np.atleast_2d(array) 443 # To ensure that array flags are maintained ValueError: Expected 2D array, got 1D array instead: array=[0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 1. 0. 0. 1. 1. 0. 1. 1. 1. 1. 1. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 1. 1. 1. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 1. 0. 1. 1. 1. 0. 0. 1. 1.]. 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.
GauravBh1010tt commented 5 years ago

You need to get the dependencies right. It may be possible that you are using a newer version of the libraries to those that I have used.

By the way, looking at the error you might need to reshape the data to a 2D array before passing to the standard scalar. Try printing shape of the data array you need to get something like this - (1,20) instead of (20,) or (,20).

Hope it helps.

VishnuPy commented 5 years ago

hi Gaurav,

Thanks a ton for responding to my trivial question.

I realized that i was trying to run TRANSFORM on test dataset. this should not be done. Reason being that Test Dataset and Train Dataset incidentally have differences. Thanks for Responding once again.

Regards, Vishnu

https://www.avast.com/en-in/recommend?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=default3&tag=07719cfe-3295-44be-8980-e6d826bca8f3 I’m protected online with Avast Free Antivirus. Get it here — it’s free forever. https://www.avast.com/en-in/recommend?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=default3&tag=07719cfe-3295-44be-8980-e6d826bca8f3 <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Tue, Apr 9, 2019 at 11:15 AM Gaurav Bhatt notifications@github.com wrote:

You need to get the dependencies right. It may be possible that you are using a newer version of the libraries to those that I have used.

By the way, looking at the error you might need to reshape the data to a 2D array before passing to the standard scalar. Try printing shape of the data array you need to get something like this - (1,20) instead of (20,) or (,20).

Hope it helps.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/GauravBh1010tt/DeepLearn/issues/25#issuecomment-481110603, or mute the thread https://github.com/notifications/unsubscribe-auth/Auruq_XKm90m6Vs9_LdkYUE2iSTJ2WoGks5vfCjpgaJpZM4ceo96 .

krtarunsingh commented 1 year ago

Use

X_train= X_train.values.reshape(-1, 1) X_test = X_test.values.reshape(-1, 1)