<jupyter_start><jupyter_text>Training a simple model<jupyter_code>from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import RandomizedSearchCV
from sklearn.metrics import accuracy_score, f1_score, roc_auc_score, confusion_matrix
from scipy.stats import uniform
from sklearn.linear_model import LogisticRegression as lr
import seaborn as sns

clf = lr()
clf.fit(padded_text_train_norm, y_train)
y_pred = clf.predict(padded_text_cv_norm)
print("Acccuracy is",accuracy_score(y_cv,y_pred))
print("F1 score is", f1_score(y_cv, y_pred))
confusion_matrix_test = confusion_matrix(y_cv, y_pred)
sns.heatmap(confusion_matrix_test,xticklabels=["1","0"], yticklabels=["1","0"],annot=True).set_title("Confusion Matrix")
plt.show()<jupyter_output><empty_output><jupyter_text>Comaparing and Fine tuning ML classification models<jupyter_code>from xgboost import XGBClassifier
models = {
    'lr': lr(),
    'svc': SVC(),
    'dtc':DecisionTreeClassifier(),
    'rfc': RandomForestClassifier(),
    'xgbc':XGBClassifier(objective = 'binary:logistic', verbosity = 0)
}
def train_and_test_model(model_key, distributions, model_name):
    print("Model training", model_name)
    model = models[model_key]
    clf = RandomizedSearchCV(model, distributions, random_state=0, n_iter = 10, verbose=0)
    clf.fit(padded_text_train_norm, y_train)
    print(clf.best_estimator_)
    final_model = clf.best_estimator_
    final_model.fit(padded_text_train_norm, y_train)
    y_pred = final_model.predict(padded_text_cv)
    print("Acccuracy is",accuracy_score(y_cv,y_pred))
    print("F1 score is", f1_score(y_cv, y_pred))
    confusion_matrix_test = confusion_matrix(y_cv, y_pred)
    sns.heatmap(confusion_matrix_test,xticklabels=["1","0"], yticklabels=["1","0"],annot=True).set_title("Confusion Matrix")
    plt.show()
distributions = dict(C=uniform(loc=0.00001, scale=10000), penalty=['l2', 'l1'], class_weight=['balanced', None])
train_and_test_model('lr', distributions, "logistic regression")
distributions = dict(C=uniform(loc=0.00001, scale=10000),kernel=['linear', 'poly', 'rbf', 'sigmoid'] ,coef0=uniform(loc=0, scale=10), class_weight=['balanced', None], gamma=['scale', 'auto'])
train_and_test_model('svc', distributions, "SVM")
from scipy.stats import randint
distributions = dict(criterion=['gini', 'entropy'], splitter=['best', 'random'], max_depth = randint(1, 100), min_samples_split= uniform(loc=0, scale=1), max_features=['auto', 'sqrt', 'log2'], class_weight=['balanced', None])
train_and_test_model('dtc', distributions, "Decisoin Tree Classifier")
distributions = dict(criterion=['gini', 'entropy'], n_estimators = randint(10, 500) , max_depth = randint(1, 100), min_samples_split= uniform(loc=0, scale=1), max_features=['auto', 'sqrt', 'log2'], class_weight=['balanced', 'balanced_subsample', None])
train_and_test_model('rfc', distributions, "Random forest classifier")<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup