[英]np.reshape returns the Data must be one dimensional even though the data is one dimensional
I am trying to reshape my y_train value so that I can put it in the StandardScaler and then use it to invert the transformed valued that I get from predicted the x_values.我正在尝试重塑我的 y_train 值,以便我可以将其放入 StandardScaler,然后使用它来反转我从预测的 x_values 中获得的转换值。
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
dataset = pd.read_csv('/home/ritvik/Desktop/dataset_for_hackathon/wheat-2014-supervised.csv')
dataset = dataset.drop(['CountyName','State','Date'],axis = 1)
df_x = dataset.iloc[:,0:]
df_x = df_x.fillna(df_x.mean())
df_y = dataset.iloc[:,22]
x_train,x_test,y_train,y_test = train_test_split(df_x,df_y,test_size = 0.2)
scaler_x = StandardScaler()
scaler_y = StandardScaler()
scaler_x.fit(x_train)
x_train = scaler_x.transform(x_train)
#this returns the value (146039,) clearly a one dimensional array
print(y_train.shape)
y_train = np.reshape(y_train,(-1,1)) # <-Throws an error saying that the data should be one dimensional
y_train = scaler_y.fit(y_train)
regressor = LinearRegression()
regressor.fit(x_train,y_train)
predicted = regressor.predict(scaler_x.transform(x_test))
print(pd.DataFrame(scaler_y.inverse_transform(predicted),y_test))
This is the error stack这是错误堆栈
File "<ipython-input-53-f4ca71947844>", line 1, in <module>
y_train = np.reshape(y_train,(-1,1))
File "<__array_function__ internals>", line 6, in reshape
File "/home/ritvik/anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 301, in reshape
return _wrapfunc(a, 'reshape', newshape, order=order)
File "/home/ritvik/anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 58, in _wrapfunc
return _wrapit(obj, method, *args, **kwds)
File "/home/ritvik/anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py", line 51, in _wrapit
result = wrap(result)
File "/home/ritvik/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py", line 1999, in __array_wrap__
return self._constructor(result, **d).__finalize__(self)
File "/home/ritvik/anaconda3/lib/python3.7/site-packages/pandas/core/series.py", line 311, in __init__
data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
File "/home/ritvik/anaconda3/lib/python3.7/site-packages/pandas/core/internals/construction.py", line 729, in sanitize_array
raise Exception("Data must be 1-dimensional")
Exception: Data must be 1-dimensional
I have no idea where I am going wrong.我不知道我哪里出错了。
Substitute the line:替换行:
y_train = np.reshape(y_train,(-1,1))
to:到:
y_train = np.reshape(y_train.values,(-1,1))
and you will get rid off your error message.你会摆脱你的错误信息。
One more detail.还有一个细节。 Change:
改变:
y_train = scaler_y.fit(y_train)
to到
y_train = scaler_y.fit_transform(y_train)
and your code will become fully runnable.并且您的代码将变得完全可运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.