gpt4 book ai didi

python - “函数”对象没有属性 'train_test_split'

转载 作者:行者123 更新时间:2023-12-02 01:38:53 25 4
gpt4 key购买 nike

我刚开始使用 Python 实现机器学习,目前正在按照 YouTube 教程尝试 KNN 分类。这是代码。

import numpy as np
#from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_validate
import pandas as pd

df=pd.read_csv('breast-cancer-wisconsin.data.txt')
df.replace('?', -99999, inplace=True)
df.drop(['id'],1,inplace=True)

X=np.array(df.drop(['class'],1))
y=np.array(df['class'])

X_train, X_test, y_train, y_test=cross_validate.train_test_split(X,y,test_size=0.2)

我收到以下错误:-

X_train, X_test, y_train, y_test=cross_validate.train_test_split(X,y,test_size=0.2)
AttributeError: 'function' object has no attribute 'train_test_split'

我尝试将 train_test_split 导入为

 from sklearn.model_selection import train_test_split

但后来我得到了同样的错误。任何帮助表示赞赏。谢谢!

最佳答案

train_test_split 是一个单独的模块 ( docs ),不能与 cross_validate 结合使用;这里正确的用法是(假设 scikit-learn v0.20):

from sklearn.model_selection import train_test_split
# [...]
X_train, X_test, y_train, y_test=train_test_split(X,y,test_size=0.2)

关于python - “函数”对象没有属性 'train_test_split',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55303202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com