Python Forum
valueError: expected 2d, got 1d instead - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: valueError: expected 2d, got 1d instead (/thread-20527.html)



valueError: expected 2d, got 1d instead - brkolvr - Aug-16-2019

Googling this problem it only seems to come up when using a classifier, though I'm trying to normalise a column, and it seems to need a 2d array, but I'm not sure how I would make a 2d array out of my single column:

wgt_l1 = normalize(data.wgt, norm='l1')
wgt_l2= normalize(data.wgt, norm='l2')
Considering my weighting ranges from over 400,000 to below 20,000 means I should probably normalise it, though I can't seem to get the method to run properly. Not as much the actual code, but can I ask why it is necessary to need a 2d array to normalise?

Error msg:
Please find a small sample of numbers (can change to whatever typing necessary, currently in float as the error message mentioned float but I'd rather it be int):
I would try to do something like:
data.wgt.reshape(-1,1)
But then I get the error:
Error:
AttributeError: 'Series' object has no attribute 'reshape'

data['wgt'] = MinMaxScaler().fit_transform(data['wgt'].values.reshape(-1,1))
Got it with this, though I still don't understand why it must be 2d?


RE: valueError: expected 2d, got 1d instead - boring_accountant - Aug-16-2019

As per the error message, data.wgt is a pandas.Series object. You can get the data in a numpy array, which has the 'reshape' method, by accessing the 'values' attribute.
data.wgt.values.reshape(-1, 1)
A quick look at the Pandas docs point towards using 'to_numpy()' instead
data.wgt.to_numpy().reshape(-1, 1)