Python源码:
#!/usr/bin/env python
# encoding: utf-8
"""
@Company:华中科技大学电气学院聚变与等离子研究所
@version: V1.0
@author: Victor
@contact: [email protected]
@software: PyCharm
@file: Taitannic.py
@time: 2018/11/20 19:39
@Desc:通过使用各种算法预测泰坦尼克号上的某个人是否获救(根据每个人的特征建立模型)
"""
###通过建立模型预测一个人是否被获救
import pandas as pd
taitannic = pd.read_csv("taitannic_train.csv")
# taitannic.head()
# print(taitannic.describe())###计算输出每个特征的统计信息,可以看到age特征中有缺失!!!!
###用均值填补age的缺失值
taitannic['Age'] = taitannic['Age'].fillna(taitannic['Age'].median())
# print(taitannic.describe())
print(taitannic['Sex'].unique())
###将字符型性别转化为0和1,因为机器学习处理数据都是转化为数值型。
taitannic.loc[taitannic['Sex'] == 'male', 'Sex'] = 0
taitannic.loc[taitannic['Sex'] == 'female', 'Sex'] = 1
print(taitannic['Sex'].unique())
####将字符型Embarked转化为数值型,并且用众数填充缺失值
print(taitannic["Embarked"].unique())
taitannic["Embarked"] = taitannic["Embarked"].fillna('S')
taitannic.loc[taitannic["Embarked"] == "S", "Embarked"] = 0
taitannic.loc[taitannic["Embarked"] == "C", "Embarked"] = 1
taitannic.loc[taitannic["Embarked"] == "Q", "Embarked"] = 2
print(taitannic["Embarked"].unique())
#######调用线性回归的模型库来训练数据得到好的参数#############
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import KFold
from sklearn.model_selection import train_test_split
##选择的特征
predictors = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked"]
alg = LinearRegression() ##初始化模型对象
kf = KFold(3, False, random_state=1)
print(kf)
##kf是切分后的数据(训练集中的train+test)
predictions = []
for train, test in kf.split(taitannic[predictions]):
###train和test都是划分后的索引
# print(train)
# print('===========')
# print(test)
# print('================***************')
train_predictors = (taitannic[predictors].iloc[train, :]) ##通过行号索引数据,只取选好的特征数据
# print(taitannic[predictors])
train_target = taitannic['Survived'].iloc[train]
###训练
alg.fit(train_predictors, train_target)
####预测
test_predictions = alg.predict(taitannic[predictors].iloc[test, :])
predictions.append(test_predictions) ##保存三次交叉验证中每个人的预测正确率
##print(sum(predictions))
import numpy as np
predictions = np.concatenate(predictions, axis=0)
##print(predictions)
####如果正确率大于0.5则对,否则为错
predictions[predictions > 0.5] = 1
predictions[predictions <= 0.5] = 0
####和实际结果进行比对,计算准确率
accuracy = sum(predictions[predictions == taitannic['Survived']]) / len(predictions)
print(accuracy * 3) ###三次交叉验证均化了
###########逻辑回归:通过概率值###################################
from sklearn import cross_validation
from sklearn.linear_model import LogisticRegression
# Initialize our algorithm
alg = LogisticRegression(random_state=1)
# Compute the accuracy score for all the cross validation folds. (much simpler than what we did before!)
scores = cross_validation.cross_val_score(alg, taitannic[predictors], taitannic["Survived"], cv=3)
# Take the mean of the scores (because we have one for each fold)
print(scores.mean())
######测试数据集#############################################
taitannic_test = pd.read_csv("test.csv")
taitannic_test["Age"] = taitannic_test["Age"].fillna(taitannic["Age"].median())
taitannic_test["Fare"] = taitannic_test["Fare"].fillna(taitannic_test["Fare"].median())
taitannic_test.loc[taitannic_test["Sex"] == "male", "Sex"] = 0
taitannic_test.loc[taitannic_test["Sex"] == "female", "Sex"] = 1
taitannic_test["Embarked"] = taitannic_test["Embarked"].fillna("S")
taitannic_test.loc[taitannic_test["Embarked"] == "S", "Embarked"] = 0
taitannic_test.loc[taitannic_test["Embarked"] == "C", "Embarked"] = 1
taitannic_test.loc[taitannic_test["Embarked"] == "Q", "Embarked"] = 2
######随机森林分类#################################################
from sklearn import cross_validation
from sklearn.ensemble import RandomForestClassifier
predictors = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked"]
alg = RandomForestClassifier(random_state=1, n_estimators=10, min_samples_split=2, min_samples_leaf=1)
kf = cross_validation.KFold(taitannic.shape[0], n_folds=3, random_state=1)
scores = cross_validation.cross_val_score(alg, taitannic[predictors], taitannic["Survived"], cv=kf)
print(scores.mean())
####改变树的数量
alg = RandomForestClassifier(random_state=1, n_estimators=60, min_samples_split=4, min_samples_leaf=2)
kf = cross_validation.KFold(taitannic.shape[0], 3, random_state=1)
scores = cross_validation.cross_val_score(alg, taitannic[predictors], taitannic["Survived"], cv=kf)
print(scores.mean())
#####重新构造特征参数来优化模型############################
# 家族特征
taitannic["FamilySize"] = taitannic["SibSp"] + taitannic["Parch"]
##名字长度特征
taitannic["NameLength"] = taitannic["Name"].apply(lambda x: len(x))
import re
def get_title(name):
title_search = re.search(' ([A-Za-z]+)\.', name)
if title_search:
return title_search.group(1)
return ""
titles = taitannic["Name"].apply(get_title)
print(pd.value_counts(titles))
###看名字中的称呼是否有对生存有影响
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Dr": 5, "Rev": 6, "Major": 7, "Col": 7, "Mlle": 8,
"Mme": 8, "Don": 9, "Lady": 10, "Countess": 10, "Jonkheer": 10, "Sir": 9, "Capt": 7, "Ms": 2}
for k, v in title_mapping.items():
titles[titles == k] = v
print(pd.value_counts(titles))
taitannic["Title"] = titles
#############探索各个特征的重要性,通过改变某个特征的数据,再看结果变化是否很大来确定重要性#######
import numpy as np
from sklearn.feature_selection import SelectKBest, f_classif
import matplotlib.pyplot as plt
predictors = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare", "Embarked", "FamilySize", "Title", "NameLength"]
selector = SelectKBest(f_classif, k=5)
selector.fit(taitannic[predictors], taitannic["Survived"])
scores = -np.log10(selector.pvalues_)
plt.bar(range(len(predictors)), scores)
plt.xticks(range(len(predictors)), predictors, rotation='vertical')
plt.show() ####直方图长的重要性高一些。
predictors = ["Pclass", "Sex", "Fare", "Title"]
alg = RandomForestClassifier(random_state=1, n_estimators=50, min_samples_split=8, min_samples_leaf=4)
##################集成算法分类################################
from sklearn.ensemble import GradientBoostingClassifier
import numpy as np
# The algorithms we want to ensemble.
# We're using the more linear predictors for the logistic regression, and everything with the gradient boosting classifier.
algorithms = [
[GradientBoostingClassifier(random_state=1, n_estimators=25, max_depth=3),
["Pclass", "Sex", "Age", "Fare", "Embarked", "FamilySize", "Title", ]],
[LogisticRegression(random_state=1), ["Pclass", "Sex", "Fare", "FamilySize", "Title", "Age", "Embarked"]]
]
# Initialize the cross validation folds
kf = KFold(taitannic.shape[0], n_folds=3, random_state=1)
predictions = []
for train, test in kf:
train_target = taitannic["Survived"].iloc[train]
full_test_predictions = []
for alg, predictors in algorithms:
alg.fit(taitannic[predictors].iloc[train, :], train_target)
test_predictions = alg.predict_proba(taitannic[predictors].iloc[test, :].astype(float))[:, 1]
full_test_predictions.append(test_predictions)
test_predictions = (full_test_predictions[0] + full_test_predictions[1]) / 2
test_predictions[test_predictions <= .5] = 0
test_predictions[test_predictions > .5] = 1
predictions.append(test_predictions)
predictions = np.concatenate(predictions, axis=0)
accuracy = sum(predictions[predictions == taitannic["Survived"]]) / len(predictions)
print(accuracy)