#泰坦尼克号
https://www.kaggle.com/ 数据来源
import pandas#读取数据
titanic = pandas.read_csv("D:/panana/taitan/titanic_train.csv")#查看数据前五行
#print(titanic.head())#进行统计#
print (titanic.describe())
#对缺失值进行填写 把缺失的值按照平均值填充 fillna 缺失值填充
titanic["Age"].median
求出age的均值
titanic["Age"] = titanic["Age"].fillna(titanic["Age"].median())
#print(titanic.describe())
#数据预处理 把不能处理的值改成可以处理的 把字母改成数字
#先查看 字段 Sex 下有哪几种可能
#print(titanic["Sex"].unique())
#因为性别是str类型 所以要转换成int male 转换成0 female 转换成1
#loc 进行定位 把字段Sex ==male 改成 Sex==0titanic.loc[titanic["Sex"]=="male","Sex"] =0titanic.loc[titanic["Sex"]=="female","Sex"] =1
#上船地点 把str类型处理成int#查看Embarked 上船地点有哪几种类型:print(titanic["Embarked"].unique())
#得出“S”,"C","Q",nan
#因Embarked 有缺失值 所以需要补全缺失值 把所有的缺失值补成Stitanic["Embarked"] = titanic["Embarked"].fillna('S')
#把数据进行转换 S,C,Q 转换成0,1,2
titanic.loc[titanic["Embarked"]=="S","Embarked"] =0
titanic.loc[titanic["Embarked"]=="C","Embarked"] =1
titanic.loc[titanic["Embarked"]=="Q","Embarked"] =2
#导入机器学习裤子 LinearRegression 线性回归算法
from sklearn.linear_modelimport LinearRegression
#引入交叉验证
from sklearn.cross_validationimport KFold
#选择要搞的特征
predictors = ["Pclass","Sex","Age","SibSp","Parch","Fare","Embarked"]
#导入线性回归alg = LinearRegression()
#进行交叉验证的选择 n_folds =3 表示选择3倍的交叉验证 titanic.shape[0]表示m个样本#含义就是 把m个样本平均分成三份进行交叉验证
kf = KFold(titanic.shape[0],n_folds=3,random_state=1)
predictions =[]
for train, test in kf:
#取到原始数据 train 代表训练数据 先把训练数据提取出来
train_predictors = (titanic[predictors].iloc[train,:])
train_target = titanic["Survived"].iloc[train]
#.fit 表示把选择的算法应用在当前的数据上
alg.fit(train_predictors, train_target)
#训练完后查看模型的好坏程度 alg.predict() 对测试集数据进行预测
test_predictions = alg.predict(titanic[predictors].iloc[test,:])
#把测试结果导入到predictors里面
predictions.append(test_predictions)
import numpy as np
predictions = np.concatenate(predictions,axis=0)
#类别归属 当大于0.5 存放到1 小于0.5存放到0
predictions[predictions > .5] =1
predictions[predictions <= .5]=0
#查看模型准确率
accuracy = sum(predictions[predictions == titanic["Survived"]]) /len(predictions)
print(accuracy)
最终准确率只有百分之20 --换种方法
0.2615039281705948
from sklearn import cross_validation
#LogisticRegression 逻辑回归
from sklearn.linear_model import LogisticRegression
alg = LogisticRegression(random_state = 1)
scores = cross_validation.cross_val_score(alg,titanic[predictors],titanic["Survived"],cv = 3 )
print(scores.mean())
最终准确率
0.7878787878787877
使用集成算法 随机森林
#使用随机森林进行预测 防止过拟合
from sklearn import cross_validation
#调用随机森林的包
from sklearn.ensemble import RandomForestClassifier
predictors = ["Pclass","Sex","Age","SibSp","Parch","Fare","Embarked"]
#指定随机森林的参数 n_estimators设置决策树的个数 min_samples_split最小的样本个数 min_samples_leaf 最小叶子节点的个数
alg = RandomForestClassifier(random_state=1,n_estimators=10,min_samples_split=2,min_samples_leaf=1)
#进行三次交叉验证
kf =cross_validation.KFold(titanic.shape[0],n_folds=3,random_state=1)
scores = cross_validation.cross_val_score(alg,titanic[predictors],titanic["Survived"],cv=kf)
print(scores.mean())
最终准确率
0.7856341189674523
#对参数进行修改
#更改参数像后在进行查看 把决策树个数改成50 最小样本个数提升到4 最小叶子节点提升到2
alg = RandomForestClassifier(random_state=1,n_estimators=50,min_samples_split=4,min_samples_leaf=2)
kf =cross_validation.KFold(titanic.shape[0],n_folds=3,random_state=1)
scores = cross_validation.cross_val_score(alg,titanic[predictors],titanic["Survived"],cv=kf)
print(scores.mean())
最终准确率
0.8159371492704826
未完--
网友评论