<jupyter_start><jupyter_text>**We moved all the pre-processing including splitting>imputation>Standardization to the CV iterations**<jupyter_code>cols_to_norm=['gcs', 'urine', 'wbc', 'sodium',
       'temperature', 'respiratoryrate', 'heartrate', 'meanbp', 'creatinine',
       'ph', 'hematocrit', 'albumin', 'pao2', 'pco2', 'bun', 'glucose',
       'bilirubin', 'fio2', 'age', 'offset']

X=df.drop('destcopy', 1)
y=df['destcopy']
df_cols = list(X)     #fancy impute removes column names.
df.columns<jupyter_output><empty_output><jupyter_text>**XGB**<jupyter_code>%matplotlib inline
from sklearn.model_selection import KFold
from sklearn import preprocessing
from imblearn.over_sampling import SMOTE
from sklearn.metrics import f1_score
from imblearn.metrics import classification_report_imbalanced
# explicitly require this experimental feature

from yellowbrick.classifier import ROCAUC
from sklearn.linear_model import LogisticRegression
from numpy import loadtxt
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import io 


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


kf = KFold(n_splits=10)


for i in range (1,11):

    for fold, (train_index, test_index) in enumerate(kf.split(X), 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------------------------------------

    

    #------------------------------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()
        #X_train_oversampled, y_train_oversampled = sm.fit_sample(X_train, y_train)
        model = XGBClassifier(max_depth=8, gamma=0.063, colsample_bytree=0.71)
        model.fit(X_train, y_train)  
        y_pred = model.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("XGB_Unbalanced_{}_{}.pdf".format(i, fold), clear_figure=True) 
        print(f'For fold {fold}:')
        print(f'Accuracy: {model.score(X_test, y_test)}')
        f1=f1_score(y_test, y_pred, average='micro')
        print(f'f-score: {f1}')
        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("XGB-U-D.csv" , sep=',', encoding='utf-8', doublequote=False, index=False, mode="a", header=False)
        df.loc["2":"2","pre":"sup"].to_csv("XGB-U-H.csv" , sep=',', encoding='utf-8', doublequote=False, index=False, mode="a", header=False)
        df.loc["3":"3","pre":"sup"].to_csv("XGB-U-N.csv" , sep=',', encoding='utf-8', doublequote=False, index=False, mode="a", header=False)
        df.loc["4":"4","pre":"sup"].to_csv("XGB-U-R.csv" , sep=',', encoding='utf-8', doublequote=False, index=False, mode="a", header=False)
        df.iloc[6:7,:].to_csv("XGB-U-avg.csv" , sep=',', encoding='utf-8', doublequote=False, index=False, mode="a", header=False)
        from sklearn.metrics import confusion_matrix
        print(confusion_matrix(y_test, y_pred))

        #<jupyter_output>/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:42: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

/anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py:543: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:43: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-v[...]
{'fraction_non_alphanumeric': 0.17185604528912252, 'fraction_numerical': 0.010715729882733522, 'mean_word_length': 3.601860465116279, 'pattern_counts': {'":': 8, '<': 8, '<?xml version=': 0, '>': 10, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '427740', 'license': 'MIT', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1643, 'n_words': 351, 'path': 'No-adjustment/R2-XB-U.ipynb', 'repo_name': 'data-intelligence-for-health-lab/Discharge-Prediction_eICU-CRD', 'size': '4946'}