coding=utf-8
“”"
author:lei
function: 决策树
“”"
from sklearn.tree import DecisionTreeClassifier
from sklearn.feature_extraction import DictVectorizer
from sklearn.model_selection import train_test_split
from sklearn.tree import export_graphviz
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
import pandas as pd
train = pd.read_csv("./data/train.csv")
test = pd.read_csv("./data/test.csv")
list(train).extend(list(test))
print(train)
def decision_tree():
“”"
决策树对泰坦尼克号进行预测生死
:return:
“”"
# 获取数据
titan = pd.read_csv("")
# 处理数据
x = train[["pclass", "age", "sex"]]
y = titan["survived"]
# 缺失值处理
x["age"].fillna(x["age"].mean(), inplace=True) # 填补缺失值
# 分割数据集到 训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)
# 进行处理(特征工程) 特征--》类别 --》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)
print(x_test)
# 使用决策树进行预测
dec = DecisionTreeClassifier(max_depth=5)
dec.fit(x_train, y_train)
print("预测的准确率:", dec.score(x_test, y_test))
# 导出决策树的结构
export_graphviz(dec, out_file="./tree.dot", feature_names=["年龄", "age", ""])
def random_forest():
ir = load_iris()
x_train, x_test, y_train, y_test = train_test_split(ir.data, ir.target, test_size=0.25)
rf = RandomForestClassifier()
# 网格搜索和交叉验证
gc = GridSearchCV(rf, param_grid={"n_estimators": [250, 100, 500], "max_depth": [5, 7, 10]}, cv=3) # 三轮交叉验证
gc.fit(x_train, y_train)
print("准确率:", gc.score(x_test, y_test))
print("选择的最佳模型:", gc.best_params_)
if name == ‘main’:
# decision_tree()
random_forest()