#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Mar 5 12:57:32 2022 @author: tanu """ #%% # data, etc for now comes from my_data6.py and/or my_data5.py #%% try combinations #import sys, os #os.system("imports.py") def precision(y_true,y_pred): return precision_score(y_true,y_pred,pos_label = 1) def recall(y_true,y_pred): return recall_score(y_true, y_pred, pos_label = 1) def f1(y_true,y_pred): return f1_score(y_true, y_pred, pos_label = 1) #%% numerical_features_df.shape categorical_features_df.shape all_features_df.shape #%% target = target1 #target = target3 X_trainN, X_testN, y_trainN, y_testN = train_test_split(numerical_features_df, target, test_size = 0.33, random_state = 42) X_trainC, X_testC, y_trainC, y_testC = train_test_split(categorical_features_df, target, test_size = 0.33, random_state = 42) X_train, X_test, y_train, y_test = train_test_split(all_features_df, target, test_size = 0.33, random_state = 42) #%% #%% preprocessor = ColumnTransformer( transformers=[ ('num', MinMaxScaler() , numerical_features_names) ,('cat', OneHotEncoder(), categorical_features_names) ], remainder = 'passthrough') f = preprocessor.fit(numerical_features_df) f2 = preprocessor.transform(numerical_features_df) f3 = preprocessor.fit_transform(numerical_features_df) (f3==f2).all() preprocessor.fit_transform(numerical_features_df) #preprocessor.fit_transform(all_features_df) #%% model_log = Pipeline(steps = [ ('preprocess', preprocessor) #,('log_reg', linear_model.LogisticRegression()) ,('log_reg', LogisticRegression( class_weight = 'unbalanced')) ]) model = model_log #%% seed = 42 model_rf = Pipeline(steps = [ ('preprocess', preprocessor) ,('rf', RandomForestClassifier( min_samples_leaf=50, n_estimators=150, bootstrap=True, oob_score=True, n_jobs=-1, random_state=seed, max_features='auto')) ]) model = model_rf #%% model.fit(X_trainN, y_trainN) y_pred = model.predict(X_testN) y_pred acc = make_scorer(accuracy_score) prec = make_scorer(precision) rec = make_scorer(recall) f1 = make_scorer(f1) output = cross_validate(model, X_trainN, y_trainN , scoring = {'acc' : acc ,'prec': prec ,'rec' : rec ,'f1' : f1} , cv = 10 , return_train_score = False) pd.DataFrame(output).mean() #%% Run multiple models using MultClassPipeline # only good for numerical features as categ features is not supported yet! t1_res = MultClassPipeline2(X_trainN, X_testN, y_trainN, y_testN, input_df = all_features_df) t1_res #%% # https://machinelearningmastery.com/columntransformer-for-numerical-and-categorical-data/ #Each transformer is a three-element tuple that defines the name of the transformer, the transform to apply, and the column indices to apply it to. For example: # (Name, Object, Columns) # Determine categorical and numerical features numerical_ix = all_features_df.select_dtypes(include=['int64', 'float64']).columns numerical_ix categorical_ix = all_features_df.select_dtypes(include=['object', 'bool']).columns categorical_ix # Define the data preparation for the columns t = [('cat', OneHotEncoder(), categorical_ix) , ('num', MinMaxScaler(), numerical_ix)] col_transform = ColumnTransformer(transformers=t , remainder='passthrough') # create pipeline (unlike example above where the col transfer was a preprocess step and it was fit_transformed) pipeline = Pipeline(steps=[('prep', col_transform) , ('classifier', LogisticRegression())]) #%% Added this to the MultClassPipeline tN_res = MultClassPipeline(X_trainN, X_testN, y_trainN, y_testN) tN_res t2_res = MultClassPipeline2(X_train, X_test, y_train, y_test, input_df = all_features_df) t2_res t3_res = MultClassPipeSKF(input_df = numerical_features_df , y_targetF = target1 , var_type = 'numerical' , skf_splits = 10) t3_res t4_res = MultClassPipeSKF(input_df = all_features_df , y_targetF = target1 , var_type = 'mixed' , skf_splits = 10) t4_res