<jupyter_start><jupyter_text>Do you currently have a mental health disorder?<jupyter_code># Create our target:
y = encoded_df_dx_mh["mh_dx_current"]

# Create our features:
X = encoded_df_dx_mh.drop(columns = "mh_dx_current", axis =1)
# Split the data:
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=5, stratify=y, test_size = 0.25)<jupyter_output><empty_output><jupyter_text>Balanced Random Forest Classsifier<jupyter_code>from sklearn.ensemble import RandomForestClassifier

# Create a random forest classifier:
rf_model = RandomForestClassifier(n_estimators=100, random_state=1) 

# Fitting the model:
rf_model = rf_model.fit(X_train, y_train)

# Making predictions using the testing data:
predictions = rf_model.predict(X_test)
# Predict outcomes for test data set:
predictions = rf_model.predict(X_test)
predictions_df =  pd.DataFrame({"Actual": y_test, "Prediction": predictions})
predictions_df.head()
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import numpy as np

# Plot predictions data frame:
x1 = y_test
y1 = np.arange(0,192,1)
x2 = predictions
y2 = np.arange(0,192,1)

# Plot actual and predictions:
figure(figsize=(10, 6), dpi=80)
plt.plot(x1, y1, label = "Actual", color = "blue")
plt.plot(x2, y2, label = "Predictions", color = "red")
plt.title("Actual and Predictions for current mental health diagnostic target:")
plt.xlabel("MH diagnostic value")
plt.ylabel("Index")
plt.show()
#plt.savefig("../images/random_forrest_current_dx_predictions.png")
y_pred = ([0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0,
       1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1,
       0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1,
       0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0,
       1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1,
       0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0,
       1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1,
       1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0])
confusion_matrix(y_test, predictions)

# Create a DataFrame from the confusion matrix:
matrix = confusion_matrix(y_test, y_pred)
results = pd.DataFrame(matrix, index = ["Dx_positif", "Dx_negatif"], columns = ["Predicted True", "Predicted False"])
results
x = results["Predicted True"].value_counts()
y = results["Predicted False"].value_counts() 
results.plot()
plt.xlabel("Diagnosis")
plt.ylabel("Numbers of Diagnosis")
plt.show()
# Calculated the balanced accuracy score
from sklearn.metrics import accuracy_score
y_pred = predictions

# Print the imbalanced classification report:
from imblearn.metrics import classification_report_imbalanced

print(f"Accuracy score: {accuracy_score(y_test, y_pred)}")
print("------------------------------------------------------------------------------------")
print(f"Classification report:           Balanced Random Forest Classifier   ")
print("------------------------------------------------------------------------------------")
print(classification_report_imbalanced(y_test, y_pred))
print("------------------------------------------------------------------------------------")<jupyter_output>Accuracy score: 0.7864583333333334
------------------------------------------------------------------------------------
Classification report:           Balanced Random Forest Classifier   
------------------------------------------------------------------------------------
                   pre       rec       spe        f1       geo       iba       sup

          0       0.82      0.81      0.75      0.81      0.78      0.61       111
          1       0.74      0.75      0.81      0.75      0.78      0.61        81

avg / total       0.79      0.79      0.78      0.79      0.78      0.61       192

------------------------------------------------------------------------------------<jupyter_text>Naive Random Oversampling:<jupyter_code># Resample the training data with the RandomOversampler
from imblearn.over_sampling import RandomOverSampler
from collections import Counter

ros = RandomOverSampler(random_state=1)
X_resampled, y_resampled = ros.fit_resample(X_train, y_train)

Counter(y_resampled)
# Train the Logistic Regression model using the resampled data:
from sklearn.linear_model import LogisticRegression

model = LogisticRegression(solver='lbfgs', random_state=1)
model.fit(X_resampled, y_resampled)

# Predict outcomes for test data set
predictions = model.predict(X_test)
predictions_df = pd.DataFrame({"Actual": y_test, "Prediction": predictions,})
predictions_df.head()
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import numpy as np

# Plot predictions data frame:
x1 = y_test
y1 = np.arange(0,192,1)
x2 = predictions
y2 = np.arange(0,192,1)

# Plot actual and predictions:
figure(figsize=(10, 6), dpi=80)
plt.plot(x1, y1, label = "Actual", color = "blue")
plt.plot(x2, y2, label = "Predictions", color = "red")
plt.title("Actual and Predictions for current mental health diagnostic target:")
plt.xlabel("MH diagnostic value")
plt.ylabel("Index")
plt.show()
confusion_matrix(y_test, predictions)

# Create a DataFrame from the confusion matrix:
matrix = confusion_matrix(y_test, y_pred)
results = pd.DataFrame(matrix, index = ["Dx_positif", "Dx_negatif"], columns = ["Predicted True", "Predicted False"])
results
x = results["Predicted True"].value_counts()
y = results["Predicted False"].value_counts() 
results.plot()
plt.xlabel("Diagnosis")
plt.ylabel("Numbers of Diagnosis")
plt.show()
# Calculated the balanced accuracy score:
y_pred = model.predict(X_test)

# Print the imbalanced classification report:

print(f"Accuracy score: {accuracy_score(y_test, y_pred)}")
print("------------------------------------------------------------------------------------")
print(f"Classification report:           Naive Random Oversampling:  ")
print("------------------------------------------------------------------------------------")
print(classification_report_imbalanced(y_test, y_pred))
print("------------------------------------------------------------------------------------")<jupyter_output>Accuracy score: 0.8385416666666666
------------------------------------------------------------------------------------
Classification report:           Naive Random Oversampling:  
------------------------------------------------------------------------------------
                   pre       rec       spe        f1       geo       iba       sup

          0       0.89      0.82      0.86      0.85      0.84      0.71       111
          1       0.78      0.86      0.82      0.82      0.84      0.71        81

avg / total       0.84      0.84      0.85      0.84      0.84      0.71       192

------------------------------------------------------------------------------------<jupyter_text>Model improvement: Add more features into the algorithm.<jupyter_code># Select the desire columns for machine learning model:
tech_2016_df_current_dx_mh2 = tech_2016_df[["work_position", "company_size", "mh_medical_leave", "country_work", "mh_family_history",
                                           "mh_dx_pro", "mh_coverage", "mh_dx_current", "gender", "mh_dx_past", "prev_mh_benefits"
                                           ,"country_live", "mh_discussion_coworkers", "mh_discussion_supervisors",
                                            "yes_condition_dx", "remote", "prev_mh_discussion_coworkers", "prev_mh_discussion_supervisors",
                                            "prev_mh_importance_employer", "mh_hurt_on_career"
                                          ]]

# Check:
print(tech_2016_df_current_dx_mh2.shape)
tech_2016_df_current_dx_mh2.head()
# Create our target:
y = encoded_df_dx_mh["mh_dx_current"]

# Create our features:
X = encoded_df_dx_mh.drop(columns = "mh_dx_current", axis =1)
# Encode dataset:

# Create label encoder instance:
le = LabelEncoder()

# Make a copy of desire data:
encoded_df_dx_mh = tech_2016_df_current_dx_mh2.copy()

# Encode all desired columns:
features = encoded_df_dx_mh.columns.tolist()
for feature in features:
    encoded_df_dx_mh[feature] = le.fit_transform(encoded_df_dx_mh[feature])

# Check:
print(encoded_df_dx_mh.shape)
encoded_df_dx_mh.head()

# Split the data:
X_train1, X_test1, y_train1, y_test1 = train_test_split(X, y, random_state=5, stratify=y, test_size = 0.25)

from sklearn.ensemble import RandomForestClassifier

# Create a random forest classifier:
rf_model = RandomForestClassifier(n_estimators=100, random_state=1) 

# Fitting the model:
rf_model = rf_model.fit(X_train1, y_train1)

# Making predictions using the testing data:
predictions = rf_model.predict(X_test1)
# Predict outcomes for test data set:
predictions1 = rf_model.predict(X_test1)
predictions_df = pd.DataFrame({"Actual": y_test1, "Prediction": predictions1})
predictions_df.head()
confusion_matrix(y_test1, predictions)

# Create a DataFrame from the confusion matrix:
matrix = confusion_matrix(y_test1, y_pred)
results = pd.DataFrame(matrix, index = ["Dx_positif", "Dx_negatif"], columns = ["Predicted True", "Predicted False"])
results
x = results["Predicted True"].value_counts()
y = results["Predicted False"].value_counts() 
results.plot()
plt.xlabel("Diagnosis")
plt.ylabel("Numbers of Diagnosis")
plt.show()
# Calculated the balanced accuracy score
from sklearn.metrics import accuracy_score
y_pred = predictions1

# Print the imbalanced classification report:
from imblearn.metrics import classification_report_imbalanced

print(f"Accuracy score: {accuracy_score(y_test1, y_pred)}")
print("------------------------------------------------------------------------------------")
print(f"Classification report:           Balanced Random Forest Classifier   ")
print("------------------------------------------------------------------------------------")
print(classification_report_imbalanced(y_test1, y_pred))
print("------------------------------------------------------------------------------------")<jupyter_output>Accuracy score: 0.8072916666666666
------------------------------------------------------------------------------------
Classification report:           Balanced Random Forest Classifier   
------------------------------------------------------------------------------------
                   pre       rec       spe        f1       geo       iba       sup

          0       0.85      0.81      0.80      0.83      0.81      0.65       111
          1       0.76      0.80      0.81      0.78      0.81      0.65        81

avg / total       0.81      0.81      0.81      0.81      0.81      0.65       192

------------------------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.2388170055452865, 'fraction_numerical': 0.05027726432532347, 'mean_word_length': 1.98099173553719, 'pattern_counts': {'":': 6, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '64582', 'license': 'MIT', 'n_tokens_mistral': 3679, 'n_tokens_neox': 3248, 'n_words': 898, 'path': 'machine_learning/ML_model_2.0.ipynb', 'repo_name': 'ArtTucker/mental_health_and_economics', 'size': '10820'}