📌  相关文章
📜  从 sklearn.cross_validation 导入 train_test_split 错误 (1)

📅  最后修改于: 2023-12-03 15:21:53.863000             🧑  作者: Mango

ImportError: No module named 'sklearn.cross_validation'

当出现这个错误时,提示我们无法导入train_test_split函数。这是因为cross_validation在较新版本的scikit-learn库中已经被弃用,被model_selection代替。

为了解决这个问题,我们需要将导入方式更改为如下所示:

from sklearn.model_selection import train_test_split

代码片段如下:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

data = pd.read_csv("data.csv")
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
regressor = LinearRegression()
regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)

这样一来,我们就可以愉快地使用train_test_split函数来划分数据集了。