Python Forum
valueError: expected 2d, got 1d instead
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
valueError: expected 2d, got 1d instead
#1
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?
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: Buffer dtype mismatch, expected 'Python object' but got 'long long' MaJeFi 2 12,402 Mar-20-2019, 06:00 AM
Last Post: MaJeFi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020