Python 3.x 训练预测模型时输入形状不正确

Python 3.x 训练预测模型时输入形状不正确,python-3.x,numpy,valueerror,Python 3.x,Numpy,Valueerror,我使用以下代码使用sklearn训练货币汇率预测模型,但出现错误: import numpy as np x = [[30],[40],[50],[60],[70],[80],[90],[100],[120],[130],[140],[150]] y = ['jan','febuary,'march','april','may','june','july','august','september','october','november','december'] y_2 = np.resha

我使用以下代码使用sklearn训练货币汇率预测模型,但出现错误:

import numpy as np
x = [[30],[40],[50],[60],[70],[80],[90],[100],[120],[130],[140],[150]] 
y = ['jan','febuary,'march','april','may','june','july','august','september','october','november','december']

y_2 = np.reshape(y, (-1, 2))
#reshaping because it throws in an error to reshape

from sklearn import preprocessing
le = preprocessing.LabelEncoder()
y_3 = le.fit_transform(y_2)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-144-c98a5b8bd15a> in <module>
----> 1 y_3 = le.fit_transform(y_2)

c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\sklearn\preprocessing\label.py in fit_transform(self, y)
233         y : array-like of shape [n_samples]
234         """
--> 235         y = column_or_1d(y, warn=True)
236         self.classes_, y = _encode(y, encode=True)
237         return y

c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\sklearn\utils\validation.py in column_or_1d(y, warn)
795         return np.ravel(y)
796 
--> 797     raise ValueError("bad input shape {0}".format(shape))
798 
799 

ValueError: bad input shape (6, 2)
将numpy导入为np
x=[[30]、[40]、[50]、[60]、[70]、[80]、[90]、[100]、[120]、[130]、[140]、[150]]
y=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’、‘九月’、‘十月’、‘十一月’、‘十二月’]
y_2=np.重塑(y,(-1,2))
#重塑,因为重塑时会出现错误
从sk学习导入预处理
le=预处理。LabelEncoder()
y_3=le.fit_变换(y_2)
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在里面
---->1 y_3=le.fit_变换(y_2)
c:\users\user\appdata\local\programs\python37-32\lib\site packages\sklearn\preprocessing\label.py in fit\u transform(self,y)
233 y:形状的阵列状[n_样本]
234         """
-->235 y=列_或_1d(y,警告=真)
236 self.classes,y=\u encode(y,encode=True)
237返回y
c:\users\user\appdata\local\programs\python\python37-32\lib\site packages\sklearn\utils\validation.py在列\u或\u 1d中(y,警告)
795返回np.ravel(y)
796
-->797提升值错误(“错误的输入形状{0}”。格式(形状))
798
799
ValueError:输入形状不正确(6,2)

要修复此错误,我需要做什么?

无需对您正在执行的
y
执行
重塑()

将numpy导入为np
x=[[30]、[40]、[50]、[60]、[70]、[80]、[90]、[100]、[120]、[130]、[140]、[150]]
y=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’、‘九月’、‘十月’、‘十一月’、‘十二月’]
#y_2=np。重塑(y,(-1,2))-->这是不需要的
#重塑,因为重塑时会出现错误
从sk学习导入预处理
le=预处理。LabelEncoder()
y2=线性拟合变换(y)
打印(“LabelEncoder classes=,le.classes_u3;)
#LabelEncoder类=[“四月”“八月”“十二月”“二月”“一月”“七月”“六月”“三月”“五月”“十一月”“十月”“九月”]

如果我不重塑,它会引发以下错误:ValueError:需要2D数组,改为1D数组:array=[4 3 7 0 8 6 5 1 11 10 9 2]。使用array重塑数据。如果数据有单个要素或数组,则重塑(-1,1)。重塑(1,-1)如果它只包含一个样本。我已经分享了上面的代码,这很好。你能分享你的完整代码吗?