#### Random Forest Regression

H
```{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"collapsed": true
},
"outputs": [],
"source": [
"# Importing the libraries\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"from sklearn.ensemble import RandomForestRegressor"
]
},
{
"cell_type": "code",
"execution_count": 2,
"collapsed": true
},
"outputs": [],
"source": [
"# Importing the dataset\n",
"X = dataset.iloc[:, 1:2].values\n",
"y = dataset.iloc[:, 2].values"
]
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [
{
"data": {
"text/plain": [
"RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=None,\n",
"           max_features='auto', max_leaf_nodes=None,\n",
"           min_impurity_split=1e-07, min_samples_leaf=1,\n",
"           min_samples_split=2, min_weight_fraction_leaf=0.0,\n",
"           n_estimators=300, n_jobs=1, oob_score=False, random_state=0,\n",
"           verbose=0, warm_start=False)"
]
},
"execution_count": 3,
"output_type": "execute_result"
}
],
"source": [
"# Fitting Random Forest Regression to the dataset\n",
"regressor = RandomForestRegressor(n_estimators = 300, random_state = 0)\n",
"regressor.fit(X, y)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"collapsed": true
},
"outputs": [],
"source": [
"# Predicting a new result\n",
"y_pred = regressor.predict(6.5)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 160333.33333333]\n"
]
}
],
"source": [
"print(y_pred)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.figure.Figure at 0xa760ed0>"
]
},
"output_type": "display_data"
}
],
"source": [
"# Visualising the Random Forest Regression results (higher resolution)\n",
"X_grid = np.arange(min(X), max(X), 0.01)\n",
"X_grid = X_grid.reshape((len(X_grid), 1))\n",
"plt.scatter(X, y, color = 'red')\n",
"plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')\n",
"plt.title('Truth or Bluff (Random Forest Regression)')\n",
"plt.xlabel('Position level')\n",
"plt.ylabel('Salary')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"collapsed": true
},
"outputs": [],
"source": []
}
],
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
```
##### 关于这个算法
``````# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.ensemble import RandomForestRegressor``````
``````# Importing the dataset
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values``````
``````# Fitting Random Forest Regression to the dataset
regressor = RandomForestRegressor(n_estimators = 300, random_state = 0)
regressor.fit(X, y)``````
```RandomForestRegressor(bootstrap=True, criterion=&#x27;mse&#x27;, max_depth=None,
max_features=&#x27;auto&#x27;, max_leaf_nodes=None,
min_impurity_split=1e-07, min_samples_leaf=1,
min_samples_split=2, min_weight_fraction_leaf=0.0,
n_estimators=300, n_jobs=1, oob_score=False, random_state=0,
verbose=0, warm_start=False)```
``````# Predicting a new result
y_pred = regressor.predict(6.5)``````
``print(y_pred)``
```[ 160333.33333333]
```
``````# Visualising the Random Forest Regression results (higher resolution)
X_grid = np.arange(min(X), max(X), 0.01)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color = 'red')
plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')
plt.title('Truth or Bluff (Random Forest Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()``````