Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

python - ImportError: cannot import name cross_validation

I cannot import the cross_validation from sklearn library; I use sklearn version 0.20.0

from sklearn import cross_validation

later in the code:

features_train, features_test, labels_train, labels_test = cross_validation.train_test_split(word_data, authors, test_size=0.1, random_state=42)

Error:

Traceback (most recent call last):
  File "D:meM.ScUdacity_ML_courseud120-projects-  master
aive_bayes
b_author_id.py", line 16, in <module>
    from email_preprocess import preprocess
  File "../tools/email_preprocess.py", line 8, in <module>
    from sklearn import cross_validation
ImportError: cannot import name cross_validation
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This happens because there is no cross_validation object in sklearn. You're likely looking for something more like the cross_validate function. You can access that through

from sklearn.model_selection import cross_validate

However, you don't need to import any cross-validation software to perform the train-test split, since that will just randomly sample from the data. Try

from sklearn.model_selection import train_test_split

followed by

features_train, features_test, labels_train, labels_test = train_test_split(word_data, authors, test_size=0.1, random_state=42)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...