File size: 3,576 Bytes
c0575f2 51fc47b c0575f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | {
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: scikit-learn in /Users/owen/.pyenv/versions/3.10.11/lib/python3.10/site-packages (1.3.0)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in /Users/owen/.pyenv/versions/3.10.11/lib/python3.10/site-packages (from scikit-learn) (3.1.0)\n",
"Requirement already satisfied: joblib>=1.1.1 in /Users/owen/.pyenv/versions/3.10.11/lib/python3.10/site-packages (from scikit-learn) (1.3.1)\n",
"Requirement already satisfied: scipy>=1.5.0 in /Users/owen/.pyenv/versions/3.10.11/lib/python3.10/site-packages (from scikit-learn) (1.11.1)\n",
"Requirement already satisfied: numpy>=1.17.3 in /Users/owen/.pyenv/versions/3.10.11/lib/python3.10/site-packages (from scikit-learn) (1.23.5)\n",
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.0.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49m/Users/owen/.pyenv/versions/3.10.11/bin/python3.10 -m pip install --upgrade pip\u001b[0m\n",
"Note: you may need to restart the kernel to use updated packages.\n",
"sklearn.__version__='1.3.0'\n"
]
}
],
"source": [
"%pip install scikit-learn==1.3.1\n",
"import sklearn\n",
"print(f\"{sklearn.__version__=}\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "ljmtcrVxoxfO"
},
"outputs": [],
"source": [
"from sklearn.datasets import load_iris\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"from sklearn.metrics import accuracy_score\n",
"\n",
"# Load the Iris dataset\n",
"iris = load_iris()\n",
"X, y = iris.data, iris.target\n",
"\n",
"# Split the data into training and test sets\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\n",
"\n",
"# Initialize the classifier\n",
"classifier = RandomForestClassifier(n_estimators=100, random_state=42)\n",
"\n",
"# Train the classifier\n",
"classifier.fit(X_train, y_train)\n",
"\n",
"# Make predictions on the test set\n",
"predictions = classifier.predict(X_test)\n",
"\n",
"# Calculate the accuracy\n",
"accuracy = accuracy_score(y_test, predictions)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "NL58M19xo4PP"
},
"outputs": [
{
"data": {
"text/plain": [
"['model.joblib']"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from joblib import dump\n",
"dump(classifier, 'model.joblib')"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"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.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|