{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Import Libraries" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "import pandas as pd \n", "import numpy as np \n", "import os\n", "import pikle\n", "from sklearn.preprocessing import MinMaxScaler\n", "from sklearn.ensemble import RandomForestClassifier\n", "from sklearn.metrics import accuracy_score, classification_report\n", "from sklearn.model_selection import train_test_split " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Pre-Processing" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal.lengthsepal.widthpetal.lengthpetal.widthvariety
05.13.51.40.2Setosa
14.93.01.40.2Setosa
24.73.21.30.2Setosa
34.63.11.50.2Setosa
45.03.61.40.2Setosa
\n", "
" ], "text/plain": [ " sepal.length sepal.width petal.length petal.width variety\n", "0 5.1 3.5 1.4 0.2 Setosa\n", "1 4.9 3.0 1.4 0.2 Setosa\n", "2 4.7 3.2 1.3 0.2 Setosa\n", "3 4.6 3.1 1.5 0.2 Setosa\n", "4 5.0 3.6 1.4 0.2 Setosa" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('dataset/iris.csv') \n", "df.head() " ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.3 7.9\n", "2.0 4.4\n", "1.0 6.9\n", "0.1 2.5\n" ] } ], "source": [ "print(min(df['sepal.length']), max(df['sepal.length']))\n", "print(min(df['sepal.width']), max(df['sepal.width']))\n", "print(min(df['petal.length']), max(df['petal.length']))\n", "print(min(df['petal.width']), max(df['petal.width']))" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "### Independent and Dependent features\n", "X=df.iloc[:,:-1]\n", "y=df.iloc[:,-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Split and Normalize dataset" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "# splitting data into training and testing data with 30 % of data as testing data respectively \n", "\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 42) \n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "# Normalize train set\n", "scaler = MinMaxScaler()\n", "scaler.fit(X_train)\n", "X_train = scaler.transform(X_train)\n", "X_test = scaler.transform(X_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Model" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
RandomForestClassifier()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ], "text/plain": [ "RandomForestClassifier()" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# importing the random forest classifier model and training it on the dataset \n", "\n", "classifier = RandomForestClassifier() \n", "classifier.fit(X_train, y_train) \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Evaluation" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "# predicting on the test dataset \n", "y_pred = classifier.predict(X_test) " ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# finding out the accuracy \n", "score = accuracy_score(y_test, y_pred)\n", "score" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " precision recall f1-score support\n", "\n", " Setosa 1.00 1.00 1.00 19\n", " Versicolor 1.00 1.00 1.00 13\n", " Virginica 1.00 1.00 1.00 13\n", "\n", " accuracy 1.00 45\n", " macro avg 1.00 1.00 1.00 45\n", "weighted avg 1.00 1.00 1.00 45\n", "\n" ] } ], "source": [ "# show out classification report\n", "print(classification_report(y_test, y_pred))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Save to Joblib file" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "# Save Model\n", "with open(\"saved_models/rf_clf.pkl\", \"wb\") as model_file:\n", " pickle.dump(classifier, model_file) \n", "model_file.close()\n", "\n", "# Save Scaler\n", "with open(\"saved_models/scaler.pkl\", \"wb\") as scaler_file:\n", " pickle.dump(scaler, scaler_file) \n", "scaler_file.close()" ] } ], "metadata": { "kernelspec": { "display_name": "venv", "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.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }