Skip to content

Commit

Permalink
add module 3 week 3 exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
dohuyduc2002 committed Sep 7, 2024
1 parent a5d77cf commit 095bff6
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions Module_3/Week3/exercise.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from sklearn import datasets\n",
"from sklearn.model_selection import train_test_split,GridSearchCV\n",
"from sklearn.metrics import accuracy_score,mean_squared_error\n",
"from sklearn.tree import DecisionTreeClassifier,DecisionTreeRegressor\n",
"from sklearn.datasets import fetch_openml"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sample Test: [1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0]\n",
"Accuracy score 1.0 \n"
]
}
],
"source": [
"iris_X, iris_y = datasets.load_iris(return_X_y=True)\n",
"\n",
"X_train,X_test,y_train,y_test = train_test_split(iris_X,iris_y,test_size=0.2,random_state=42)\n",
"\n",
"dt_obj = DecisionTreeClassifier()\n",
"params = {'criterion':['gini','entropy'],\n",
" 'splitter':['best','random'],\n",
" 'max_depth':range(5,10,1)}\n",
"dt_classifier = GridSearchCV(dt_obj,params)\n",
"\n",
"dt_classifier.fit(X_train,y_train)\n",
"dt_classifier.best_params_\n",
"\n",
"y_pred = dt_classifier.predict(X_test)\n",
"print(\"Sample Test:\", y_pred)\n",
"print(f\"Accuracy score {accuracy_score ( y_test , y_pred )} \")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/microwave/opt/anaconda3/lib/python3.9/site-packages/sklearn/datasets/_openml.py:320: UserWarning: Multiple active versions of the dataset matching the name machine_cpu exist. Versions may be fundamentally different, returning version 1. Available versions:\n",
"- version 1, status: active\n",
" url: https://www.openml.org/search?type=data&id=230\n",
"- version 2, status: active\n",
" url: https://www.openml.org/search?type=data&id=733\n",
"\n",
" warn(warning_msg)\n"
]
},
{
"data": {
"text/plain": [
"0.03333333333333333"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"machine_cpu = fetch_openml(name='machine_cpu')\n",
"machine_data = machine_cpu.data\n",
"machine_labels = machine_cpu.target\n",
"\n",
"X_train,X_test,y_train,y_test = train_test_split(iris_X,iris_y,test_size=0.2,random_state=42)\n",
"\n",
"params = {'criterion':['poisson', 'squared_error', 'friedman_mse', 'absolute_error'],\n",
" 'splitter':['best','random'],\n",
" 'max_depth':range(5,10,1)}\n",
"dt_obj = DecisionTreeRegressor()\n",
"dt_regressor = GridSearchCV(dt_obj,params)\n",
"\n",
"dt_regressor.fit(X_train,y_train)\n",
"dt_regressor.best_params_\n",
"\n",
"y_pred = dt_regressor.predict(X_test)\n",
"mean_squared_error(y_test,y_pred)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 095bff6

Please sign in to comment.