<jupyter_start><jupyter_text>Build a classifier: Predict species<jupyter_code>#Import libraries
import pandas as pd
import numpy as np
import seaborn as sns
from pathlib import Path
import matplotlib.pyplot as plt
%matplotlib inline
#Set data path
iris_data_path = Path('../data/iris.csv')
#Load dataset
df = pd.read_csv(iris_data_path)
df.head()<jupyter_output><empty_output><jupyter_text>Preprocessing<jupyter_code>#Rename columns
def rename_columns(dataf):
    
    renamed_df = (dataf
                  .rename(columns={'SepalLengthCm': 'sepal_length',
                                   'SepalWidthCm' : 'sepal_width',
                                   'PetalLengthCm': 'petal_length',
                                   'PetalWidthCm' : 'petal_width',
                                   'Species' : 'species'}
                         )
                 )
    
    return renamed_df

def keep_columns(dataf):
    
    keep_columns_df = (dataf[['sepal_length', 
                              'sepal_width',
                              'petal_width',
                              'petal_length',
                              'species']]
                      )
        
    return keep_columns_df
clean_df = (df
            .pipe(rename_columns)
            .pipe(keep_columns)
           )
clean_df.head()<jupyter_output><empty_output><jupyter_text>Data exploration<jupyter_code>clean_df.info()
clean_df.describe()
clean_df.isna().sum()
clean_df['species'].value_counts()
sns.set_style("dark")
sns.pairplot(clean_df, hue='species');
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16,8))

sns.scatterplot(x='sepal_length', y='sepal_width', data=clean_df, hue='species', ax=ax1)
sns.scatterplot(x='petal_length', y='petal_width', data=clean_df, hue='species', ax=ax2);
fig, ((ax1, ax2) , (ax3, ax4)) = plt.subplots(2, 2, figsize=(18,12))

sns.violinplot(x='species', y='sepal_length', data=clean_df, hue='species', ax=ax1)
sns.violinplot(x='species', y='sepal_width', data=clean_df, hue='species', ax=ax2)
sns.violinplot(x='species', y='petal_length', data=clean_df, hue='species', ax=ax3)
sns.violinplot(x='species', y='petal_width', data=clean_df, hue='species', ax=ax4);
fig = plt.figure(figsize=(12,6))
ax = fig.add_axes([0,0,1,1])

sns.heatmap(clean_df.corr(),annot=True,cmap='coolwarm', ax=ax);<jupyter_output><empty_output><jupyter_text>Simple classifierBased on mean petal width value<jupyter_code>import math
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
#Function to create dataframe with numeric predictor
#iris-setosa = 0, iris-versicolor=1, iris-verginica=2
def classifier_dataf(dataf):
    
    encode_cat = LabelEncoder()
    
    classifier_df = (dataf
                     .assign(species= encode_cat.fit_transform(dataf['species']))
                    )
    
    return classifier_df
#Create dataframe with species transformed to numeric
classifier_df = clean_df.pipe(classifier_dataf)
#Determine features and predictor 
X = classifier_df[['petal_width']]
y = classifier_df['species']

#Split dataframe in 
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
#Use petal_width to determine species
def prediction_petal(X_test, X_train):

    predictions = X_test['petal_width'].apply(lambda x: 0 if (x < math.floor(X_train.mean())) else 
                                                       (2 if (x > math.ceil(X_train.mean())) else
                                                        1
                                                        )
                                             )
    return predictions
#Fit on training data and predict on test
y_pred = prediction_petal(X_test, X_train)
# Test accuracy Score
print(accuracy_score(y_test, y_pred))<jupyter_output>0.868421052631579<jupyter_text>Random forest classifier<jupyter_code>from sklearn.ensemble import RandomForestClassifier
#Create classifier instance
rfc = RandomForestClassifier()

#Fit the classifier
rfc.fit(X_train, y_train)
    
#Make predictions 
y_pred = rfc.predict(X_test)
# Test accuracy Score
print(accuracy_score(y_test, y_pred))<jupyter_output>1.0
{'fraction_non_alphanumeric': 0.11373954599761052, 'fraction_numerical': 0.013381123058542414, 'mean_word_length': 2.9305164319248824, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '382654', 'license': 'MIT', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1355, 'n_words': 258, 'path': 'notebooks/iris.ipynb', 'repo_name': 'casperdamen123/python_projects_DiP', 'size': '4185'}