数据预处理
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())