| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- #!/usr/bin/env python
- # coding: utf-8
- # In[4]:
- import pandas as pd
- # In[5]:
- # 1、获取数据
- titianic = pd.read_csv("./titanic.csv")
- titianic.head()
- # In[6]:
- # 筛选特征值和目标值
- x = titianic[["pclass", "age", "sex"]]
- y = titianic["survived"]
- # In[7]:
- x.head()
- # In[8]:
- y.head()
- # In[9]:
- # 2、数据处理
- # 1) 缺失值处理 age字段
- x.loc[:, "age"] = x["age"].fillna(x["age"].mean())
- # In[10]:
- # 2) 转换成字典
- x = x.to_dict(orient="records")
- # In[11]:
- from sklearn.model_selection import train_test_split
- # 3、数据集划分
- x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=22)
- # In[12]:
- # 4、字典特征抽取
- from sklearn.feature_extraction import DictVectorizer
- # In[13]:
- transfer = DictVectorizer()
- x_train = transfer.fit_transform(x_train)
- x_test = transfer.transform(x_test)
- # In[14]:
- from sklearn.tree import DecisionTreeClassifier
- from sklearn.tree import export_graphviz
- # 5、模型训练和预测
- estimator = DecisionTreeClassifier(criterion="entropy")
- estimator.fit(x_train, y_train)
- # 6、 模型评估
- # 方法一: 直接比对真实值和预测值
- y_predict = estimator.predict(x_test)
- print("直接比对真实值和预测值:\n", y_test == y_predict)
- # 方法二: 计算准确率
- score = estimator.score(x_test, y_test)
- print("准确率: \n", score)
- # 6) 决策树的可视化
- export_graphviz(estimator, out_file="./titian_tree.dot", feature_names=transfer.get_feature_names_out())
- # #### 随机森林预测泰坦尼克数据集
- # In[15]:
- from sklearn.ensemble import RandomForestClassifier
- from sklearn.model_selection import GridSearchCV
- # In[16]:
- # 4) 随机森林算法预估
- estimator = RandomForestClassifier(n_estimators=120, random_state=22, max_features="sqrt") # n_estimators: 树的数量
- # 加入网格搜索与交叉验证
- param_grid = {"n_estimators": [120, 200, 300, 500, 800, 1200],
- "max_depth": [5, 8, 15, 25, 30]}
- estimator = GridSearchCV(estimator, param_grid=param_grid, cv=3)
- estimator.fit(x_train, y_train)
- # 5) 模型评估
- # 方法一: 直接比对真实值和预测值
- y_predict = estimator.predict(x_test)
- print("y_predict:\n", y_predict)
- print("直接比对真实值和预测值:\n", y_test == y_predict)
- # 方法二: 计算准确率
- score = estimator.score(x_test, y_test)
- print("准确率: \n", score)
- # 6. 获取最佳参数
- print("最佳参数: \n", estimator.best_params_)
- print("最佳结果: \n", estimator.best_score_)
- print("最佳估计器: \n", estimator.best_estimator_)
- print("交叉验证结果: \n", estimator.cv_results_)
|