<jupyter_start><jupyter_text>data analysis columns eidvdatercountgenderdialysisrenalendstageasthmairondefpneumsubstancedependencepsychologicaldisordermajordepresspsychotherfibrosisandothermalnutritionhemohematocritneutrophilssodiumglucosebloodureanitrocreatininebmipulserespirationsecondarydiagnosisnonicd9dischargedfacidTarget = lengthofstay<jupyter_code>data = pd.read_csv("/Users/anthonycoplo/Desktop/LengthOfStay - Copy _2_.csv")
data
data.columns
data.dtypes
data.dtypes.value_counts().plot.pie()<jupyter_output><empty_output><jupyter_text>NaN<jupyter_code>(data.isna().sum()/data.shape[0]).sort_values(ascending = True)<jupyter_output><empty_output><jupyter_text>Target analysis<jupyter_code>data['lengthofstay'].value_counts()
sns.countplot(x=data["lengthofstay"])<jupyter_output><empty_output><jupyter_text>distribution plot<jupyter_code>for col in data.select_dtypes('float64'):
    sns.set()
    sns.displot(data[col], kde =True)
for col in data.select_dtypes('int64'):
    sns.set()
    sns.displot(data[col], kde =True)<jupyter_output><empty_output><jupyter_text>Sex / target analysis<jupyter_code>sns.displot(data, x = 'gender', hue = 'lengthofstay')<jupyter_output><empty_output><jupyter_text>Pre process<jupyter_code>df1 = data
#transform date in datetime object
df1['vdate'] = pd.to_datetime(df1['vdate'])

#create new feature
df1['daysofweek_admit']=df1['vdate'].dt.day_name()
#transform target to numerical values
df1['lengthofstay']=df1['lengthofstay'].astype('int')
## encode categorical features
from sklearn.preprocessing import LabelEncoder

encoder = LabelEncoder()

df1['rcount'] = encoder.fit_transform(df1['rcount'])

df1['gender'] = encoder.fit_transform(df1['gender'])

df1['daysofweek_admit'] = encoder.fit_transform(df1['daysofweek_admit'])

df1['facid'] = encoder.fit_transform(df1['facid'])
print(df1.dtypes)<jupyter_output>eid                                    int64
vdate                         datetime64[ns]
rcount                                 int64
gender                                 int64
dialysisrenalendstage                  int64
asthma                                 int64
irondef                                int64
pneum                                  int64
substancedependence                    int64
psychologicaldisordermajor             int64
depress                                int64
psychother                             int64
fibrosisandother                       int64
malnutrition                           int64
hemo                                   int64
hematocrit                           float64
neutrophils                          float64
sodium                               float64
glucose                              float64
bloodureanitro                       float64
creatinine                           float64
bmi                                  float64
pulse     [...]<jupyter_text>split data<jupyter_code>df2 = df1[['rcount', 'gender', 'dialysisrenalendstage', 'asthma',
       'irondef', 'pneum', 'substancedependence', 'psychologicaldisordermajor',
       'depress', 'psychother', 'fibrosisandother', 'malnutrition', 'hemo',
       'hematocrit', 'neutrophils', 'sodium', 'glucose', 'bloodureanitro',
       'creatinine', 'bmi', 'pulse', 'respiration',
       'secondarydiagnosisnonicd9',  'facid', 'daysofweek_admit','lengthofstay']]
label = df1[['rcount', 'gender', 'dialysisrenalendstage', 'asthma',
       'irondef', 'pneum', 'substancedependence', 'psychologicaldisordermajor',
       'depress', 'psychother', 'fibrosisandother', 'malnutrition', 'hemo',
       'hematocrit', 'neutrophils', 'sodium', 'glucose', 'bloodureanitro',
       'creatinine', 'bmi', 'pulse', 'respiration',
       'secondarydiagnosisnonicd9',  'facid', 'daysofweek_admit']]
target = df2["lengthofstay"]
X_train, X_test, y_train, y_test = train_test_split(label, target, test_size=0.3) # 70% training and 30% test<jupyter_output><empty_output><jupyter_text>try with RF regressor<jupyter_code>from sklearn.ensemble import RandomForestRegressor

model = RandomForestRegressor(random_state = 0)
model.fit(X_train, y_train)

y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print('Mean Squared Error for {} is'.format(name), mse)
score = model.score(X_test,y_test)
score
y_pred<jupyter_output><empty_output><jupyter_text>Try to optimise model by gridsearch<jupyter_code>tuned_parameters = {'n_estimators': [500, 700, 1000], 'max_depth': [None, 1, 2, 3], 'min_samples_split': [1, 2, 3]}
clf = GridSearchCV(RandomForestRegressor(), tuned_parameters, cv=5, n_jobs=-1, verbose=1)
clf.fit(X_train, y_train)
print(clf.best_estimator_)
model = RandomForestRegressor(min_samples_split=3, n_estimators=1000)
model.fit(X_train, y_train)

y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print('Mean Squared Error for {} is'.format(name), mse)
score = model.score(X_test,y_test)
score<jupyter_output><empty_output><jupyter_text>Feature importance<jupyter_code>importance = model.feature_importances_

# summarize feature importance
for i,v in enumerate(importance):
    print('Feature: %0d, Score: %.5f' % (i,v))
    
# plot feature importance
plt.bar([x for x in range(len(importance))], importance)
plt.show()<jupyter_output>Feature: 0, Score: 0.56629
Feature: 1, Score: 0.00072
Feature: 2, Score: 0.00361
Feature: 3, Score: 0.00132
Feature: 4, Score: 0.00185
Feature: 5, Score: 0.00143
Feature: 6, Score: 0.00169
Feature: 7, Score: 0.00593
Feature: 8, Score: 0.00201
Feature: 9, Score: 0.00099
Feature: 10, Score: 0.00035
Feature: 11, Score: 0.00110
Feature: 12, Score: 0.02117
Feature: 13, Score: 0.04588
Feature: 14, Score: 0.01509
Feature: 15, Score: 0.02562
Feature: 16, Score: 0.02570
Feature: 17, Score: 0.01780
Feature: 18, Score: 0.02552
Feature: 19, Score: 0.02615
Feature: 20, Score: 0.02324
Feature: 21, Score: 0.02802
Feature: 22, Score: 0.00251
Feature: 23, Score: 0.15311
Feature: 24, Score: 0.00290<jupyter_text>export model<jupyter_code>import pickle
import bz2

# save the model to disk
pickle.dump(model, open('/Users/anthonycoplo/Desktop/LOS_model.pkl','wb'))

X_train.columns
X_test
pd.set_option('display.max_columns', None)
liste = ['0','1','0','0','0','0','0','0', '0', '0', '0','0','0','11.9','9.4','135.149424','136.819548','12.0','1.303508','32.424760','77','6.5','1','1','2']
listenp = pd.Series(liste)
listenp
X_test
y_test
model_imp = pickle.load(open('/Users/anthonycoplo/Desktop/model/LOS_model.pkl', 'rb'))
res = X_test.loc[70589]
res
y_pred_v = model_imp.predict([['0','1','0','0','0','0','0','0', '0', '0', '0','0','0','11.9','9.4','135.149424','136.819548','12.0','1.303508','32.424760','77','6.5','1','1','2']])
y_pred_v
str(y_pred_v.item())
X_test["facid"].value_counts()
help("modules")<jupyter_output>Please wait a moment while I gather a list of all available modules...
{'fraction_non_alphanumeric': 0.1406570841889117, 'fraction_numerical': 0.062188325022000585, 'mean_word_length': 4.205343511450382, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '191184', 'license': 'MIT', 'n_tokens_mistral': 2712, 'n_tokens_neox': 2364, 'n_words': 432, 'path': 'LOS in hospital.ipynb', 'repo_name': 'anthony-coplo/LOS-in-hospital', 'size': '6818'}