Train new sklearn model using Iris dataset
Browse filesTrain a new classifier on the Iris dataset to fix the garden tutorial.
- Train_Model.ipynb +131 -0
- model.joblib +3 -0
Train_Model.ipynb
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 3,
|
6 |
+
"id": "b0e43fdc-4787-4b95-ae75-6f73750c0e78",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [
|
9 |
+
{
|
10 |
+
"name": "stdout",
|
11 |
+
"output_type": "stream",
|
12 |
+
"text": [
|
13 |
+
"Defaulting to user installation because normal site-packages is not writeable\n",
|
14 |
+
"Requirement already satisfied: scikit-learn in /home/hayden/.local/lib/python3.10/site-packages (1.4.2)\n",
|
15 |
+
"Requirement already satisfied: scipy>=1.6.0 in /home/hayden/.local/lib/python3.10/site-packages (from scikit-learn) (1.13.0)\n",
|
16 |
+
"Requirement already satisfied: joblib>=1.2.0 in /home/hayden/.local/lib/python3.10/site-packages (from scikit-learn) (1.4.0)\n",
|
17 |
+
"Requirement already satisfied: threadpoolctl>=2.0.0 in /home/hayden/.local/lib/python3.10/site-packages (from scikit-learn) (3.4.0)\n",
|
18 |
+
"Requirement already satisfied: numpy>=1.19.5 in /home/hayden/.local/lib/python3.10/site-packages (from scikit-learn) (1.26.4)\n",
|
19 |
+
"Note: you may need to restart the kernel to use updated packages.\n",
|
20 |
+
"1.4.2\n"
|
21 |
+
]
|
22 |
+
}
|
23 |
+
],
|
24 |
+
"source": [
|
25 |
+
"%pip install scikit-learn\n",
|
26 |
+
"import sklearn\n"
|
27 |
+
]
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"cell_type": "code",
|
31 |
+
"execution_count": 5,
|
32 |
+
"id": "af4e3bc3-6fcc-46d8-b7cc-d2fed9a05fc1",
|
33 |
+
"metadata": {},
|
34 |
+
"outputs": [
|
35 |
+
{
|
36 |
+
"name": "stdout",
|
37 |
+
"output_type": "stream",
|
38 |
+
"text": [
|
39 |
+
"sklearn version: 1.4.2\n"
|
40 |
+
]
|
41 |
+
}
|
42 |
+
],
|
43 |
+
"source": [
|
44 |
+
"print(f\"sklearn version: {sklearn.__version__}\")"
|
45 |
+
]
|
46 |
+
},
|
47 |
+
{
|
48 |
+
"cell_type": "code",
|
49 |
+
"execution_count": 6,
|
50 |
+
"id": "ee4d871c-441c-4ee9-8af0-415047644335",
|
51 |
+
"metadata": {},
|
52 |
+
"outputs": [],
|
53 |
+
"source": [
|
54 |
+
"from sklearn.datasets import load_iris\n",
|
55 |
+
"from sklearn.model_selection import train_test_split\n",
|
56 |
+
"from sklearn.ensemble import RandomForestClassifier\n",
|
57 |
+
"from sklearn.metrics import accuracy_score\n",
|
58 |
+
"\n",
|
59 |
+
"# Load the Iris dataset\n",
|
60 |
+
"iris = load_iris()\n",
|
61 |
+
"X, y = iris.data, iris.target\n",
|
62 |
+
"\n",
|
63 |
+
"# Split the data into training and test sets\n",
|
64 |
+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\n",
|
65 |
+
"\n",
|
66 |
+
"# Initialize the classifier\n",
|
67 |
+
"classifier = RandomForestClassifier(n_estimators=100, random_state=42)\n",
|
68 |
+
"\n",
|
69 |
+
"# Train the classifier\n",
|
70 |
+
"classifier.fit(X_train, y_train)\n",
|
71 |
+
"\n",
|
72 |
+
"# Make predictions on the test set\n",
|
73 |
+
"predictions = classifier.predict(X_test)\n",
|
74 |
+
"\n",
|
75 |
+
"# Calculate the accuracy\n",
|
76 |
+
"accuracy = accuracy_score(y_test, predictions)"
|
77 |
+
]
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"cell_type": "code",
|
81 |
+
"execution_count": 7,
|
82 |
+
"id": "84cd4fc6-4e68-4c79-bfeb-777bce8e62e5",
|
83 |
+
"metadata": {},
|
84 |
+
"outputs": [
|
85 |
+
{
|
86 |
+
"data": {
|
87 |
+
"text/plain": [
|
88 |
+
"['model.joblib']"
|
89 |
+
]
|
90 |
+
},
|
91 |
+
"execution_count": 7,
|
92 |
+
"metadata": {},
|
93 |
+
"output_type": "execute_result"
|
94 |
+
}
|
95 |
+
],
|
96 |
+
"source": [
|
97 |
+
"from joblib import dump\n",
|
98 |
+
"dump(classifier, 'model.joblib')"
|
99 |
+
]
|
100 |
+
},
|
101 |
+
{
|
102 |
+
"cell_type": "code",
|
103 |
+
"execution_count": null,
|
104 |
+
"id": "022af2af-01fd-4de5-a056-0f41337c0c1a",
|
105 |
+
"metadata": {},
|
106 |
+
"outputs": [],
|
107 |
+
"source": []
|
108 |
+
}
|
109 |
+
],
|
110 |
+
"metadata": {
|
111 |
+
"kernelspec": {
|
112 |
+
"display_name": "Python 3 (ipykernel)",
|
113 |
+
"language": "python",
|
114 |
+
"name": "python3"
|
115 |
+
},
|
116 |
+
"language_info": {
|
117 |
+
"codemirror_mode": {
|
118 |
+
"name": "ipython",
|
119 |
+
"version": 3
|
120 |
+
},
|
121 |
+
"file_extension": ".py",
|
122 |
+
"mimetype": "text/x-python",
|
123 |
+
"name": "python",
|
124 |
+
"nbconvert_exporter": "python",
|
125 |
+
"pygments_lexer": "ipython3",
|
126 |
+
"version": "3.10.12"
|
127 |
+
}
|
128 |
+
},
|
129 |
+
"nbformat": 4,
|
130 |
+
"nbformat_minor": 5
|
131 |
+
}
|
model.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f7513aa7aa8d793ef8535d15ed998e1d6b5d79ff4af3d67df7f604214a9afaa7
|
3 |
+
size 183761
|