我是机器学习新手,没有太多Python经验。
在下面的代码中,我们调用 fit()
后训练的数据存储在哪里?调用 predict()
后如何知道数据是否经过训练方法。
我知道这可能是一个愚蠢的问题,但我们将非常感谢您提供一些帮助。谢谢
#Fitting Simple linear regression to the training set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
#Predicting the Test search results
y_pred = regressor.predict(X_test)
最佳答案
fit()
函数只是将线性模型拟合到您的数据。训练数据已分配给变量 X_train
(特征)和 y_train
(标签),因此调用 fit()
后不会存储任何训练数据>。但是,您可以保存经过训练/拟合的模型,其中包括使用训练数据近似的参数。
现在回到你的第二个问题,在将模型拟合到数据之前不可能进行预测。如果您尝试在调用 fit()
之前调用 predict()
,您将得到 NotFittedError
:
Exception class to raise if estimator is used before fitting.
This class inherits from both ValueError and AttributeError to help with exception handling and backward compatibility.
Example:
>>> from sklearn.svm import LinearSVC >>> from sklearn.exceptions import NotFittedError >>> try: ... LinearSVC().predict([[1, 2], [2, 3], [3, 4]]) ... except NotFittedError as e: ... print(repr(e)) NotFittedError("This LinearSVC instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator."...)
关于python - 在 ml 中调用 fit() 后,训练后的数据存储在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59766182/