[英]Expected 2D array, got 1D array instead
I am getting this error even though after much debugging, the array seems to be a 2D array: 即使经过大量调试,我仍收到此错误,该数组似乎是2D数组:
reg = linear_model.SGDClassifier()
X = []
Y = []
with open('data/legitimate.txt', 'r', encoding='utf8') as f:
for line in f:
X.append(get_heuristics(line))
Y.append(0)
with open('data/bad.txt', 'r', encoding='utf8') as f:
for line in f:
X.append(get_heuristics(line))
Y.append(1)
X = np.array(X)
Y = np.array(Y)
reg.fit(X, Y)
Where get_heuristics()
returns a 1D array. 其中get_heuristics()
返回一维数组。 Any reason why this could be happening? 为什么会发生这种情况? Thanks 谢谢
The X
array is a 2D array. X
数组是2D数组。 As you correctly say getheuristics()
returns a 1D array and appending a 1D array will make a 2D array. 正确地说, getheuristics()
返回一个1D数组,追加一个1D数组将构成一个2D数组。 However the error "Expected 2D array, got 1D array instead" will be displayed because Y
is only 1D. 但是,将显示错误“预期的2D数组,取而代之的是1D数组”,因为Y
仅为1D。 After all, you are simply appending a single digit each time, hence a 1D array. 毕竟,您每次只是简单地附加一个数字,因此是一维数组。 Scikit-learn's fit
requires two 2D arrays. Scikit-learn的fit
需要两个2D数组。 As I'm sure the console will have suggested, you might want to try: 正如我确定控制台会建议的那样,您可能需要尝试:
Y = Y.reshape(-1, 1) Y = Y.reshape(-1,1)
before you call reg.fit(X,Y)
. 在致电reg.fit(X,Y)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.