<jupyter_start><jupyter_text>**We moved all the pre-processing including splitting>imputation>Standardization to the CV iterations**<jupyter_code>df2.columns
from sklearn import svm
from sklearn.decomposition import TruncatedSVD
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline

from sklearn_hierarchical_classification.classifier import HierarchicalClassifier
from sklearn_hierarchical_classification.constants import ROOT
from sklearn_hierarchical_classification.metrics import h_fbeta_score, multi_labeled<jupyter_output><empty_output><jupyter_text>**Random Forest**<jupyter_code>class_hierarchy = {
        ROOT: ["1", "A"],
        "A": ["2", "B"],
        "B": ["3", "4"],
    }
!jupyter nbextension enable --py --sys-prefix widgetsnbextension
from collections import Counter
from sklearn.model_selection import KFold
from sklearn import preprocessing
from imblearn.over_sampling import SMOTE
from imblearn.over_sampling import SMOTENC
from sklearn.metrics import f1_score
from imblearn.metrics import classification_report_imbalanced
from fancyimpute import IterativeImputer
from yellowbrick.classifier import ROCAUC
from sklearn.linear_model import LogisticRegression
import numpy as np
import pandas as pd
from hyperopt import hp, tpe
from hyperopt.fmin import fmin
from sklearn.model_selection import cross_val_score, StratifiedKFold
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import make_scorer
import xgboost as xgb
import lightgbm as lgbm
from sklearn.model_selection import StratifiedKFold
from collections import Counter
import io 

classes=['Death','Home','Nursing Home','Rehabilitation']


kf = StratifiedKFold(n_splits=10)
y = y.astype(str)

for fold, (train_index, test_index) in enumerate(kf.split(X,y), 1):
    X_train = X.iloc[train_index]
    y_train = y.iloc[train_index]  # Based on your code, you might need a ravel call here, but I would look into how you're generating your y
    X_test = X.iloc[test_index]
    y_test = y.iloc[test_index]  # See comment on ravel and  y_train
    
    
#------------------------------IMPUTE Training Set------------------------------------
    
    # Use MICE to fill in each row's missing features
    X_train = pd.DataFrame(IterativeImputer(verbose=False, sample_posterior=True).fit_transform(X_train))
    X_train.columns = df_cols

#------------------------------IMPUTE Testing Set------------------------------------ 

    # Use MICE to fill in each row's missing features
    X_test = pd.DataFrame(IterativeImputer(verbose=False, sample_posterior=True).fit_transform(X_test))
    X_test.columns = df_cols
    
#------------------------------Standardize Testing Set------------------------------------
    
    std_scale = preprocessing.StandardScaler().fit(X_train[cols_to_norm])
    X_train[cols_to_norm] = std_scale.transform(X_train[cols_to_norm])
    X_test[cols_to_norm] = std_scale.transform(X_test[cols_to_norm])
#------------------------------------------------------------------------------------------

 # Hyperparameters are optimized using hyperopt

    #sm = SMOTE()
    
    #sm = SMOTENC(random_state=50, categorical_features=[1,2,3,22,23,24,25,26,27,28,29,30,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61, 62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81])
    #X_train_oversampled, y_train_oversampled = sm.fit_sample(X_train, y_train)
    print(sorted(Counter(y_train).items()))
    
    model = linear_model.LogisticRegression(class_weight='balanced') 
    clf = HierarchicalClassifier(
        base_estimator=model,
        class_hierarchy=class_hierarchy,
    )
    clf.fit(X_train, y_train)  
    y_pred = clf.predict(X_test)
    visualizer = ROCAUC(model, classes=classes)
    visualizer.fit(X_train, y_train)  # Fit the training data to the visualizer
    visualizer.score(X_test, y_test)  # Evaluate the model on the test data
    visualizer.poof("LR_Hierarchy_{}.pdf".format(fold), clear_figure=True) 
    print(f'For fold {fold}:')
    print(f'Accuracy: {clf.score(X_test, y_test)}')
    f1=f1_score(y_test, y_pred, average='micro')
    print(f'f-score: {f1}')
    print(sorted(Counter(y_test).items()))
    print(classification_report_imbalanced(y_test, y_pred))
    K= classification_report_imbalanced(y_test, y_pred)
    df = pd.read_fwf(io.StringIO(K))
    df.loc["1":"1","pre":"sup"].to_csv("LR-Hierarchy-D.csv" , sep=',', encoding='utf-8', doublequote=False, index=False, mode="a", header=False)
    df.loc["2":"2","pre":"sup"].to_csv("LR-Hierarchy-H.csv" , sep=',', encoding='utf-8', doublequote=False, index=False, mode="a", header=False)
    df.loc["3":"3","pre":"sup"].to_csv("LR-Hierarchy-N.csv" , sep=',', encoding='utf-8', doublequote=False, index=False, mode="a", header=False)
    df.loc["4":"4","pre":"sup"].to_csv("LR-Hierarchy-R.csv" , sep=',', encoding='utf-8', doublequote=False, index=False, mode="a", header=False)
    df.iloc[6:7,:].to_csv("LR-Hierarchy-avg.csv" , sep=',', encoding='utf-8', doublequote=False, index=False, mode="a", header=False)

        #
    
    #

    
    
X.shape
y.shape<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup