【问题标题】:This SVC instance is not fitted yet. Call 'fit' with appropriate arguments before using this method此 SVC 实例尚未安装。在使用此方法之前使用适当的参数调用“fit”
【发布时间】:2019-10-05 18:15:51
【问题描述】:

我使用了sklearn的SVC来拟合训练集,并尝试通过classifier.predict(X_test)预测y_pred,但是返回NotFittedError: This SVC instance is not fit ye​​t。在使用此方法之前,使用适当的参数调用“fit”。

我尝试重新启动 python,它没有工作。我还尝试了来自 sklearn.linear_model 的 LogisticRegression,它工作得很好。

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('Social_Network_Ads.csv')
X = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4].values

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

from sklearn.svm import SVC
classifier = SVC(kernel = 'linear', random_state = 0)
classifier.fit = (X_train, y_train)

y_pred = classifier.predict(X_test)

我希望 y_pred 包含 X_test 的预测值。

相反,我收到以下错误消息, NotFittedError:此 SVC 实例尚未安装。在使用此方法之前,使用适当的参数调用“fit”。

【问题讨论】:

  • 我想你只是打错了:classifier.fit(X_train, y_train) 而不是classifier.fit = (X_train, y_train)(没有等号)

标签: python-3.x scikit-learn svm


【解决方案1】:

最后第二行是:

classifier.fit(X_train, y_train) 

而不是

classifier.fit = (X_train, y_train)

【讨论】:

    猜你喜欢
    • 2020-04-14
    • 2018-12-26
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    相关资源
    最近更新 更多