| import numpy as np | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| import warnings | |
| warnings.filterwarnings('ignore') | |
| df = pd.read_csv('/content/Raw Data.csv') | |
| df.info() | |
| cols_index = list(range(7, 14)) + list(range(16, 26)) + list(range(28,37)) | |
| df.drop(columns=df.columns[cols_index], inplace=True) | |
| df.info() | |
| df.columns = df.columns.str.replace(r'^\d+\.\s*','', regex=True) | |
| df.rename(columns = {'Did you receiver a waiver or scholarship at your university?': 'Waiver/Scholarship'}, inplace=True) | |
| df.info() | |
| value_cols = ['Anxiety Value', 'Stress Value', 'Depression Value'] | |
| for col in value_cols: | |
| plt.figure(figsize=(10,6)) | |
| plt.title(f'{col}, Distribution') | |
| sns.histplot(data=df, x=col, bins=10) | |
| plt.show() | |
| label_cols = ['Anxiety Label', 'Stress Label', 'Depression Label'] | |
| for col in label_cols: | |
| data = df[col].value_counts().reset_index() | |
| plt.figure(figsize=(10,6)) | |
| plt.title(f'{col} Distribution') | |
| sns.barplot(data=data, x=col, y='count', palette='viridis', width=.4) | |
| plt.xticks(rotation=45) | |
| plt.xlabel('') | |
| plt.ylabel('') | |
| plt.show() | |
| features = df.drop(columns=value_cols+label_cols).columns | |
| for label in label_cols: | |
| label_name = label.split(' ')[0] | |
| print(f'\n{label_name} Analysis\n') | |
| for col in features: | |
| pivot_table = pd.crosstab(df[col], df[label]) | |
| pivot_tabel = pivot_table.div(pivot_table.sum(axis=1), axis=0) * 100 | |
| plt.figure(figsize=(10,8)) | |
| sns.heatmap(pivot_table, annot=True, fmt='.2f', cmap='YlGnBu') | |
| plt.title(f'{label_name} vs {col}') | |
| plt.xlabel('') | |
| plt.ylabel('') | |
| plt.yticks(rotation=0) | |
| plt.show() | |
| from sklearn.preprocessing import OrdinalEncoder | |
| df.drop(columns=value_cols, inplace=True) | |
| ordinal_encoder = OrdinalEncoder() | |
| df[df.columns] = ordinal_encoder.fit_transform(df[df.columns]) | |
| df.head() | |
| for label in label_cols: | |
| cols = list(features).copy() | |
| cols.append(label) | |
| data = df[cols].corr() | |
| plt.figure(figsize=(10,8)) | |
| sns.heatmap(data, annot=True, fmt='.3f', cmap='coolwarm') | |
| plt.title(f'{label} Correlation') | |
| plt.show() | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.metrics import accuracy_score | |
| print('Model Accuracy') | |
| for label in label_cols: | |
| X = df.drop(columns=label_cols) | |
| y = df[label] | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2, random_state = 0) | |
| model = RandomForestClassifier(random_state=0, n_estimators=30, max_depth=8) | |
| model.fit(X_train, y_train) | |
| preds = model.predict(X_test) | |
| print(f'{label}: {accuracy_score(y_test, preds) * 100:.2f}%') |