报错1
AttributeError: 'function' object has no attribute 'data'
function对象没有data属性
原本代码
from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
cancer = load_breast_cancer
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, stratify=cancer.target, random_state = 42)
tree = DecisionTreeClassifier(random_state=0)
tree.fit(X_train, y_train)
print("Accuracy on traning set:{:.3f}".format(tree.score(X_train, y_train)))
print("Accuracy on test set:{:.3f}".format(tree.score(X_test, y_test)))
print("tree max depth:{}".format(tree. tree_max_depth))
修改后代码
from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import datasets
import matplotlib.pyplot as plt
import numpy as np
cancer = datasets.load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, stratify=cancer.target, random_state = 42)
tree = DecisionTreeClassifier(random_state=0)
tree.fit(X_train, y_train)
print("Accuracy on traning set:{:.3f}".format(tree.score(X_train, y_train)))
print("Accuracy on test set:{:.3f}".format(tree.score(X_test, y_test)))
print("tree max depth:{}".format(tree. tree_.max_depth))
key
from sklearn import datasets
cancer = datasets.load_breast_cancer()
报错2
from sklearn import datasets
iris=datasets.load_iris
X = iris.data
AttributeError: 'function' object has no attribute 'data
I load iris dataset from sklearn ,but when i write "X = iris.data",an error occur as "AttributeError: 'function' object has no attribute 'data'"
解决方法2.1
load_iris
is a function and needs to be called as such:
from sklearn import datasets
iris=datasets.load_iris()
X = iris.data
解决方法2.2
Use
load_iris()
instead of
load_iris
, like this,
iris = datasets.load_iris()
版权归原作者 alwaysuzybaiyy 所有, 如有侵权,请联系我们删除。