分类算法分析船员数据

数据预处理

import pandas
titanic=pandas.read_csv("titanic_train.csv")
print(titanic.describe())
titanic['Age']=titanic['Age'].fillna(titanic['Age'].median())
print(titanic.describe())
print(titanic['Sex'].unique())
#Replace all the occurences of male with number 0
titanic.loc[titanic['Sex']=='male','Sex']=0
titanic.loc[titanic['Sex']=='female','Sex']=1
print(titanic['Embarked'].unique)
titanic['Embarked']=titanic['Embarked'].fillna('S')
titanic.loc[titanic['Embarked']=='S','Emberked']=0
titanic.loc[titanic['Embarked']=='C','Emberked']=1
titanic.loc[titanic['Embarked']=='Q','Embarked']=2

使用逻辑回归算法进行预测

#使用逻辑回归算法进行预测
#Import the linear regression class
from sklearn.linear_model import LinearRegression
#Sklearn also has a helper that makes it easy to do cross validation
from sklearn.model_selection import KFold
#The columns we will use to predict the target
predictors=['Pclass','Sex','Age','SibSp','Parch','Fare','Embarked']#分类器特征
#Initialize our algorithm class
alg=LinearRegression()
#Generate cross validation folds for titanic dataset.It return the row indices corresponding to train and test
#We set random_state to ensure we get the same splits every time we run this
kf=KFold(titanic.shape[0],random_state=1)
predictions=[]
for train,test in kf.split():
    #The predictions we are using the train algorithm. Note how we only take the rows in the train folds
    train_predictions=(titanic[predictions].iloc[train,:])
    #The target we are using to train the algorithm
    train_target=titanic['Survived'].iloc[train]
    #Training the algorithm using the predictions and target
    alg.fit(train_predictors,train_target)
    #We can now make predictions on the test fold
    test_predictions=alg.predict(titanic[predictions].iloc[test,:])
    predictions.append(test_predictions)
import numpy as np
#The predictions are in three separate numpy arrays. Concatenate them into one.
#We concatenate them on axis 0, as they only have one axis
predictions=np.concatenate(predictions,axis=0)
#Map predictions to outcomes (only possible are 1 and 0)
predictions[predictions>0.5]=1
predictions[prediction<=0.5]=0
accuracy=sum(predictions[predictions==titanic['Survived']])/len(predictions)
print(accuracy)

使用随机森林改进模型

#使用随机森林改进模型
from sklearn import cross_validation
from sklearn.ensemble import RandomForestClassifier
predictions=['Pclass','Sex','Age','SipSp','Parch','Free','Embarked']
#Initialize our algorithm with the default paramters
#n_estimators is the number of trees we want to make
#min_samples_split is the minimum number of rows we need to make a split
#min_samples_leaf is the minimum number of samples we can have at the place where a tree branch ends
alg=RandomForestClassfier(random_state=1,n_estimators=10,min_samples_split=2,min_samples_leaf=1)
#Compute the acuuracy score for all the cross validation folds.(much simpler than what we did)
kf=model_slection.KFold(titanic.shape[0],n_folds=3,random_state=1)
scores=cross_validation.cross_val_score(alg,titanic[predictors].titanic['Survived'],cv=kf)
#The the mean of the scores (before we have one for each fold
print(score.mean())

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值