目录
报错1
原本代码
修改后代码
key
报错2
解决方法2.1
解决方法2.2
报错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,文章来源:https://www.toymoban.com/news/detail-755821.html
iris = datasets.load_iris()
文章来源地址https://www.toymoban.com/news/detail-755821.html
到了这里,关于解决‘function‘ object has no attribute ‘data‘的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!