mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-01 00:41:09 +00:00
148 lines
16 KiB
Plaintext
148 lines
16 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {
|
||
|
"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,
|
||
|
"metadata": {
|
||
|
"collapsed": true
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Importing the dataset\n",
|
||
|
"dataset = pd.read_csv('Position_Salaries.csv')\n",
|
||
|
"X = dataset.iloc[:, 1:2].values\n",
|
||
|
"y = dataset.iloc[:, 2].values"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {},
|
||
|
"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,
|
||
|
"metadata": {},
|
||
|
"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,
|
||
|
"metadata": {
|
||
|
"collapsed": true
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Predicting a new result\n",
|
||
|
"y_pred = regressor.predict(6.5)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"[ 160333.33333333]\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(y_pred)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAaEAAAEWCAYAAADPZygPAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XmcXFWd9/HPNx1ICAgJkGHJziSKcUGgBwPMuABCADE4\nIuBEySCYUWEE0UeB+MgaxEEFHBl4MgGBsU1YlYisg7KNsiSIQECGGMiCBALZIB2SdOf3/HFPm0pT\nvVSlum5X6vt+vepVt85dzu/e6q5fnXtPnauIwMzMLA998g7AzMzql5OQmZnlxknIzMxy4yRkZma5\ncRIyM7PcOAmZmVlunISsS5JGS+o1ffklHSLppRKWP1XSa5LekrSDpH+QNC+9/mQH61wi6dSKBV0C\nST+TdG4edVvlSZou6ewKbOfTkpoqEVNv4iRU49IHadtjg6Q1Ba8nlrnNxZI+VuFQS6n/QknrC/bj\nWUlHl7mt/sAPgI9HxHYRsRK4ELg0vb69yDq7Ap8DpqfXh6Rj+5akNyX9SdIJ5e9h7yDpZEmt7f6G\nLqtyDJ0mXEl9JYWk1Sm+xekLQs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GLVxYWnkF5NYxIbVgenT01HLriIhpEdEYEY2DB3c56oSZ2ZZh+PDS\nyiug2kno1bZTYOn5tVT+Mpve+31oKuusfGiR8nLqMDMzgKlTYcCATcsGDMjKe0i1k9AsoK2H2yTg\ntoLyE1IPtnHAynRK7W7gUEmDUoeEQ4G707xVksalXnEntNtWKXWYmRnAxIkwbRqMGAFS9jxtWlbe\nQ3psAFNJM4CPATtLWkzWy+1i4EZJJwELgGPT4ncARwDzgGbgRICIWCbpAuDxtNz5EdHW2eGrZD3w\ntgHuTA9KrcPMzApMnNi
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0xa760ed0>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"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,
|
||
|
"metadata": {
|
||
|
"collapsed": true
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"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
|
||
|
}
|