<jupyter_start><jupyter_text>Evaluation for Split Class- Evaluates the effect of preprocessy train test split function on model accuracy compared to sklearn train test split- Evaluates on 4 datasets    * iris    * boston    * breast_cancer    * diabetes- Using RandomForestClassifier() model on first 2 datasets- Using LinearRegression() model on other 2 datasets- Using r2_score of sklearn.metrics- Comparisons between sklearn and preprocessy based on accuracy and time for different test sizes have been indicated at the end<jupyter_code># To access preprocessy module. Required in .ipynb files
import os
import sys
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
    sys.path.append(module_path)
import numpy as np
import pandas as pd
import time

from sklearn.datasets import load_iris, load_boston, load_breast_cancer, load_diabetes
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, r2_score
from sklearn.model_selection import train_test_split

from preprocessy.resampling import Split
splits = [None, 0.2, 0.3]
def preprocessy_eval(X, y, split, model):
    X_train, X_test, y_train, y_test = Split().train_test_split(X, y, test_size=split)
    preprocessy_test_size = None

    if split:
        preprocessy_test_size = split
    else:
        preprocessy_test_size = 1 / np.sqrt(len(X.columns))
    
    model.fit(X_train, y_train)
    preds = model.predict(X_test)
    
    return preprocessy_test_size, preds, y_test
def sklearn_eval(X, y, split, model):
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=split, random_state=69
    )
    sklearn_test_size = None

    if split:
        sklearn_test_size = split
    else:
        sklearn_test_size = 0.25  # from sklearn docs
    
    model.fit(X_train, y_train)
    preds = model.predict(X_test)
    return sklearn_test_size, preds, y_test
def eval(X, y, dataset, model):
    print(f"Dataset - {dataset}\n")
    for split in splits:
        start = time.time()
        preprocessy_test_size, preprocessy_preds, preprocessy_y_test = preprocessy_eval(
            X, y, split, model
        )
        end = time.time()
        preprocessy_time = np.round(end - start,4)

        start = time.time()
        sklearn_test_size, sklearn_preds, sklearn_y_test = sklearn_eval(
            X, y, split, model
        )
        end = time.time()
        sklearn_time = np.round(end - start,4)
        
        preprocessy_accuracy =  np.round(
                                classification_report(
                                preprocessy_y_test, preprocessy_preds, output_dict=True
                                )["accuracy"],
                                4)
        sklearn_accuracy    =   np.round(classification_report(
                                sklearn_y_test, sklearn_preds, output_dict=True
                                )["accuracy"],
                                4)
        
        dt = {'Test_size': [preprocessy_test_size, sklearn_test_size],
            'Accuracy': [preprocessy_accuracy, sklearn_accuracy],
            'Time': [preprocessy_time, sklearn_time]
        }
        print(pd.DataFrame(dt,index=['Preprocessy','sklearn']))
        print()

def eval2(X, y, dataset, model):
    print(f"Dataset - {dataset}\n")
    for split in splits:
        start = time.time()
        preprocessy_test_size, preprocessy_preds, preprocessy_y_test = preprocessy_eval(
            X, y, split, model
        )
        end = time.time()
        preprocessy_time = np.round(end - start,4)

        start = time.time()
        sklearn_test_size, sklearn_preds, sklearn_y_test = sklearn_eval(
            X, y, split, model
        )
        end = time.time()
        sklearn_time = np.round(end - start,4)

        preprocessy_accuracy = np.round(r2_score(preprocessy_y_test, preprocessy_preds),4)
        sklearn_accuracy = np.round(r2_score(sklearn_y_test, sklearn_preds),4)
        
        dt = {'Test_size': [preprocessy_test_size, sklearn_test_size],
            'Accuracy': [preprocessy_accuracy, sklearn_accuracy],
            'Time': [preprocessy_time, sklearn_time]
        }
        print(pd.DataFrame(dt,index=['Preprocessy','sklearn']))
        print()
def evaluate_on_iris():
    model = RandomForestClassifier()
    X, y = load_iris(return_X_y=True, as_frame=True)
    eval(X, y, "iris", model)
def evaluate_on_breast_cancer():
    model = RandomForestClassifier()
    X, y = load_breast_cancer(return_X_y=True, as_frame=True)
    eval(X, y, "breast cancer", model)
def evaluate_on_diabetes():
    model = LinearRegression(fit_intercept=True, normalize=True, copy_X=True)
    X, y = load_diabetes(return_X_y=True, as_frame=True)
    eval2(X, y, "diabetes", model)
def evaluate_on_boston():
    model = LinearRegression(fit_intercept=True, normalize=True, copy_X=True)
    dataset = load_boston()
    X = pd.DataFrame(dataset.data, columns=dataset.feature_names)
    y = pd.Series(dataset.target, name="Target")
    eval2(X, y, "boston", model)
evaluate_on_iris()
evaluate_on_breast_cancer()
evaluate_on_diabetes()
evaluate_on_boston()<jupyter_output>Dataset - boston

             Test_size  Accuracy   Time
Preprocessy    0.27735    0.6891  0.009
sklearn        0.25000    0.6722  0.007

             Test_size  Accuracy    Time
Preprocessy        0.2    0.6752  0.0119
sklearn            0.2    0.6747  0.0060

             Test_size  Accuracy   Time
Preprocessy        0.3    0.6755  0.006
sklearn            0.3    0.6927  0.004
{'fraction_non_alphanumeric': 0.09050259345376498, 'fraction_numerical': 0.01860132355571454, 'mean_word_length': 2.7082228116710874, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '234678', 'license': 'MIT', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1823, 'n_words': 416, 'path': 'evaluations/evaluate_split.ipynb', 'repo_name': 'Priyesh1202/preprocessy', 'size': '5591'}