我正在尝试在Jupyter中运行以下命令,但显示形状错误

时间:2020-07-17 10:20:23

标签: python r jupyter

from sklearn.model_selection import train_test_split
X_train,y_train,X_test,y_test = train_test_split(X,y,test_size=0.2,random_state=0)
regressor= LogisticRegression(random_state=0)
regressor.fit(X_train,y_train)

我明白了

ValueError
Traceback (most recent call last)
<ipython-input-105-ff7b581df55c> in <module>
----> 1 regressor.fit(X_train,y_train)

~\anaconda3\lib\site-packages\sklearn\linear_model\_logistic.py in fit(self, X, y, sample_weight)
   1525 
   1526         X, y = check_X_y(X, y, accept_sparse='csr', dtype=_dtype, order="C",
-> 1527                          accept_large_sparse=solver != 'liblinear')
   1528         check_classification_targets(y)
   1529         self.classes_ = np.unique(y)

~\anaconda3\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    758                         dtype=None)
    759     else:
--> 760         y = column_or_1d(y, warn=True)
    761         _assert_all_finite(y)
    762     if y_numeric and y.dtype.kind == 'O':

~\anaconda3\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 (328, 25)

1 个答案:

答案 0 :(得分:0)

您的train_test_split()分配顺序错误,是:

X_train, X_test, y_train, y_test

不是

X_train, y_train, X_test, y_test