<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
from numpy import mean
from numpy import std
from pandas import read_csv
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="ticks", color_codes=True)

from sklearn.pipeline import make_pipeline
from sklearn.svm import SVC
from sklearn.linear_model import SGDClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import cross_val_score
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import RandomForestClassifier

import imblearn
from collections import Counter
from sklearn.datasets import make_classification
from matplotlib import pyplot
from numpy import where
from imblearn.over_sampling import SMOTE
from collections import Counter
from matplotlib import pyplot
from sklearn.preprocessing import LabelEncoder


import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="ticks", color_codes=True)<jupyter_output><empty_output><jupyter_text>Dataframe preparation<jupyter_code>beh = pd.read_csv("behaviorals.csv")
foof = pd.read_csv("foof2features.csv")
foof = foof.rename(columns={"C1": "IDs" ,"C2": "Intercept", "C3": "Slope"})
df = pd.merge( foof, beh, on='IDs', how='inner')
#df.head(50)
print(len(df.index))
print(df['DX_01_Cat'].value_counts())
categories = 10
df2 = df.loc[df['DX_01_Cat'].isin(df['DX_01_Cat'].value_counts()[:categories].index.tolist())]
#print(df2['DX_01'])
print(df2['DX_01_Cat'].value_counts())
df2 = df2.rename(columns={"DX_01_Cat": "DiagnosisCategory"})
len(df2.index)

df2["label"] = df2["DiagnosisCategory"] 
df = df2<jupyter_output><empty_output><jupyter_text>Neurodevelopmental disorders are heavily overrepresented, that's why we divide this category for 3 subcategories: 1. ADHD combined type,  2. ADHD Inattentive type,  3. other neurodevelopmental disorders:<jupyter_code>df.loc[df.DX_01 == "ADHD-Combined Type"  , ['label']] = 'ADHD-Combined Type'
df.loc[df.DX_01 == "ADHD-Inattentive Type"  , ['label']] = 'ADHD-Inattentive Type'
df.loc[df.label == "Neurodevelopmental Disorders"  , ['label']] = 'Other Neurodevelopmental Disorders'

#choose only 6 categories
categories = 6
df = df.loc[df['label'].isin(df['label'].value_counts()[:categories].index.tolist())]
#print(df2['DX_01'])
print(df['label'].value_counts())
len(df.index)


df.to_csv('table_withlabels.csv', index=False)<jupyter_output>Other Neurodevelopmental Disorders    492
ADHD-Inattentive Type                 388
ADHD-Combined Type                    376
Anxiety Disorders                     241
No Diagnosis Given                    203
Depressive Disorders                   85
Name: label, dtype: int64<jupyter_text>Start with classification<jupyter_code>#lets run classification with the 2 features only :  intercept and slope

### here the features and labels:
X = df[['Intercept', 'Slope']].to_numpy()
y = df['label'].to_numpy()<jupyter_output><empty_output><jupyter_text>first shot: SVC<jupyter_code>#SVC
clf = make_pipeline(StandardScaler(), SVC(gamma='auto'))
clf.fit(X, y)

scores = cross_val_score(clf, X, y, cv=5)
score = mean(scores)
score<jupyter_output><empty_output><jupyter_text>Dealing with inbalanced dataset:<jupyter_code># summarize class distribution
counter = Counter(y)
print(counter)
sns.catplot(y="label", kind="count", palette="ch:.25", data=df)
# transform the dataset
oversample = SMOTE()
X, y = oversample.fit_resample(X, y)
# summarize the new class distribution
counter = Counter(y)
print(counter)
#summarize the oversampled distribution
counter = Counter(y)
for k,v in counter.items():
	per = v / len(y) * 100
# plot the distribution
pyplot.bar(counter.keys(), counter.values())
pyplot.show()<jupyter_output><empty_output><jupyter_text>Cost-Sensitive Learning for Multi-Class Classification<jupyter_code># cost sensitive random forest with default class weights
#naive baseline = 0.16666
# evaluate a model
def evaluate_model(X, y, model):
	# define evaluation procedure
	cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=3, random_state=1)
	# evaluate model
	scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
	return scores

# define the model
model = RandomForestClassifier(n_estimators=1000, class_weight='balanced')
# evaluate the model
scores = evaluate_model(X, y, model)
# summarize performance
print('Mean Accuracy: %.3f (%.3f)' % (mean(scores), std(scores)))<jupyter_output>Mean Accuracy: 0.412 (0.019)
{'fraction_non_alphanumeric': 0.11427967891846219, 'fraction_numerical': 0.019433882551753275, 'mean_word_length': 6.3753894080996885, 'pattern_counts': {'":': 4, '<': 27, '<?xml version=': 0, '>': 27, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '205838', 'license': 'MIT', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1557, 'n_words': 394, 'path': 'archive/choice_of_classes.ipynb', 'repo_name': 'MartynaPlomecka/AI4Health', 'size': '4734'}