Am,试图使用scikit-学习在我的培训集中训练一个模型,但是得到了这个错误:
ValueError: Expected 2D array, got 1D array instead: array=[90. 4.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.步骤1:将x和y分割成训练和测试集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.4, random_state = 4)检查新分割的x值的形状(培训和测试)
X_train = X_train.shape
X_test = X_test.shape
print(X_train)
print(X_test)检查新分割y值的形状(培训和测试)
y_train = y_train.shape
y_test = y_test.shape
print(y_train)
print(y_test)第二步:在培训集上培训我们的模型(使用物流回归)
logR = LogisticRegression()
logR = logR.fit(X_train, y_train)运行这段代码我得到了错误
发布于 2020-01-31 14:54:58
似乎您正在用数据点的形状替换数据点:
X_train = X_train.shape
X_test = X_test.shape
y_train = y_train.shape
y_test = y_test.shape删除这些行并重新运行。
发布于 2020-01-31 15:04:06
你做了很棒的工作,但是你做了一件错误的事情:你用它们的形状来替换训练和测试数据,这就是你面临这个错误的原因
#replace these line
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.4, random_state = 4)
print( X_train.shape)
print( X_test.shape)
print(y_train.shape)
print(y_test.shape)
logR = LogisticRegression()
logR = logR.fit(X_train, y_train)
# Now it work finehttps://stackoverflow.com/questions/60006087
复制相似问题