text_plain_output_1.png
from collections import Counter

import os

import pandas as pd

dataset_path = '/kaggle/input/diabetes-readmission-prediction-i43/'
df = pd.read_csv(os.path.join(dataset_path, 'train.csv'), na_values='?')

X = df.drop(['readmitted'], axis=1)
y = df['readmitted'].copy()

train_data = pd.concat([X_train, y_train], axis=1)

class patient_nbr_transformer:
    def transform(self, X, y=None):
        new_data = X[['encounter_id', 'patient_nbr']].copy(deep=True)
        self.history = pd.concat([self.history, new_data]).drop_duplicates('encounter_id').reset_index(drop=True)
        countDat = self.history['patient_nbr'].value_counts()
        X_copy = X.copy(deep=True)
        X_copy['Count'] = X_copy['patient_nbr'].apply(lambda x: countDat[x])
        history_copy = self.history.copy(deep=True)
        patient_nbr_array = history_copy['patient_nbr'].unique()
        history_copy['visit_number'] = history_copy['patient_nbr']
        for i in patient_nbr_array:
            index_list = history_copy[history_copy['patient_nbr'] == i].sort_values(['patient_nbr', 'encounter_id'], axis=0, ascending=True, inplace=False).index.tolist()
            for j in range(1, len(index_list) + 1):
                history_copy['visit_number'][index_list[j - 1]] = j
        X_copy['visit_number'] = X_copy['encounter_id']
        X_copy['visit_number'] = X_copy['visit_number'].apply(lambda x: history_copy.loc[history_copy['encounter_id'] == x, 'visit_number'].iloc[0])
        return X_copy
    def fit(self, X, y=None):
        self.history = X[['encounter_id', 'patient_nbr']].copy(deep=True)
        return self

test_df = pd.read_csv(os.path.join(dataset_path, 'test.csv'), na_values='?')

from collections import Counter
Counter(test_df['readmitted'])