[英]Iris decision tree in python
我正在嘗試了解決策樹,最終找到了一篇有關決策樹的文章。 這篇文章的目的是確定一朵花是否是鳶尾花,但我似乎遇到了一些錯誤,我希望有人能回答我得到兩個錯誤,如下所示:
iris: Bunch iris: inner_f 'tuple' 的實例沒有'target' 成員
和
iris: Bunch iris: inner_f 'tuple' 的實例沒有'data' 成員
我在 x = iris.data 行和 y = iris.target 行得到這些錯誤。
這是代碼:
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
#load iris data
iris = datasets.load_iris()
x = iris.data
y = iris.target
d = [{"sepal_length":row[0],
"sepal_width":row[1],
"petal_length":row[2],
"petal_width":row[3]} for row in x]
df = pd.DataFrame(d) # construct dataframe
df["types"] = y # assign types
df = df.sample(frac=1.0) # random shuffle rows
df.head()
有沒有人知道我為什么會收到這些錯誤?
您的錯誤消息表明有問題的值iris
是一個元組,它沒有您引用的屬性。 檢查您正在使用的工具的文檔; 他們應該解釋如何將datasets.load_iris()
解壓縮到您需要的對象中。
在大多數情況下,我不會過濾警告,因為您可以從警告中獲得有用的信息。
因此,sklearn 數據集格式是一個 Bunch,它是一個像字典一樣工作的專用容器 object。 您可以使用點表示法訪問它,例如 iris.data 或字典表示法,例如 iris['data']。 在這里,不清楚您的機器上的錯誤是什么,因為我(像其他評論者一樣)在 python 3.8.5 中訪問 iris.data 或 iris['data'] 沒有問題。
我想讓你知道幾個地方來改進你的方法:
(1) It is unclear why you need to construct a dataframe as you can get the samples you need directly from calling train_test_split on the concatenated numpy arrays or you can get a random sample of indices from the numpy arrays directly.
(2) 您構建 dataframe 的方法比需要的復雜。
import pandas as pd
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
# load iris data
iris = datasets.load_iris()
# train test split
X_train, y_train, X_test, y_test = train_test_split(iris.data, iris.target)
# random shuffle of data/target indices
rng = np.random.default_rng()
rng_size = iris.data.shape[0]
idx_sample = rng.choice(np.arange(rng_size), size=rng_size, replace=False)
# simpler way to create dataframe
# concatenate along the columns (axis 1)
# then set the column names in one place
df = pd.concat([pd.DataFrame(iris.data), pd.DataFrame(iris.target)], axis=1)
df.columns = ["sepal_length", "sepal_width", "petal_length", "petal_width", "types"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.