【问题标题】:I am trying to run the following in Jupyter but it is showing shape error我正在尝试在 Jupyter 中运行以下命令,但它显示形状错误
【发布时间】:2020-07-17 10:20:23
【问题描述】:
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)

【问题讨论】:

    标签: python r jupyter


    【解决方案1】:

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

    X_train, X_test, y_train, y_test
    

    不是

    X_train, y_train, X_test, y_test
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 2019-10-12
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多