[机器学习]贝叶斯算法 对泰坦尼克号生存人群 分类预测 [简单示例]

1.代码

  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 # @File  : 泰坦尼克号.py
  4 # @Author: 赵路仓
  5 # @Date  : 2020/4/4
  6 # @Desc  :
  7 # @Contact : 398333404@qq.com 
  8 
  9 import pandas as pd
 10 from sklearn.model_selection import train_test_split, GridSearchCV
 11 from sklearn.tree import DecisionTreeClassifier, export_graphviz
 12 from sklearn.feature_extraction import DictVectorizer
 13 from sklearn.ensemble import RandomForestClassifier
 14 
 15 
 16 
 17 def titanic():
 18     # 导入数据
 19     titanic = pd.read_csv("titanic.txt")
 20     # print(titanic)
 21 
 22     # 筛选特征值和目标值
 23     x = titanic[["pclass", "age", "sex"]]
 24     y = titanic["survived"]
 25     # print(x)
 26     # print(y)
 27 
 28     # 数据处理
 29     # 1.缺失值处理
 30     x["age"].fillna(x["age"].mean(), inplace=True)
 31     # print(x["age"])
 32     # 2.转换成字典
 33     x = x.to_dict(orient="records")
 34     # 3.数据集划分
 35     x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=21)
 36 
 37     # 4.字典特征抽取
 38     transfer = DictVectorizer()
 39     x_train = transfer.fit_transform(x_train)
 40     x_test = transfer.transform(x_test)
 41     print(x_train.toarray())
 42     print(transfer.get_feature_names())
 43     # 5.决策树预估器
 44     estimator = DecisionTreeClassifier(criterion="entropy")
 45     estimator.fit(x_train, y_train)
 46 
 47     # 6.模型评估
 48     # 方法一:直接对比真实数据和预测值
 49     y_predit = estimator.predict(x_test)
 50 
 51     print("y_predit:\n", y_predit)
 52     print("对比真实值和预测值:\n", y_test == y_predit)
 53 
 54     # 方法2:计算准确率
 55     score = estimator.score(x_test, y_test)
 56     print("准确率为:\n", score)
 57 
 58     # 预测
 59     pre=transfer.transform([{'pclass': '1st', 'age': 48.0, 'sex': 'male'}])
 60     prediction=estimator.predict(pre)
 61     print(prediction)
 62     # 可视化决策树
 63     # 生成文件
 64     dot_data = export_graphviz(estimator, out_file="titanic.dot")
 65 
 66 
 67 def titanic_forest():
 68     # 导入数据
 69     titanic = pd.read_csv("titanic.txt")
 70     # print(titanic)
 71 
 72     # 筛选特征值和目标值
 73     x = titanic[["pclass", "age", "sex"]]
 74     y = titanic["survived"]
 75 
 76     # 数据处理
 77     # 1.缺失值处理
 78     x["age"].fillna(x["age"].mean(), inplace=True)
 79     # print(x["age"])
 80     # 2.转换成字典
 81     x = x.to_dict(orient="records")
 82     # 3.数据集划分
 83     x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=21)
 84 
 85     # 4.字典特征抽取
 86     transfer = DictVectorizer()
 87     x_train = transfer.fit_transform(x_train)
 88     x_test = transfer.transform(x_test)
 89 
 90     estimator=RandomForestClassifier()
 91     # 加入网格搜索与交叉验证
 92     param_dict = {
 93         "n_estimators": [120,200,300,500,800,1200],"max_depth":[5,8,15,25,30]
 94     }
 95     estimator = GridSearchCV(estimator, param_grid=param_dict, cv=3)
 96     estimator.fit(x_train, y_train)
 97 
 98     # 5.模型评估
 99     # 方法一:直接对比真实数据和预测值
100     y_predit = estimator.predict(x_test)
101     print("y_predit:\n", y_predit)
102     print("对比真实值和预测值:\n", y_test == y_predit)
103 
104     # 方法2:计算准确率
105     score = estimator.score(x_test, y_test)
106     print("准确率为:\n", score)
107 
108     """
109        最佳参数:best_params_
110        最佳结果:best_score_
111        最佳估计器:best_estimator_
112        交叉验证结果:cv_results_
113     """
114     print("最佳参数:\n", estimator.best_params_)
115     print("最佳结果:\n", estimator.best_score_)
116     print("最佳估计器:\n", estimator.best_estimator_)
117     print("交叉验证结果:\n", estimator.cv_results_)
118     return None
119 
120 
121 if __name__ == "__main__":
122     titanic()
123     titanic_forest()

2.解释

第一个函数 titanic() 根据游客数据

1.筛选有效数据

2.缺失值处理

3.转换为字典

5.划分数据集

6.转换为特征值

7.训练模型

8.模型评估

9.预测

形成模型并评估,可以进行简单的预测分类

第二个函数 titanic_forest() 

随机森林找到最优方案/模型,确定最优参数等等

posted @ 2020-04-09 10:50  雾霾王者  阅读(1118)  评论(0编辑  收藏  举报