<jupyter_start><jupyter_text>Iris Model***<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>1. Load data into the python environment***<jupyter_code>data = pd.read_excel('./iris.xls')
data.head()
data.Classification.value_counts()<jupyter_output><empty_output><jupyter_text>From the above result it can be concluded that the above problem statement belongs to **"Multi-Class Classification Problem"**  2. Pre-processing***<jupyter_code># shape
data.shape
# info
data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 5 columns):
 #   Column          Non-Null Count  Dtype  
---  ------          --------------  -----  
 0   SL              150 non-null    float64
 1   SW              150 non-null    float64
 2   PL              150 non-null    float64
 3   PW              150 non-null    float64
 4   Classification  150 non-null    object 
dtypes: float64(4), object(1)
memory usage: 6.0+ KB<jupyter_text>There are 4 columns in iris dataset out of which the predictor column 'Classification' belongs to object type. Rest columns belongs to float<jupyter_code># check for null values
data.isna().sum()<jupyter_output><empty_output><jupyter_text>There are null values present in dataset<jupyter_code>for column in data.columns[:-1]:
    data[column].plot.hist()
    plt.show()<jupyter_output><empty_output><jupyter_text>From the histogram it is clear that columns in dataset are almost normally distributed, so null values can be replaced with mean<jupyter_code>data.describe().T
# Filling missing values
for column in data.columns[:-1]:
    data[column].fillna(round(data[column].mean(), 1), inplace= True)
data.isna().sum()<jupyter_output><empty_output><jupyter_text>All the null values have been removed<jupyter_code>data.describe().T<jupyter_output><empty_output><jupyter_text>Filling with mean didnot introduce observable change in dataset<jupyter_code># Label Encode 'Classification' Column
from sklearn.preprocessing import LabelEncoder
data['Classification'] = LabelEncoder().fit_transform(data['Classification'])
data.Classification.value_counts()<jupyter_output><empty_output><jupyter_text>3. Model Building***<jupyter_code># Function to check model performances
from sklearn.metrics import f1_score, accuracy_score, precision_score, recall_score, confusion_matrix
def check_model_metrices(y_test, y_pred):
    print('Model Accuracy = ', accuracy_score(y_test, y_pred))
    print('Model Precision = ', precision_score(y_test, y_pred, average='micro'))
    print('Model Recall = ', recall_score(y_test, y_pred, average='micro'))
    print('Model F1 Score = ', f1_score(y_test, y_pred, average='micro'))
    print('Confusion Matrix = \n', confusion_matrix(y_test, y_pred))
# feature selection
X = data[data.columns[:-1]] # features
y = data['Classification'] # target
print(f'Feature shape: {X.shape}')
# Spliting to training and testing sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
print(f'X_train shape: {X_train.shape}')
print(f'X_test shape: {X_test.shape}')<jupyter_output>X_train shape: (105, 4)
X_test shape: (45, 4)<jupyter_text>Model : Random Forest***<jupyter_code>from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier()
rf.fit(X_train, y_train)
rf_pred = rf.predict(X_test)
check_model_metrices(y_test, rf_pred)
import pickle
pickle_file = open('./iris-model.pkl', 'wb')
pickle.dump(rf, pickle_file)
pickle_file.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11864872287833013, 'fraction_numerical': 0.017577588574567427, 'mean_word_length': 5.480427046263345, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '506873', 'license': 'MIT', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1192, 'n_words': 295, 'path': 'projects/IrisPredictor-WebApp/iris-model.ipynb', 'repo_name': 'sreeh4ri/ictk_projects', 'size': '3641'}