aolingwen 6 years ago
parent 33ef096a14
commit 031cf6c181

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

@ -0,0 +1,9 @@
# 6.1.1 泰坦尼克生还问题简介
泰坦尼克号的沉船事件是是历史上最臭名昭著的沉船事件之一。`1912`年`4`月`15`日,泰坦尼克在航线中与冰山相撞,`2224`名乘客中有`1502`名乘客丧生。
泰坦尼克号数据集是目标是给出一个模型来预测某位泰坦尼克号的乘客在沉船事件中是生还是死。而且该数据集是一个非常好的数据集,能够让您快速的开始数据科学之旅。
泰坦尼克生还预测实训已在`educoder`平台上提供,若感兴趣可以输入链接进行体验。
链接https://www.educoder.net/shixuns/kz3fixv9/challenges

@ -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 。

@ -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)
```

@ -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))
```
Loading…
Cancel
Save