.
This commit is contained in:
commit
42a130e272
7 files changed
+9878
No files matched your search
Executable
+837
@@ -0,0 +1,837 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "7a9115cb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import pandas as pd\n",
|
||||
"import numpy as np\n",
|
||||
"import seaborn as sns; sns.set()\n",
|
||||
"from sklearn.model_selection import train_test_split"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "b58c3a40",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"heart_disease=pd.read_csv('./data/heart-disease.csv')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "0bc694fa",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"x = heart_disease.drop('target',axis=1)\n",
|
||||
"y = heart_disease['target']\n",
|
||||
"\n",
|
||||
"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "91e073a1",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.7868852459016393"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from sklearn.ensemble import RandomForestClassifier\n",
|
||||
"x = heart_disease.drop('target',axis=1)\n",
|
||||
"y = heart_disease['target']\n",
|
||||
"\n",
|
||||
"x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)\n",
|
||||
"\n",
|
||||
"clf = RandomForestClassifier()\n",
|
||||
"clf.fit(x_train, y_train)\n",
|
||||
"\n",
|
||||
"clf.score(x_test, y_test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "33eb1968",
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1,\n",
|
||||
" 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1,\n",
|
||||
" 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1], dtype=int64)"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"clf.predict(x_test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "5dc76fe7",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.7868852459016393"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"y_preds = clf.predict(x_test)\n",
|
||||
"np.mean(y_preds == y_test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "3e29898c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[0.16, 0.84],\n",
|
||||
" [0. , 1. ],\n",
|
||||
" [0.14, 0.86],\n",
|
||||
" [0.69, 0.31],\n",
|
||||
" [0.21, 0.79]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"clf.predict_proba(x_test[:5])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "8f29756f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([1, 1, 1, 0, 1], dtype=int64)"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"clf.predict(x_test[:5])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"id": "58ecfe8a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from sklearn.ensemble import RandomForestRegressor\n",
|
||||
"from sklearn.datasets import fetch_california_housing\n",
|
||||
"np.random.seed(42)\n",
|
||||
"housing = fetch_california_housing()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"id": "969a0902",
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'data': array([[ 8.3252 , 41. , 6.98412698, ..., 2.55555556,\n",
|
||||
" 37.88 , -122.23 ],\n",
|
||||
" [ 8.3014 , 21. , 6.23813708, ..., 2.10984183,\n",
|
||||
" 37.86 , -122.22 ],\n",
|
||||
" [ 7.2574 , 52. , 8.28813559, ..., 2.80225989,\n",
|
||||
" 37.85 , -122.24 ],\n",
|
||||
" ...,\n",
|
||||
" [ 1.7 , 17. , 5.20554273, ..., 2.3256351 ,\n",
|
||||
" 39.43 , -121.22 ],\n",
|
||||
" [ 1.8672 , 18. , 5.32951289, ..., 2.12320917,\n",
|
||||
" 39.43 , -121.32 ],\n",
|
||||
" [ 2.3886 , 16. , 5.25471698, ..., 2.61698113,\n",
|
||||
" 39.37 , -121.24 ]]),\n",
|
||||
" 'target': array([4.526, 3.585, 3.521, ..., 0.923, 0.847, 0.894]),\n",
|
||||
" 'frame': None,\n",
|
||||
" 'target_names': ['MedHouseVal'],\n",
|
||||
" 'feature_names': ['MedInc',\n",
|
||||
" 'HouseAge',\n",
|
||||
" 'AveRooms',\n",
|
||||
" 'AveBedrms',\n",
|
||||
" 'Population',\n",
|
||||
" 'AveOccup',\n",
|
||||
" 'Latitude',\n",
|
||||
" 'Longitude'],\n",
|
||||
" 'DESCR': '.. _california_housing_dataset:\\n\\nCalifornia Housing dataset\\n--------------------------\\n\\n**Data Set Characteristics:**\\n\\n :Number of Instances: 20640\\n\\n :Number of Attributes: 8 numeric, predictive attributes and the target\\n\\n :Attribute Information:\\n - MedInc median income in block group\\n - HouseAge median house age in block group\\n - AveRooms average number of rooms per household\\n - AveBedrms average number of bedrooms per household\\n - Population block group population\\n - AveOccup average number of household members\\n - Latitude block group latitude\\n - Longitude block group longitude\\n\\n :Missing Attribute Values: None\\n\\nThis dataset was obtained from the StatLib repository.\\nhttps://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.html\\n\\nThe target variable is the median house value for California districts,\\nexpressed in hundreds of thousands of dollars ($100,000).\\n\\nThis dataset was derived from the 1990 U.S. census, using one row per census\\nblock group. A block group is the smallest geographical unit for which the U.S.\\nCensus Bureau publishes sample data (a block group typically has a population\\nof 600 to 3,000 people).\\n\\nAn household is a group of people residing within a home. Since the average\\nnumber of rooms and bedrooms in this dataset are provided per household, these\\ncolumns may take surpinsingly large values for block groups with few households\\nand many empty houses, such as vacation resorts.\\n\\nIt can be downloaded/loaded using the\\n:func:`sklearn.datasets.fetch_california_housing` function.\\n\\n.. topic:: References\\n\\n - Pace, R. Kelley and Ronald Barry, Sparse Spatial Autoregressions,\\n Statistics and Probability Letters, 33 (1997) 291-297\\n'}"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"housing"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"id": "ba187d99",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"housing_df = pd.DataFrame(housing['data'],\n",
|
||||
" columns=housing['feature_names'])\n",
|
||||
"housing_df['target']=housing['target']\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"id": "f47503b8",
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>MedInc</th>\n",
|
||||
" <th>HouseAge</th>\n",
|
||||
" <th>AveRooms</th>\n",
|
||||
" <th>AveBedrms</th>\n",
|
||||
" <th>Population</th>\n",
|
||||
" <th>AveOccup</th>\n",
|
||||
" <th>Latitude</th>\n",
|
||||
" <th>Longitude</th>\n",
|
||||
" <th>target</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>8.3252</td>\n",
|
||||
" <td>41.0</td>\n",
|
||||
" <td>6.984127</td>\n",
|
||||
" <td>1.023810</td>\n",
|
||||
" <td>322.0</td>\n",
|
||||
" <td>2.555556</td>\n",
|
||||
" <td>37.88</td>\n",
|
||||
" <td>-122.23</td>\n",
|
||||
" <td>4.526</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>8.3014</td>\n",
|
||||
" <td>21.0</td>\n",
|
||||
" <td>6.238137</td>\n",
|
||||
" <td>0.971880</td>\n",
|
||||
" <td>2401.0</td>\n",
|
||||
" <td>2.109842</td>\n",
|
||||
" <td>37.86</td>\n",
|
||||
" <td>-122.22</td>\n",
|
||||
" <td>3.585</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>7.2574</td>\n",
|
||||
" <td>52.0</td>\n",
|
||||
" <td>8.288136</td>\n",
|
||||
" <td>1.073446</td>\n",
|
||||
" <td>496.0</td>\n",
|
||||
" <td>2.802260</td>\n",
|
||||
" <td>37.85</td>\n",
|
||||
" <td>-122.24</td>\n",
|
||||
" <td>3.521</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>5.6431</td>\n",
|
||||
" <td>52.0</td>\n",
|
||||
" <td>5.817352</td>\n",
|
||||
" <td>1.073059</td>\n",
|
||||
" <td>558.0</td>\n",
|
||||
" <td>2.547945</td>\n",
|
||||
" <td>37.85</td>\n",
|
||||
" <td>-122.25</td>\n",
|
||||
" <td>3.413</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>3.8462</td>\n",
|
||||
" <td>52.0</td>\n",
|
||||
" <td>6.281853</td>\n",
|
||||
" <td>1.081081</td>\n",
|
||||
" <td>565.0</td>\n",
|
||||
" <td>2.181467</td>\n",
|
||||
" <td>37.85</td>\n",
|
||||
" <td>-122.25</td>\n",
|
||||
" <td>3.422</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" MedInc HouseAge AveRooms AveBedrms Population AveOccup Latitude \\\n",
|
||||
"0 8.3252 41.0 6.984127 1.023810 322.0 2.555556 37.88 \n",
|
||||
"1 8.3014 21.0 6.238137 0.971880 2401.0 2.109842 37.86 \n",
|
||||
"2 7.2574 52.0 8.288136 1.073446 496.0 2.802260 37.85 \n",
|
||||
"3 5.6431 52.0 5.817352 1.073059 558.0 2.547945 37.85 \n",
|
||||
"4 3.8462 52.0 6.281853 1.081081 565.0 2.181467 37.85 \n",
|
||||
"\n",
|
||||
" Longitude target \n",
|
||||
"0 -122.23 4.526 \n",
|
||||
"1 -122.22 3.585 \n",
|
||||
"2 -122.24 3.521 \n",
|
||||
"3 -122.25 3.413 \n",
|
||||
"4 -122.25 3.422 "
|
||||
]
|
||||
},
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"housing_df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"id": "4346e2e7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
"\n",
|
||||
"X = housing_df.drop('target', axis=1)\n",
|
||||
"y = housing['target']\n",
|
||||
"\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 37,
|
||||
"id": "cd0d7834",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = RandomForestRegressor(n_estimators=1000)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 38,
|
||||
"id": "0e65ca48",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"RandomForestRegressor(n_estimators=1000)"
|
||||
]
|
||||
},
|
||||
"execution_count": 38,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model.fit(X_train, y_train)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 39,
|
||||
"id": "394a8a63",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.8077999156142389"
|
||||
]
|
||||
},
|
||||
"execution_count": 39,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model.score(X_test,y_test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 41,
|
||||
"id": "2c4bb4b8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"y_preds = model.predict(X_test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 42,
|
||||
"id": "ff01087a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([0.659563 , 2.69825401, 4.99800197, ..., 0.84376895, 4.72698433,\n",
|
||||
" 2.33940125])"
|
||||
]
|
||||
},
|
||||
"execution_count": 42,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"y_preds"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 44,
|
||||
"id": "7516e56f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([0.696 , 3.356 , 5.00001, 3.606 , 2.766 , 0.835 , 1.22 ,\n",
|
||||
" 1.693 , 3.153 , 0.789 ])"
|
||||
]
|
||||
},
|
||||
"execution_count": 44,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"np.array(y_test[:10])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 57,
|
||||
"id": "aab2f50d",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"RandomForestRegressor(n_estimators=1000)"
|
||||
]
|
||||
},
|
||||
"execution_count": 57,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from sklearn.ensemble import RandomForestClassifier\n",
|
||||
"\n",
|
||||
"X = heart_disease.drop('target', axis=1)\n",
|
||||
"y = heart_disease['target']\n",
|
||||
"\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
|
||||
"\n",
|
||||
"model = RandomForestClassifier(n_estimators=1000)\n",
|
||||
"model.fit(X_train, y_train)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 59,
|
||||
"id": "8617403d",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.4126102400932402"
|
||||
]
|
||||
},
|
||||
"execution_count": 59,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model.score(X_test, y_test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 88,
|
||||
"id": "37f1f469",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"RandomForestRegressor(n_estimators=50)"
|
||||
]
|
||||
},
|
||||
"execution_count": 88,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from sklearn.ensemble import RandomForestRegressor\n",
|
||||
"\n",
|
||||
"X = heart_disease.drop('target', axis=1)\n",
|
||||
"y = heart_disease['target']\n",
|
||||
"\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
|
||||
"\n",
|
||||
"model_reg = RandomForestRegressor(n_estimators=50)\n",
|
||||
"model_reg.fit(X_train, y_train)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 89,
|
||||
"id": "00bd7beb",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.1977237472766885"
|
||||
]
|
||||
},
|
||||
"execution_count": 89,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model_reg.score(X_test,y_test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 90,
|
||||
"id": "43d63da0",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.2423773420479305"
|
||||
]
|
||||
},
|
||||
"execution_count": 90,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"maximum = 0\n",
|
||||
"for i in range(10):\n",
|
||||
" model_reg.fit(X_train, y_train)\n",
|
||||
" if maximum < model_reg.score(X_test,y_test):\n",
|
||||
" maximum = model_reg.score(X_test,y_test)\n",
|
||||
"maximum"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 96,
|
||||
"id": "a43b1703",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([0.83606557, 0.90163934, 0.78688525, 0.8 , 0.76666667])"
|
||||
]
|
||||
},
|
||||
"execution_count": 96,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from sklearn.model_selection import cross_val_score\n",
|
||||
"from sklearn.ensemble import RandomForestClassifier\n",
|
||||
"\n",
|
||||
"X = heart_disease.drop('target', axis=1)\n",
|
||||
"y = heart_disease['target']\n",
|
||||
"\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
|
||||
"\n",
|
||||
"model = RandomForestClassifier(n_estimators=1000)\n",
|
||||
"model.fit(X_train, y_train)\n",
|
||||
"cross_val_score(model, X, y)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 94,
|
||||
"id": "3801871c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.819672131147541"
|
||||
]
|
||||
},
|
||||
"execution_count": 94,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model.score(X_test, y_test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 97,
|
||||
"id": "9e096485",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.8316129032258065"
|
||||
]
|
||||
},
|
||||
"execution_count": 97,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"np.mean(cross_val_score(clf, X, y, cv=10))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 98,
|
||||
"id": "4a3038da",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([0.81967213, 0.8852459 , 0.80327869, 0.8 , 0.8 ])"
|
||||
]
|
||||
},
|
||||
"execution_count": 98,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"cross_val_score(clf, X, y, scoring=None )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 102,
|
||||
"id": "e1098ca4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from sklearn.metrics import roc_curve\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
|
||||
"\n",
|
||||
"y_probs = clf.predict_proba(X_test)\n",
|
||||
"y_probs_positive = y_probs[:,1]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 104,
|
||||
"id": "ba39424f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fpr, tpr, thresholds =roc_curve(y_test, y_probs_positive)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 107,
|
||||
"id": "1a5d135c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([0. , 0. , 0. , 0. , 0. ,\n",
|
||||
" 0.03571429, 0.03571429, 0.03571429, 0.03571429, 0.03571429,\n",
|
||||
" 0.03571429, 0.03571429, 0.07142857, 0.07142857, 0.32142857,\n",
|
||||
" 0.39285714, 0.71428571, 1. ])"
|
||||
]
|
||||
},
|
||||
"execution_count": 107,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fpr"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 108,
|
||||
"id": "88352c7e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def plot_roc_curve(fpr, tpr):\n",
|
||||
" plt.plot(fpr, tpr, c='orange', label='ROC')\n",
|
||||
" plt.plot([0,1],[0,1],c='darkblue', label='Guessing')\n",
|
||||
" \n",
|
||||
" plt.xlabel('FPR')\n",
|
||||
" plt.ylabel('TPR')\n",
|
||||
" plt.title('ROC curve')\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 109,
|
||||
"id": "eaf11c67",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEXCAYAAACtTzM+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAvi0lEQVR4nO3deXxM9/7H8VeCbIIQk6RVtVTFFntrKXJpiSIU1fXSxdXq8nMNgiax1ZZaGpfeLtEqLmqpvb1FS8USai+1FKW1lGQiloTsc35/1M29qSVBJpPMvJ+PRx+PnJwz5v2VmnfOzJzPuBiGYSAiIk7P1d4BRESkaFAhiIgIoEIQEZHrVAgiIgKoEERE5DoVgoiIAFDS3gFEbC0wMJCaNWvi6uqKi4sLqampeHt7M3r0aIKCggC4du0aM2bMYMOGDbi5uQHQrl073njjDTw8PHL+rOXLl7Nw4ULS0tLIzMykSZMmhIWFUbZsWbusTaQgueg6BHF0gYGBbNu2jQoVKuR877PPPmPdunUsWrSIrKwsXnzxRRo2bMjAgQPx9PQkNTWVqVOncvjwYebMmUPJkiX5+OOP2bRpE9OnT6dixYpkZmYyYcIEfv75ZxYsWGDHFYoUDJ0hiNPJysri3LlzlCtXDoA1a9ZgtVp55513co7x9PQkIiKCp556im+//Zbg4GA++eQTli9fTsWKFQEoVaoUQ4cO5dtvvyUjIyPnzOI/vv/+e6ZNm4bVasXLy4sxY8bg7e1NaGgoe/fuBeDMmTM528uWLePLL7/MOYPJzMzklVdeISQkBIDJkycDEBYWxpIlS/jiiy+wWq34+PgwYsQIHnroIZv/3YljUyGIU3jppZcAuHjxIu7u7rRt25aJEycCsHfvXpo2bXrDbVxcXGjRogW7d++mcuXKeHh4ULVq1VzHeHp60rVr1xtum5iYSFhYGHPnzqVOnTqsW7eOKVOmMHr06NvmPH78OBs2bMDb25ulS5eybNkyQkJCyM7OZtWqVfzrX/9ix44drFixgvnz5+Pp6cmWLVt4++23+eabb+7uL0fkOhWCOIU5c+ZQoUIFDh48yGuvvUazZs3w9fXN2Z+VlXXT22VkZFCiRAlcXV2xWq35vr89e/bw8MMPU6dOHQA6dOhAhw4dOHPmzG1vFxgYiLe3NwCdOnVi0qRJWCwWDh06RNWqValatSqLFy/mt99+47nnnsu53ZUrV7h06RI+Pj75zijyZ3qXkTiVunXr8s477zB8+PCcB+fGjRuza9euGx7wrVYrO3fupFGjRtSoUYOsrCx+/fXXXMekp6fTr18/4uPjc32/RIkSuLi45GwbhsGRI0dwcXHhf1+2y8zMzHU7Ly+vnK89PT0JCQnhq6++YunSpfTq1SsnV7du3Vi5ciUrV65k+fLlLF26NOcpMJG7pUIQp9OlSxfq16+f85RRSEgInp6eTJgwgbS0NADS0tIYO3YspUuXpn379ri5udGvXz8iIiJITEwE/jh7mDBhAqmpqfj7++e6jwYNGvDLL79w7NgxANavX5/zbqTMzEyOHz8OwNdff33brM888wzLly9nz549Oa8ltGrViq+//pqEhAQAvvjii5ynxETuhZ4yEqc0YsQIunbtyubNm2ndujWzZs3iww8/pEePHri6upKdnU27du2YNWsWpUqVAqB///54enrSt29f4I+zg0cffZQPP/zwhj+/YsWKTJkyhWHDhpGdnY23tzfR0dGUKVOGsLAw+vXrR4UKFejYseNtc9arV48SJUrQsWNH3N3dgT8KoV+/frz66qu4uLjg7e3NBx98kOuMRORu6G2nIiIC6CkjERG5ToUgIiKACkFERK5TIYiICKBCEBGR61QIIiICFPPrEC5evIrVeufvmvX19ebChRQbJCq6tGbnoDU7h7tds6urC+XLl77l/mJdCFarcVeF8J/bOhut2Tlozc7BFmvWU0YiIgKoEERE5DoVgoiIADYuhJSUFLp06XLTGfCHDx+mR48ehISEEBERcct59CIiUjhsVgg//vgjzz///A3z4/8jLCyMkSNHsnbtWgzDYPHixbaKIiIi+WCzdxktXryYUaNGMXTo0Bv2nT17lrS0NBo2bAhAjx49mD59Oi+88IKt4hQOI/+fqFXoDGvRzm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1Jrly0OpXr2cvaMVWzZ7DSEhIQGTyZSz7efnx/79+3MdM3z4cF599VUmTJiAp6cnixcvvqP78PX1vut8JlM+Blf96gauLvk7thhwlHXcCa3ZMR06lEjfvmvZvv0cnTpV4+OP21O5cll7xypUtvg526wQrFZrrgFRhmHk2k5LSyMiIoLZs2dTv359Pv/8c4YNG0ZMTEy+7+PChRSsVuOOs5lMZbBYkvM8zjs1A3erwYV8HFvU5XfNjkRrdjwZGdnMmLGP6Og9eHu78eGH7ejfvxGJiSkOve4/u9ufs6ury21/kbbZU0YBAQFYLJacbYvFgp+fX8720aNHcXd3p379+gA8++yz7Nixw1ZxRKSY27fPQocOy3jvvV107lyNzZuf4emnnXcyqS3YrBBatmzJtm3bSEpKIjU1lXXr1tGmTZuc/VWqVOH8+fOcOHECgPXr1xMUFGSrOCJSTKWmZjFmzHY6dlxOUlIac+eG8MknT2Ayedo7msOx2VNG/v7+mM1m+vTpQ2ZmJk8//TT169enX79+DBgwgKCgICZOnMjAgQMxDANfX18mTJhgqzgiUgzFxf2O2RzLyZNX6N27FiNHNqdcOQ2jsxWbXpgWGhpKaGhoru/NnDkz5+vg4GCCg4NtGUFEiqHk5AzeffcH5sw5RJUqZVm6tAuLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 432x288 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"plot_roc_curve(fpr,tpr)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 110,
|
||||
"id": "8cae5ee2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.9751082251082251"
|
||||
]
|
||||
},
|
||||
"execution_count": 110,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from sklearn.metrics import roc_auc_score\n",
|
||||
"\n",
|
||||
"roc_auc_score(y_test, y_probs_positive)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.13.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Executable
+1559
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user