机器学习:泰坦尼克之灾获救预测

机器学习环境:Pycharm + Python3.6
数据来源:Kaggle网站

import numpy as np
import  pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

filename = 'D:/my_project/Titanic_disaster/train.csv'
data_train = pd.read_csv(filename)
print(data_train.info())

# 1. 数据预处理
### 利用RFR填补age属性缺失值
from sklearn.ensemble import RandomForestRegressor
def set_missing_age(df):
    age_df = df[['Age','Fare', 'Parch', 'SibSp', 'Pclass']]
    known_age = age_df[age_df.Age.notnull()].values
    unknown_age = age_df[age_df.Age.isnull()].values

    y = known_age[:,0]
    X = known_age[:,1:]

    rfr = RandomForestRegressor(random_state=0,n_estimators=2000,n_jobs=1)
    rfr.fit(X,y)
    predictAges = rfr.predict(unknown_age[:,1:])
    df.loc[(df.Age.isnull()),'Age'] = predictAges

    return df,rfr

def set_cabin_type(df):
    df.loc[(df.Cabin.notnull()),'Cabin'] = 'Yes'
    df.loc[(df.Cabin.isnull()), 'Cabin'] = 'No'
    return df

data_train, rfr = set_missing_age(data_train)
data_train = set_cabin_type(data_train)

### 使用类型变量编码
dummies_Cabin = pd.get_dummies(data_train.Cabin, prefix='Cabin')
dummies_Embarked = pd.get_dummies(data_train.Embarked, prefix='Embarked')
dummies_Sex = pd.get_dummies(data_train.Sex,prefix='Sex')
dummies_Pclass = pd.get_dummies(data_train.Pclass, prefix='Pclass')
df = pd.concat([data_train,dummies_Cabin,dummies_Embarked,dummies_Sex,dummies_Pclass], axis=1)
df.drop(['Pclass', 'Name', 'Sex', 'Ticket', 'Cabin', 'Embarked'], axis=1, inplace=True)

### 训练集数据标准化
from  sklearn import preprocessing
scaler = preprocessing.StandardScaler()
age_scale_param_train = scaler.fit(df['Age'].values.reshape(-1,1))
df['Age_scaled'] = scaler.fit_transform(df['Age'].values.reshape(-1,1), age_scale_param_train)
fare_scale_param_train = scaler.fit(df['Fare'].values.reshape(-1,1))
df['Fare_scaled'] = scaler.fit_transform(df['Fare'].values.reshape(-1,1), fare_scale_param_train)

# 2. 建立模型
from sklearn import linear_model
train_df = df.filter(regex='Survived|Age_.*|SibSp|Parch|Fare_.*|Cabin_.*|Embarked_.*|Sex_.*|Pclass_.*')
train_np = train_df.values

y_train = train_np[:,0]
X_train = train_np[:,1:]
clf = linear_model.LogisticRegression(C=1.0, penalty='l1', tol=1e-6)
clf.fit(X_train, y_train)

# 3. 处理验证数据
data_test = pd.read_csv('D:/my_project/Titanic_disaster/test.csv')
data_test.loc[(data_test['Fare'].isnull()), 'Fare'] = 0
### 对age、cabin进行填补
tmp_df = data_test[['Age','Fare', 'Parch', 'SibSp', 'Pclass']]
null_age = tmp_df[tmp_df.Age.isnull()].values
X = null_age[:,1:]
predictAges = rfr.predict(X)
data_test.loc[(data_test.Age.isnull()), 'Age'] = predictAges
data_test = set_cabin_type(data_test)

### 类别变量编码
dummies_Cabin = pd.get_dummies(data_test.Cabin,prefix='Cabin')
dummies_Embarked = pd.get_dummies(data_test.Embarked,prefix='Embarked')
dummies_Sex = pd.get_dummies(data_test.Sex,prefix='Sex')
dummies_Pclass = pd.get_dummies(data_test.Pclass,prefix='Pclass')
df_test = pd.concat([data_test,dummies_Cabin,dummies_Embarked,dummies_Sex,dummies_Pclass],axis=1)
df_test.drop(['Name','Cabin','Embarked','Ticket','Sex','Pclass'],axis=1,inplace=True)

df_test['Age_scaled'] = scaler.transform(df_test['Age'].values.reshape(-1,1))
df_test['Fare_scaled'] = scaler.fit_transform(df_test['Fare'].values.reshape(-1,1))

test = df_test.filter(regex='Age_.*|SibSp|Parch|Fare_.*|Cabin_.*|Embarked_.*|Sex_.*|Pclass_.*')
predictions = clf.predict(test.values)
result = pd.DataFrame({'PassengerId': data_test.PassengerId.values,'Survived':predictions.astype(np.int32)})
#result.to_csv('D:/my_project/Titanic_disaster/logistic_regression_predictions.csv', index=False)

pd.DataFrame({'columns':list(train_df.columns[1:]), 'coef':list(clf.coef_.T)})

# 4. cross validation
from sklearn import model_selection
clf = linear_model.LogisticRegresson(C=0.1,penalty='l1', tol=1e-6)
all_data = df.filter(regex='Survived|Age_.*|SibSp|Parch|Fare_.*|Cabin_.*|Embarked_.*|Sex_.*|Pclass_.*')
X = all_data.values[:,1:]
y = all_data.values[:,0]
print(model_selection.cross_val_score(clf,X,y,cv=5))

###分割训练集成为训练集和测试集
split_train, split_cv = model_selection.train_test_split(df, test_size=0.3,random_state=0)
train_df = split_train.filter(regex='Survived|Age_.*|SibSp|Parch|Fare_.*|Cabin_.*|Embarked_.*|Sex_.*|Pclass_.*')
clf = linear_model.LogisticRegression(C=0.1,penalty='l1',tol=1e-6)
clf.fit(train_df.iloc[:,1:].values, train_df.iloc[:, 0].values)
### 对验证集进行预测
cv_df = split_cv.filter(regex='Survived|Age_.*|SibSp|Parch|Fare_.*|Cabin_.*|Embarked_.*|Sex_.*|Pclass_.*')
predictions = clf.predict(cv_df.iloc[:,1:].values)

bad_cases = data_train.loc[data_train['PassengerId'].isin(split_cv[predictions != cv_df.iloc[:,0]]['PassengerId'].values)]
print(bad_cases.head())


# 5. model ensemble
from sklearn.ensemble import BaggingRegressor
train_df = df.filter(regex='Survived|Age_.*|SibSp|Parch|Fare_.*|Cabin_.*|Embarked_.*|Sex_.*|Pclass.*|Mother|Child|Family|Title')
train_np = train_df.values
X = train_np[:,1:]
y = train_np[:,0]
clf = linear_model.LogisticRegression(C=1.0, penalty='l1', tol=1e-6)
Bagging_clf = BaggingRegressor(clf,n_estimators=20,max_samples=0.8,max_features=1.0,bootstrap=True,bootstrap_features=False,n_jobs=1)
Bagging_clf.fit(X,y)
test = df_test.filter(regex='Age_.*|SibSp|Parch|Fare_.*|Cabin_.*|Embarked_.*|Sex_.*|Pclass.*|Mother|Child|Family|Title')
predictions = Bagging_clf.predict(test)
result = pd.DataFrame({'columns':df_test['PassengerId'].values, 'Survived':predictions.astype(np.int32)})
# result.to_csv('D:/my_project/Titanic_disaster/logistic_regression_Bagging_predictions.csv',index=False)

https://blog.csdn.net/han_xiaoyang/article/details/49797143#t18

你可能感兴趣的:(机器学习:泰坦尼克之灾获救预测)