決策樹例項及原理

2021-09-25 11:49:05 字數 1288 閱讀 1555

鐵達尼號**生死

import pandas as pd

from sklearn.tree import decisiontreeclassifier #決策樹分類器。

from sklearn.feature_extraction import dictvectorizer # 將特徵值對映列表轉換為向量

from sklearn.model_selection import train_test_split # 將資料集拆分成訓練集和測試集

def decision():

"""決策樹對鐵達尼號進行**生死

:return:

"""# 讀取資料

titan = pd.read_excel(r"")

# 處理資料,找到特徵值和目標值(目標值為是否存活,特徵值為對是否存活有關聯的值)

x = titan[["pclass", "age", "***"]]

y = titan["survived"]

# 處理缺失值

x["age"].fillna(x["age"].mean(), inplace=true)

# 分割資料集到訓練集和測試集 train_test_split()

x_train, x_test, y_train, y_test = train_test_split(

x, y, test_size = 0.25)

# print(x_train.to_dict(orient = "records"))

# 進行處理 (特徵工程)特徵-》類別-》one_hot編碼

dict = dictvectorizer(sparse=false)

x_train = dict.fit_transform(x_train.to_dict(orient = "records"))

print(dict.get_feature_names())

# print(x_train)

x_test = dict.transform(x_test.to_dict(orient = "records"))

# 用決策樹進行**

dec = decisiontreeclassifier(criterion="gini", max_depth = 5)

dec.fit(x_train , y_train)

# **準確率

print(dec.score(x_test, y_test))

if __name__ == "__main__":

decision()

決策樹原理

目錄 決策樹基本知識 id3 與c4.5 cart 應用 1.我們遇到的選擇都是根據以往的選擇判斷,如果把判斷背後的邏輯整理成乙個結構圖,會是乙個樹狀圖,就是決策樹的本質.2.決策樹的階段 構造與剪枝 構造 選擇什麼屬性作為結點 1 根節點 樹的頂端 2 內部節點 中間節點 3 葉節點 決策節點 剪...

決策樹01 決策樹的原理

此筆記根據 machine learning in action 和周志華教授的 機器學習 所作。缺點 可能會產生過度匹配問題。適用資料型別 數值型和標稱型 machine learning in action if so return 類標籤 else 尋找劃分資料集的最好特徵 劃分資料集 建立分...

Spark MLlib例項 決策樹

spark mllib例項 決策樹 通俗來說,決策樹分類的思想類似於找物件。現想象乙個女孩的母親要給這個女孩介紹男朋友,於是有了下面的對話 女兒 多大年紀了?母親 26。女兒 長的帥不帥?母親 挺帥的。女兒 收入高不?母親 不算很高,中等情況。女兒 是公務員不?母親 是,在稅務局上班呢。女兒 那好,...