alueError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_3744\1002171229.py in <module> ----> 1 model.fit(x_train,y_train) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\_logistic.py in fit(self, X, y, sample_weight) 1506 _dtype = [np.float64, np.float32] 1507 -> 1508 X, y = self._validate_data( 1509 X, 1510 y, C:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py in _validate_data(self, X, y, reset, validate_separately, **check_params) 579 y = check_array(y, **check_y_params) 580 else: --> 581 X, y = check_X_y(X, y, **check_params) 582 out = X, y 583 C:\ProgramData\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, estimator) 977 ) 978 --> 979 y = _check_y(y, multi_output=multi_output, y_numeric=y_numeric) 980 981 check_consistent_length(X, y) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py in _check_y(y, multi_output, y_numeric) 991 ) 992 else: --> 993 y = column_or_1d(y, warn=True) 994 _assert_all_finite(y) 995 _ensure_no_complex_data(y) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py in column_or_1d(y, warn) 1036 return np.ravel(y) 1037 -> 1038 raise ValueError( 1039 "y should be a 1d array, got an array of shape {} instead.".format(shape) 1040 ) ValueError: y should be a 1d array, got an array of shape (50, 2) instead. ```python ac=accuracy_score(y_test,model.predict(x_test)) ``` --------------------------------------------------------------------------- NotFittedError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_3744\4106996100.py in <module> ----> 1 ac=accuracy_score(y_test,model.predict(x_test)) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\_base.py in predict(self, X) 423 Vector containing the class labels for each sample. 424 """ --> 425 scores = self.decision_function(X) 426 if len(scores.shape) == 1: 427 indices = (scores > 0).astype(int) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\_base.py in decision_function(self, X) 403 this class would be predicted. 404 """ --> 405 check_is_fitted(self) 406 407 X = self._validate_data(X, accept_sparse="csr", reset=False) C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_is_fitted(estimator, attributes, msg, all_or_any) 1220 1221 if not fitted: -> 1222 raise NotFittedError(msg % {"name": type(estimator).__name__}) 1223 1224 NotFittedError: This LogisticRegression instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
解决办法:
train_test_split
时没有正确分割数据,导致 y_train
和 y_test
的形状不正确。train_test_split
应该返回一个包含四个元素的元组,分别是训练数据、训练标签、测试数据和测试标签。你需要正确地解包这个元组。
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=50)
model.fit(x_train, y_train)
ac = accuracy_score(y_test, model.predict(x_test))
print(ac)
问题解决