<jupyter_start><jupyter_text>Read dataset from PostgreSQL<jupyter_code>import os
from sqlalchemy import create_engine
# Create a POSTGRES database with the name 'COVID19_db'
# Replace username:password if it's not set to postgres:postgres
DATABASE_URI = os.environ.get('DATABASE_URL', '') or "postgresql://postgres:password@localhost:5432/COVID19_db"
print(DATABASE_URI)

engine = create_engine(DATABASE_URI)
case_df=pd.read_sql("select * from case_study_all_v where age!='' and gender!=''", con=engine)
case_df.head()
case_df
case_df.info()
case_data=case_df.drop(["status","id","age","gender"],axis=1)
data=case_data.drop("final_outcome",axis=1)
target=case_data["final_outcome"]
print(data.shape,target.shape)
feature_names=data.columns
feature_names
target_names="final_outcome"<jupyter_output><empty_output><jupyter_text>Train Test Split<jupyter_code>X=data
y=target
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, stratify=y)
X_train.head()
y_train<jupyter_output><empty_output><jupyter_text>Preprocessing<jupyter_code># Scale your data
from sklearn.preprocessing import StandardScaler
# from sklearn.preprocessing import MinMaxScaler
X_scaler = StandardScaler().fit(X_train)
X_train_scaled = X_scaler.transform(X_train)
X_test_scaled = X_scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>rf = RandomForestClassifier(n_estimators=100)
rf = rf.fit(X_train_scaled, y_train)
print(f"Training Data Score: {rf.score(X_train_scaled, y_train)}")
print(f"Testing Data Score: {rf.score(X_test_scaled, y_test)}")<jupyter_output>Training Data Score: 0.7438619774386198
Testing Data Score: 0.610337972166998<jupyter_text>Model Tuning<jupyter_code># Create the GridSearchCV model
from sklearn.model_selection import GridSearchCV
param_grid = {'n_estimators': [100, 200,500,1000],
              'min_impurity_decrease': [0,0.0000001,0.000001,0.0001,0.0001]}
rf = RandomForestClassifier()
grid = GridSearchCV(rf, param_grid, verbose=3)
# Train the model with GridSearch
grid.fit(X_train_scaled, y_train)

print(grid.best_params_)
print(grid.best_score_)
# {'min_impurity_decrease': 0.0001, 'n_estimators': 500} 0.630391506303915
best_rf=RandomForestClassifier(min_impurity_decrease=0.0001,n_estimators=500)
best_rf.fit(X_train_scaled, y_train)
best_rf.score(X_train_scaled, y_train)
best_rf.fit(X_test_scaled,y_test)
best_rf.score(X_test_scaled,y_test)
# Random Forests in sklearn will automatically calculate feature importance
importances = best_rf.feature_importances_
importances
# We can sort the features by their importance
sorted(zip(best_rf.feature_importances_, feature_names), reverse=True)<jupyter_output><empty_output><jupyter_text>Save the Random Forest Model<jupyter_code># save the model to disk
filename = 'model/dropna_age_gender/best_rf.sav'
joblib.dump(best_rf, filename)

zero_count = random.randint(0, 23)
one_count = 23 - zero_count
my_list = [0]*zero_count + [1]*one_count
random.shuffle(my_list)
my_list
# len(my_list)
# Load the model
loaded_model = joblib.load('model/dropna_age_gender/best_rf.sav')
test=np.array(my_list, dtype=np.int32).reshape(1,-1)
result = loaded_model.predict(test)
result
test<jupyter_output><empty_output><jupyter_text>Decision Tree<jupyter_code># Create and score a decision tree classifier
clf = tree.DecisionTreeClassifier(min_impurity_decrease=0.00001, min_samples_split=4)
clf = clf.fit(X_train_scaled , y_train)

print(f"Training Data Score: {clf.score(X_train_scaled, y_train)}")
print(f"Testing Data Score: {clf.score(X_test_scaled, y_test)}")<jupyter_output>Training Data Score: 0.7339084273390842
Testing Data Score: 0.6163021868787276<jupyter_text>Model Tuning<jupyter_code># Create the GridSearchCV model
param_grid = {'min_samples_split': [2, 4, 8, 16],
              'min_impurity_decrease': [0, 0.00001, 0.0001, 0.001]}
grid = GridSearchCV(clf, param_grid, verbose=3)
# Train the model with GridSearch
grid.fit(X_train_scaled, y_train)
print(grid.best_params_)
print(grid.best_score_)
# {'min_impurity_decrease': 0, 'min_samples_split': 16} 0.9441193128185765
best_clf=tree.DecisionTreeClassifier(min_impurity_decrease=0,min_samples_split=16)
best_clf.fit(X_train_scaled, y_train)
best_clf.score(X_train_scaled, y_train)
# Decision Trees in sklearn will automatically calculate feature importance
importances = best_clf.feature_importances_
importances
sorted(zip(best_clf.feature_importances_, feature_names), reverse=True)<jupyter_output><empty_output><jupyter_text>Save the Decision Tree Model<jupyter_code># save the model to disk
filename = 'model/dropna_age_gender/best_clf.sav'
joblib.dump(best_clf, filename)
# Create a decision tree graph
import graphviz 
dot_data = tree.export_graphviz(
    best_clf, out_file=None, 
    feature_names=feature_names,  
    class_names=target_names,  
    filled=True, rounded=True,  
    special_characters=True)  

import pydotplus
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_png('model/dropna_age_gender/decision_tree.png')

graph = graphviz.Source(dot_data)  
graph<jupyter_output><empty_output><jupyter_text>K-nearest neighbour<jupyter_code># Loop through different k values to see which has the highest accuracy
# Note: We only use odd numbers because we don't want any ties
train_scores = []
test_scores = []
for k in range(1, 20, 2):
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(X_train_scaled, y_train)
    train_score = knn.score(X_train_scaled, y_train)
    test_score = knn.score(X_test_scaled, y_test)
    train_scores.append(train_score)
    test_scores.append(test_score)
    print(f"k: {k}, Train/Test Score: {train_score:.3f}/{test_score:.3f}")
    
    
plt.plot(range(1, 20, 2), train_scores, marker='o')
plt.plot(range(1, 20, 2), test_scores, marker="x")
plt.xlabel("k neighbors")
plt.ylabel("Testing accuracy Score")
plt.show()<jupyter_output>k: 1, Train/Test Score: 0.698/0.571
k: 3, Train/Test Score: 0.683/0.583
k: 5, Train/Test Score: 0.666/0.604
k: 7, Train/Test Score: 0.672/0.610
k: 9, Train/Test Score: 0.678/0.618
k: 11, Train/Test Score: 0.667/0.606
k: 13, Train/Test Score: 0.656/0.624
k: 15, Train/Test Score: 0.654/0.602
k: 17, Train/Test Score: 0.648/0.571
k: 19, Train/Test Score: 0.654/0.600<jupyter_text>Model Tuning<jupyter_code>#create new a knn model
knn2 = KNeighborsClassifier()
#create a dictionary of all values we want to test for n_neighbors
param_grid = {'n_neighbors': np.arange(1, 16)}
#use gridsearch to test all values for n_neighbors
knn_gscv = GridSearchCV(knn2, param_grid, cv=5)
#fit model to data
knn_gscv.fit(X_train_scaled, y_train)
knn_gscv.best_params_
# Note: We only use odd numbers because we don't want any ties
# Note that k: 9 provides the best accuracy where the classifier starts to stablize
knn_gscv = KNeighborsClassifier(n_neighbors=9)
knn_gscv.fit(X_train_scaled, y_train)
print('k=9 Train Acc: %.3f' % knn_gscv.score(X_train_scaled, y_train))
print('k=9 Test Acc: %.3f' % knn_gscv.score(X_test_scaled, y_test))<jupyter_output>k=9 Train Acc: 0.678
k=9 Test Acc: 0.618<jupyter_text>Save the KNN Model<jupyter_code># save the model to disk
filename = 'model/dropna_age_gender/best_knn.sav'
joblib.dump(knn_gscv, filename)<jupyter_output><empty_output><jupyter_text>Deep Learning<jupyter_code>import tensorflow as tf
# from tensorflow.keras.models import gi
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

# One-hot encoding
y_train_categorical=to_categorical(y_train)
y_test_categorical=to_categorical(y_test)
X_train_scaled.shape
y_train_categorical.shape
# Create model and add layers
model = Sequential()
model.add(Dense(units=100, activation='relu', input_dim=23))
model.add(Dense(units=100, activation='relu'))
model.add(Dense(units=100, activation='relu'))
model.add(Dense(units=4, activation='softmax'))
model.summary()
# Compile and fit the model
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
# Use the training data to fit (train) the model
model.fit(
    X_train_scaled,
    y_train_categorical,
    epochs=1000,
    shuffle=True,
    verbose=2
)<jupyter_output>Train on 1507 samples
Epoch 1/1000
1507/1507 - 1s - loss: 0.8847 - accuracy: 0.5733
Epoch 2/1000
1507/1507 - 0s - loss: 0.6736 - accuracy: 0.6603
Epoch 3/1000
1507/1507 - 0s - loss: 0.6276 - accuracy: 0.6762
Epoch 4/1000
1507/1507 - 0s - loss: 0.6141 - accuracy: 0.6735
Epoch 5/1000
1507/1507 - 0s - loss: 0.6194 - accuracy: 0.6662
Epoch 6/1000
1507/1507 - 0s - loss: 0.5960 - accuracy: 0.6875
Epoch 7/1000
1507/1507 - 0s - loss: 0.5824 - accuracy: 0.6961
Epoch 8/1000
1507/1507 - 0s - loss: 0.5816 - accuracy: 0.6941
Epoch 9/1000
1507/1507 - 0s - loss: 0.5800 - accuracy: 0.6974
Epoch 10/1000
1507/1507 - 0s - loss: 0.5773 - accuracy: 0.7001
Epoch 11/1000
1507/1507 - 0s - loss: 0.5680 - accuracy: 0.6981
Epoch 12/1000
1507/1507 - 0s - loss: 0.5548 - accuracy: 0.7127
Epoch 13/1000
1507/1507 - 0s - loss: 0.5542 - accuracy: 0.7001
Epoch 14/1000
1507/1507 - 0s - loss: 0.5546 - accuracy: 0.7074
Epoch 15/1000
1507/1507 - 0s - loss: 0.5599 - accuracy: 0.6948
Epoch 16/1000
1507/1507 - 0s - loss: 0.543[...]<jupyter_text>Assess the Deep Learning Model<jupyter_code># Evaluate the model using the training data
model_loss, model_accuracy = model.evaluate(X_test_scaled, y_test_categorical, verbose=2)
print(f"Loss: {model_loss}, Accuracy: {model_accuracy}")
# Grab just one data point to test with
test = np.expand_dims(X_test_scaled[200], axis=0)
test.shape
# Make a prediction. The result should be 5 - STANDING
print(f"Predicted class: {model.predict_classes(test)}")<jupyter_output>Predicted class: [2]<jupyter_text>Save the DL Model<jupyter_code># Save the model
model.save("model/dropna_age_gender/best_dl.h5")<jupyter_output><empty_output><jupyter_text>SVM<jupyter_code>from sklearn.svm import SVC 
model = SVC(kernel='linear')
model.fit(X_train_scaled, y_train)
# Model Accuracy
print('Test Acc: %.3f' % model.score(X_test_scaled, y_test))
target_names=['Death','Hospital','Stay at Home']
# Calculate classification report
from sklearn.metrics import classification_report
predictions = model.predict(X_test_scaled)
print(classification_report(y_test, predictions,
                            target_names=target_names))
# use this to find the optimal parameters for SVR
kernel= ['linear','poly','sigmoid','rbf']
c = [0.01, 0.1, 1]
gamma = [0.01, 0.1, 1]
epsilon = [0.01, 0.1, 1]
shrinking = [True]
degree = [8]

svm_grid = {'kernel':kernel,'C': c, 'gamma' : gamma, 'epsilon': epsilon, 'shrinking' : shrinking, 'degree': degree}

svm = SVR()
svm_search = RandomizedSearchCV(svm, svm_grid, scoring='neg_mean_squared_error', cv=3, return_train_score=True, n_jobs=-1, n_iter=30, verbose=1)
svm_search.fit(X_train_scaled, y_train)<jupyter_output>Fitting 3 folds for each of 30 candidates, totalling 90 fits
{'fraction_non_alphanumeric': 0.11793274076789151, 'fraction_numerical': 0.07257399431870247, 'mean_word_length': 6.213483146067416, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 58, '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': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '341942', 'license': 'MIT', 'n_tokens_mistral': 4719, 'n_tokens_neox': 4150, 'n_words': 882, 'path': "Leo's sandbox/COVID10_classification-drop_null(age,gender).ipynb", 'repo_name': 'DutchDS/Final_Project', 'size': '10913'}