Python提示AttributeError 或者DeprecationWarning: This module was deprecated解決方法

Python提示AttributeError 或者DeprecationWarning: This module was deprecated解決方法

在使用Python的sklearn庫時,發現sklearn的cross_validation不能使用,在pycharm上直接顯示爲被橫線劃掉。
運行程序:python

import sklearn
...
X, Xt, y, yt = sklearn.cross_validation.train_test_split(X, y)

報錯:this

AttributeError: 'module' object has no attribute 'cross_validation'code

這裏嘗試修改成:rem

from sklearn.cross_validation import train_test_split
X, Xt, y, yt = train_test_split(X, y)

發現再也不報錯,而是提示警告信息:pycharm

DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
"This module will be removed in 0.20.", DeprecationWarning)

程序可以正常執行。it

可是注意看提示信息,其實是說cross_validation模塊被棄用了,改成支持model_selection這個模塊,所以,將程序改成:io

from sklearn.model_selection import train_test_split
X, Xt, y, yt = train_test_split(X, y)

再也不出現警告信息,一切正常。function

相關文章
相關標籤/搜索