Update Day 33 Random Forests
This commit is contained in:
@ -2,23 +2,27 @@
|
|||||||
<p align="center">
|
<p align="center">
|
||||||
<img src="https://github.com/MachineLearning100/100-Days-Of-ML-Code/blob/master/Info-graphs/Day%2033.png">
|
<img src="https://github.com/MachineLearning100/100-Days-Of-ML-Code/blob/master/Info-graphs/Day%2033.png">
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
### 导入库
|
### 导入库
|
||||||
```python
|
```python
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
```
|
```
|
||||||
|
|
||||||
### 导入数据集
|
### 导入数据集
|
||||||
```python
|
```python
|
||||||
dataset = pd.read_csv('Social_Network_Ads.csv')
|
dataset = pd.read_csv('Social_Network_Ads.csv')
|
||||||
X = dataset.iloc[:, [2, 3]].values
|
X = dataset.iloc[:, [2, 3]].values
|
||||||
y = dataset.iloc[:, 4].values
|
y = dataset.iloc[:, 4].values
|
||||||
```
|
```
|
||||||
|
|
||||||
### 将数据集拆分成训练集和测试集
|
### 将数据集拆分成训练集和测试集
|
||||||
```python
|
```python
|
||||||
from sklearn.cross_validation import train_test_split
|
from sklearn.cross_validation import train_test_split
|
||||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)
|
||||||
```
|
```
|
||||||
|
|
||||||
### 特征缩放
|
### 特征缩放
|
||||||
```python
|
```python
|
||||||
from sklearn.preprocessing import StandardScaler
|
from sklearn.preprocessing import StandardScaler
|
||||||
@ -26,21 +30,25 @@ sc = StandardScaler()
|
|||||||
X_train = sc.fit_transform(X_train)
|
X_train = sc.fit_transform(X_train)
|
||||||
X_test = sc.transform(X_test)
|
X_test = sc.transform(X_test)
|
||||||
```
|
```
|
||||||
|
|
||||||
### 调试训练集的随机森林
|
### 调试训练集的随机森林
|
||||||
```python
|
```python
|
||||||
from sklearn.ensemble import RandomForestClassifier
|
from sklearn.ensemble import RandomForestClassifier
|
||||||
classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0)
|
classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0)
|
||||||
classifier.fit(X_train, y_train)
|
classifier.fit(X_train, y_train)
|
||||||
```
|
```
|
||||||
|
|
||||||
### 预测测试集结果
|
### 预测测试集结果
|
||||||
```python
|
```python
|
||||||
y_pred = classifier.predict(X_test)
|
y_pred = classifier.predict(X_test)
|
||||||
```
|
```
|
||||||
|
|
||||||
### 生成混淆矩阵,也称作误差矩阵
|
### 生成混淆矩阵,也称作误差矩阵
|
||||||
```python
|
```python
|
||||||
from sklearn.metrics import confusion_matrix
|
from sklearn.metrics import confusion_matrix
|
||||||
cm = confusion_matrix(y_test, y_pred)
|
cm = confusion_matrix(y_test, y_pred)
|
||||||
```
|
```
|
||||||
|
|
||||||
### 将训练集结果可视化
|
### 将训练集结果可视化
|
||||||
```python
|
```python
|
||||||
from matplotlib.colors import ListedColormap
|
from matplotlib.colors import ListedColormap
|
||||||
@ -60,6 +68,7 @@ plt.ylabel('Estimated Salary')
|
|||||||
plt.legend()
|
plt.legend()
|
||||||
plt.show()
|
plt.show()
|
||||||
```
|
```
|
||||||
|
|
||||||
### 将数据集结果可视化
|
### 将数据集结果可视化
|
||||||
```python
|
```python
|
||||||
from matplotlib.colors import ListedColormap
|
from matplotlib.colors import ListedColormap
|
||||||
@ -78,3 +87,4 @@ plt.xlabel('Age')
|
|||||||
plt.ylabel('Estimated Salary')
|
plt.ylabel('Estimated Salary')
|
||||||
plt.legend()
|
plt.legend()
|
||||||
plt.show()
|
plt.show()
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user