<jupyter_start><jupyter_text>AutoML library to get good regressor for the dataset    Library can handle following:       1. Deal with missing values in data-set       2. Scaling for numerical values       3. Encoding for categorical values       4. Feature selection through F-test (ANOVA)    The library can give a tuned model by evaluating following regressors:       1. Linear Regression       2. Random Forest Regression       3. XG Boost Regression       4. Elastic Net Regression    Cross-validation is used to prevent over-fitting on training set    Scoring function to select model is Root Mean Square Error<jupyter_code>## Importing necessary libraries

import numpy as np
import pandas as pd
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer, make_column_selector
from sklearn.feature_selection import SelectKBest, f_regression
from sklearn.model_selection import RandomizedSearchCV
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import ElasticNet
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
import matplotlib.pyplot as plt
## Class containing code to find the best model

class AutoML_Regressor:

    def fit(self, X, y):
        X_train = X
        y_train = y

        categories = []
        
        categorical_columns = X_train.select_dtypes(include = ['category','bool', 'object'])

        for i in range(categorical_columns.shape[1]):
            categories.append(list(categorical_columns.iloc[:,i].dropna().unique()))

        preprocessor = ColumnTransformer([
          ('numerical', Pipeline([('cleaner', SimpleImputer()), ('scaler', StandardScaler())]), make_column_selector(dtype_exclude=['object','category','bool'])),
          ('categorical', Pipeline([('cleaner', SimpleImputer(strategy = 'most_frequent')), ('encoder', OneHotEncoder(sparse = False, categories=categories))]), make_column_selector(dtype_include=['object','category','bool']))
        ])

        model_pipeline_step = []
        model_pipeline_step.append(('preprocessor',preprocessor))
        model_pipeline_step.append(('feature_selector',SelectKBest(f_regression,k='all')))
        model_pipeline_step.append(('estimator',LinearRegression()))
        model_pipeline = Pipeline(model_pipeline_step)

        features = preprocessor.fit_transform(X_train).shape[1]

        optimization_grid = []

        # Linear regression
        optimization_grid.append({
            'preprocessor__numerical__scaler':[RobustScaler(),StandardScaler(),MinMaxScaler()],
            'preprocessor__numerical__cleaner__strategy':['mean','median'],
            'feature_selector__k': list(np.arange(1,features,5)),
            'estimator':[LinearRegression()]})

        # Random Forest Regressor
        optimization_grid.append({
        'preprocessor__numerical__scaler':[None],
        'preprocessor__numerical__cleaner__strategy':['mean','median'],
        'feature_selector__k': list(np.arange(1,features,5)),
        'estimator':[RandomForestRegressor(random_state=0)],
        'estimator__n_estimators':np.arange(5,1000,20),
        'estimator__criterion':['gini','entropy'],
        'estimator__max_depth': [2, 4, 6, 8, 10],
        'estimator__min_samples_split': [2, 5, 10, 15, 100],
        'estimator__min_samples_leaf': [1, 2, 5, 10]})

        # XG Boost
        optimization_grid.append({
            'preprocessor__numerical__scaler':[None],
            'preprocessor__numerical__cleaner__strategy':['mean','median'],
            'feature_selector__k': list(np.arange(1,features,5)),
            'estimator':[GradientBoostingRegressor(random_state=0)],
            'estimator__n_estimators':np.arange(5,1000,20),
            'estimator__learning_rate':np.linspace(0.1,0.9,20),
            'estimator__subsample': [0.9, 0.5, 0.2, 0.1],
            'estimator__max_depth': [2, 4, 6, 8, 10]})
        
        # Elastic Net Regression
        optimization_grid.append({
            'preprocessor__numerical__scaler':[RobustScaler(),StandardScaler(),MinMaxScaler()],
            'preprocessor__numerical__cleaner__strategy':['mean','median'],
            'feature_selector__k': list(np.arange(1,features,5)),
            'estimator':[ElasticNet(random_state = 0)],
            'estimator__alpha': [1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 0.0, 1.0, 10.0, 100.0],
            'estimator__l1_ratio': np.arange(0, 1, 0.01)})

        search = RandomizedSearchCV(model_pipeline, optimization_grid, n_iter=250, scoring = 'neg_root_mean_squared_error', n_jobs = -1, random_state = 0, verbose = 3, cv = 10)

        search.fit(X_train, y_train)
        self.best_estimator_ = search.best_estimator_
        self.best_pipeline = search.best_params_
        self.optimization_grid = optimization_grid
        self.model_pipeline = model_pipeline

    def predict(self,X,y = None):
        return self.best_estimator_.predict(X)
## Example for choosing best regressor for the Boston House prices dataset
from sklearn import datasets
boston = datasets.load_boston()
boston2 = pd.DataFrame(boston['data'], columns = boston['feature_names'])
boston2['PRICE'] = boston['target']
boston2.head(10)
y = boston2['PRICE']
X = boston2.iloc[:, 0:len(boston2.columns) - 1]
# Train-test Split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Using the defined above to create a model
model = AutoML_Regressor()
# Fitting model on the training data-set
model.fit(X_train,y_train)
# Checking R2 score for best regressor
r2_score(y_test, model.predict(X_test))
# This is the best regressor as determined by the AutoML_Regressor library
model.best_pipeline
# Getting model predictions for plotting
predictions = model.predict(X_test)
# Storing actual test datset results in np array for plotting
actual = np.array(y_test)
# Plotting actual vs predicted prices to see if the resulting trend is linear
plt.plot(predictions, actual, 'o')
plt.xlabel('Predictions')
plt.ylabel('Actual')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11102197978501524, 'fraction_numerical': 0.020375421145515802, 'mean_word_length': 4.109836065573771, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '74549', 'license': 'MIT', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1901, 'n_words': 424, 'path': 'AutoML_Regressor.ipynb', 'repo_name': 'Sanirudh1993/AutoML_Regressor', 'size': '6233'}