diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/1.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/1.jpg new file mode 100644 index 0000000..972c043 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/1.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/10.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/10.jpg new file mode 100644 index 0000000..c0ef6a0 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/10.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/11.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/11.jpg new file mode 100644 index 0000000..76baa6b Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/11.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/12.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/12.jpg new file mode 100644 index 0000000..f93e3ed Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/12.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/13.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/13.jpg new file mode 100644 index 0000000..092966c Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/13.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/14.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/14.jpg new file mode 100644 index 0000000..30ba954 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/14.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/15.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/15.jpg new file mode 100644 index 0000000..c9ea0aa Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/15.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/16.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/16.jpg new file mode 100644 index 0000000..2c3e99d Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/16.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/17.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/17.jpg new file mode 100644 index 0000000..f7c16ab Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/17.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/18.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/18.jpg new file mode 100644 index 0000000..6498ae3 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/18.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/19.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/19.jpg new file mode 100644 index 0000000..1faf402 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/19.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/2.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/2.jpg new file mode 100644 index 0000000..e716b43 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/2.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/20.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/20.jpg new file mode 100644 index 0000000..5fa6234 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/20.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/21.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/21.jpg new file mode 100644 index 0000000..3325cf4 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/21.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/22.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/22.jpg new file mode 100644 index 0000000..83b746c Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/22.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/23.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/23.jpg new file mode 100644 index 0000000..cee67e8 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/23.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/24.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/24.jpg new file mode 100644 index 0000000..b735f6c Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/24.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/25.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/25.jpg new file mode 100644 index 0000000..8adefb3 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/25.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/26.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/26.jpg new file mode 100644 index 0000000..8a41e36 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/26.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/3.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/3.jpg new file mode 100644 index 0000000..ccd7c39 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/3.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/4.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/4.jpg new file mode 100644 index 0000000..ea70023 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/4.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/5.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/5.jpg new file mode 100644 index 0000000..639f239 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/5.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/6.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/6.jpg new file mode 100644 index 0000000..2470dbb Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/6.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/7.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/7.jpg new file mode 100644 index 0000000..a10df07 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/7.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/8.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/8.jpg new file mode 100644 index 0000000..af80f9f Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/8.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/9.jpg b/Part2/Chapter6/6.1 泰坦尼克生还预测/9.jpg new file mode 100644 index 0000000..067df38 Binary files /dev/null and b/Part2/Chapter6/6.1 泰坦尼克生还预测/9.jpg differ diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/keepfile.txt b/Part2/Chapter6/6.1 泰坦尼克生还预测/EDA.md similarity index 100% rename from Part2/Chapter6/6.1 泰坦尼克生还预测/keepfile.txt rename to Part2/Chapter6/6.1 泰坦尼克生还预测/EDA.md diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/introduction.md b/Part2/Chapter6/6.1 泰坦尼克生还预测/introduction.md new file mode 100644 index 0000000..5f85f1f --- /dev/null +++ b/Part2/Chapter6/6.1 泰坦尼克生还预测/introduction.md @@ -0,0 +1,9 @@ +# 6.1.1 泰坦尼克生还问题简介 + +泰坦尼克号的沉船事件是是历史上最臭名昭著的沉船事件之一。`1912`年`4`月`15`日,泰坦尼克在航线中与冰山相撞,`2224`名乘客中有`1502`名乘客丧生。 + +泰坦尼克号数据集是目标是给出一个模型来预测某位泰坦尼克号的乘客在沉船事件中是生还是死。而且该数据集是一个非常好的数据集,能够让您快速的开始数据科学之旅。 + +泰坦尼克生还预测实训已在`educoder`平台上提供,若感兴趣可以输入链接进行体验。 + +链接:https://www.educoder.net/shixuns/kz3fixv9/challenges \ No newline at end of file diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/构建模型进行预测.md b/Part2/Chapter6/6.1 泰坦尼克生还预测/构建模型进行预测.md new file mode 100644 index 0000000..5f38a00 --- /dev/null +++ b/Part2/Chapter6/6.1 泰坦尼克生还预测/构建模型进行预测.md @@ -0,0 +1,62 @@ +# 6.1.4 构建模型进行预测 + +做好数据预处理后,可以将数据喂给机器学习模型来进行训练和预测了。不过在构建模型之前,要使用处理训练集数据的方式来处理测试集。 + +```python +test_data=pd.read_csv('./Titanic/test.csv') + +test_data['Initial']=0 +for i in test_data: + test_data.loc[:, 'Initial'] = test_data.Name.str.extract('([A-Za-z]+)\.',expand=False) #lets extract the Salutations + +test_data.loc[:, 'Initial'].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don'],['Miss','Miss','Miss','Other','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr'],inplace=True) + +test_data.loc[(test_data.Age.isnull())&(test_data.Initial=='Mr'),'Age']=33 +test_data.loc[(test_data.Age.isnull())&(test_data.Initial=='Mrs'),'Age']=36 +test_data.loc[(test_data.Age.isnull())&(test_data.Initial=='Miss'),'Age']=22 +test_data.loc[(test_data.Age.isnull())&(test_data.Initial=='Other'),'Age']=46 + +test_data['Embarked'].fillna('S', inplace=True) + +test_data['Age_band']=0 +test_data.loc[test_data['Age']<=16,'Age_band']=0 +test_data.loc[(test_data['Age']>16)&(test_data['Age']<=32),'Age_band']=1 +test_data.loc[(test_data['Age']>32)&(test_data['Age']<=48),'Age_band']=2 +test_data.loc[(test_data['Age']>48)&(test_data['Age']<=64),'Age_band']=3 +test_data.loc[test_data['Age']>64,'Age_band']=4 + +test_data['Family_Size']=0 +test_data['Family_Size']=test_data['Parch']+test_data['SibSp']+1 +test_data['Alone']=0 +test_data.loc[test_data.Family_Size==1,'Alone']=1 + +test_data['Fare_cat']=0 +test_data.loc[test_data['Fare']<=7.91,'Fare_cat']=0 +test_data.loc[(test_data['Fare']>7.91)&(test_data['Fare']<=14.454),'Fare_cat']=1 +test_data.loc[(test_data['Fare']>14.454)&(test_data['Fare']<=31),'Fare_cat']=2 +test_data.loc[(test_data['Fare']>31)&(test_data['Fare']<=513),'Fare_cat']=3 + +test_data['Sex'].replace(['male','female'],[0,1],inplace=True) +test_data['Embarked'].replace(['S','C','Q'],[0,1,2],inplace=True) +test_data['Initial'].replace(['Mr','Mrs','Miss','Master','Other'],[0,1,2,3,4],inplace=True) +test_data['Cabin'].replace(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'T'], [0, 1, 2, 3, 4, 5, 6, 7], inplace=True) + +test_data.drop(['Name','Age','Ticket','Fare','Fare_Range','PassengerId'],axis=1,inplace=True) +``` + +然后可以使用机器学习模型来训练并预测了,这里使用的是随机森林。 + +```python +Y_train = data['Survived'] +X_train = data.drop(['Survived'], axis=1) + +Y_test = test_data['Survived'] +X_test = test_data.drop(['Survived'], axis=1) + +clf = RandomForestClassifier(n_estimators=10) +clf.fit(X_train, Y_train) +predict = clf.predict(X_test) +print(accuracy_score(Y_test, predict)) +``` + +此时看到预测的准确率达到了 0.8275 。 \ No newline at end of file diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/特征工程.md b/Part2/Chapter6/6.1 泰坦尼克生还预测/特征工程.md new file mode 100644 index 0000000..105bc61 --- /dev/null +++ b/Part2/Chapter6/6.1 泰坦尼克生还预测/特征工程.md @@ -0,0 +1,113 @@ +# 6.1.2 特征工程 + +什么是特征工程?其实每当我们拿到数据时,并不是所有的特征都是有用的,可能有许多冗余的特征需要删掉,或者根据`EDA`的结果,我们可以根据已有的特征来添加新的特征,这其实就是特征工程。 + +接下来我们来尝试对一些特征进行处理。 + + +## 年龄离散化 + +年龄是一个连续型的数值特征,有的机器学习算法对于连续性数值特征不太友好,例如决策树、随机森林等`tree-base model`。所以我们可以考虑将年龄转换成年龄段。例如将年龄小于`16`的船客置为`0` ,`16`到`32`岁之间的置为`1`等。 + +```python +data['Age_band']=0 +data.loc[data['Age']<=16,'Age_band']=0 +data.loc[(data['Age']>16)&(data['Age']<=32),'Age_band']=1 +data.loc[(data['Age']>32)&(data['Age']<=48),'Age_band']=2 +data.loc[(data['Age']>48)&(data['Age']<=64),'Age_band']=3 +data.loc[data['Age']>64,'Age_band']=4 +``` + +![](21.jpg) + +我们可以看一下转换成年龄段后,年龄段与生还率的关系。 + +```python +sns.factorplot('Age_band','Survived',data=data,col='Pclass') +plt.show() +``` + +![](22.jpg) + +可以看出和我们之前`EDA`的结果相符,年龄越大,生还率越低。 + +## 家庭成员数量与是否孤身一人 + +由于家庭成员数量和是否孤身一人好像对于是否生还有影响,所以我们不妨添加新的特征。 + +```python +data['Family_Size']=0 +data['Family_Size']=data['Parch']+data['SibSp'] +data['Alone']=0 +data.loc[data.Family_Size==0,'Alone']=1 +``` + +然后再可视化看一下 + +```python +f,ax=plt.subplots(1,2,figsize=(18,6)) +sns.factorplot('Family_Size','Survived',data=data,ax=ax[0]) +ax[0].set_title('Family_Size vs Survived') +sns.factorplot('Alone','Survived',data=data,ax=ax[1]) +ax[1].set_title('Alone vs Survived') +plt.close(2) +plt.close(3) +plt.show() +``` + +![](23.jpg) + +从图中可以很明显的看出,如果你是一个人,那么生还的几率比较低,而且对于人数大于`4`人的家庭来说生还率也比较低。感觉,这可能也是一个比较好的特征,可以再深入的看一下。 + +```python +sns.factorplot('Alone','Survived',data=data,hue='Sex',col='Pclass') +plt.show() +``` + +![](24.jpg) + +可以看出,除了三等舱的单身女性的生还率比非单身女性的生还率高外,单身并不是什么好事。 + +## 花费离散化 + +和年龄一样,花费也是一个连续性的数值特征,所以我们不妨将其离散化。 + +```python +data['Fare_cat']=0 +data.loc[data['Fare']<=7.91,'Fare_cat']=0 +data.loc[(data['Fare']>7.91)&(data['Fare']<=14.454),'Fare_cat']=1 +data.loc[(data['Fare']>14.454)&(data['Fare']<=31),'Fare_cat']=2 +data.loc[(data['Fare']>31)&(data['Fare']<=513),'Fare_cat']=3 + +sns.factorplot('Fare_cat','Survived',data=data,hue='Sex') +plt.show() +``` + +![](25.jpg) + +很明显,花费越多生还率越高,金钱决定命运。 + +## 将字符串特征转换为数值型特征 + +由于我们的机器学习模型不支持字符串,所以需要将一些有用的字符串类型的特征转换成数值型的特征,比如:性别,口岸,姓名前缀。 + +```python +data['Sex'].replace(['male','female'],[0,1],inplace=True) +data['Embarked'].replace(['S','C','Q'],[0,1,2],inplace=True) +data['Initial'].replace(['Mr','Mrs','Miss','Master','Other'],[0,1,2,3,4],inplace=True) +``` + +## 删掉没多大用处的特征 + +- 姓名:难道姓名和生死有关系?这也太玄乎了,我不信,所以把它删掉。 +- 年龄:由于已经根据年龄生成了新的特征“年龄段”,所以这个特征也需要删除。 +- 票:票这个特征感觉是一堆随机的字符串,所以删掉。 +- 花费:和年龄一样,删掉。 +- 船舱:由于有很多缺失值,不好填充,所以可以考虑删掉。 +- 船客`ID`:`ID`和生死应该没啥关系,所以删掉。 + +```python +data.drop(['Name','Age','Ticket','Fare','Cabin','PassengerId'],axis=1,inplace=True) +``` + + diff --git a/Part2/Chapter6/6.1 泰坦尼克生还预测/调参.md b/Part2/Chapter6/6.1 泰坦尼克生还预测/调参.md new file mode 100644 index 0000000..9e99387 --- /dev/null +++ b/Part2/Chapter6/6.1 泰坦尼克生还预测/调参.md @@ -0,0 +1,40 @@ +# 5.1.5 调参 + +很多机器学习算法有很多可以调整的参数(即超参数),例如我们用的随机森林需要我们指定森林中有多少棵决策树,每棵决策树的最大深度等。这些超参数都或多或少的会影响这模型的性能。那么怎样才能找到合适的超参数,来让我们的模型性能达到比较好的效果呢?可以使用网格搜索! + +网格搜索的意思其实就是遍历所有我们想要尝试的参数组合,看看哪个参数组合的性能最高,那么这组参数组合就是模型的最佳参数。 + +`sklearn`为我们提供了网格搜索的接口,能很方便的进行网格搜索。 + +```python +from sklearn.model_selection import GridSearchCV + +# 想要调整的参数的字典,字典的key为参数名字,value为想要尝试参数值 +param_grid = {'n_estimators': [10, 20, 50, 100, 150, 200],'max_depth': [5, 10, 15, 20, 25, 30]} + +# 采用5折验证的方式进行网格搜索,分类器为随机森林 +grid_search = GridSearchCV(RandomForestClassifier(), param_grid, cv=5) +grid_search.fit(X_train, Y_train) + +# 打印最佳参数组合 +print(grid_search.best_params_) +# 打印最佳参数组合时模型的最佳性能 +print(grid_search.best_score_) +``` + +![](26.jpg) + +可以看到经过调参之后,随机森林模型的性能提高到了 `0.8323`,提升了接近`1%`的准确率。然后使用最佳参数构造随机森林,并对测试集测试会发现,测试集的准确率达到了 `0.8525`。 + +```python +Y_train = data['Survived'] +X_train = data.drop(['Survived'], axis=1) + +Y_test = test_data['Survived'] +X_test = test_data.drop(['Survived'], axis=1) + +clf = RandomForestClassifier(n_estimators=50, max_depth=5) +clf.fit(X_train, Y_train) +predict = clf.predict(X_test) +print(accuracy_score(Y_test, predict)) +``` \ No newline at end of file