<jupyter_start><jupyter_text>---- Aiming to understand SVM - LR - Random Forest Machine Learning Models- Manipulationn of data---<jupyter_code># common libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline 
import warnings
warnings.filterwarnings("ignore")

# sklearn machine learning models and preprocessing modules
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.utils import shuffle

from sklearn.svm import LinearSVC,SVC
from sklearn.metrics import plot_confusion_matrix, classification_report, plot_roc_curve
from sklearn.model_selection import train_test_split
# data import - load
wine = pd.read_csv("datasets/winequality-white.csv", sep=';')
wine = shuffle(wine)
wine.head()
# quality range
qualities = wine["quality"].unique()
sorted(qualities) # 7,8,9 high quality = 1 ----------- 3,4,5,6 low quality 0
# binning qualities to make this machine learning process easier
bin_wine = wine.copy()
bin_wine.quality = bin_wine.quality.replace([7,8,9],1)
bin_wine.quality = bin_wine.quality.replace([3,4,5,6],0)
qualities = bin_wine["quality"].unique()
sorted(qualities)
# getting data ready to machine learning concets - this notebook focusing on ml algorithms not on preprocessing
X = bin_wine.drop("quality", axis=1)
y = bin_wine["quality"]

X_train, X_test, y_train, y_test = train_test_split(X,y, random_state=42, test_size=0.2)
svm_clf = Pipeline([
    ('scaler', StandardScaler()),
    ('linear_svc', LinearSVC(C=1,loss='hinge'))
])

svm_clf.fit(X_train, y_train)
pred = svm_clf.predict(X_test)
print(classification_report(y_test, pred))
plot_roc_curve(svm_clf, X_test, y_test)
# SVC 
svc = Pipeline([
    ('scaler', StandardScaler()),
    ('svc', SVC(C=10,kernel='rbf'))
])

svc.fit(X_train, y_train)
pred = svc.predict(X_test)
acc = svc.score(X_test,y_test) * 100
print(acc)
print(classification_report(y_test, pred))
plot_roc_curve(svc, X_test, y_test)
from sklearn.feature_extraction.text import CountVectorizer
from sklearn import feature_extraction, linear_model, model_selection, preprocessing
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn import svm

# making ml process automated - gettign ready 4 different algorithms at once
pipe_lr = Pipeline([('scaler', StandardScaler()),
                ('clf', LogisticRegression(random_state=42))])

pipe_dt = Pipeline([('scaler', StandardScaler()),
                 ('model', DecisionTreeClassifier(random_state=42))])

pipe_rf = Pipeline([('scaler', StandardScaler()),
            ('clf', RandomForestClassifier(random_state=42))])


pipe_svm = Pipeline([('scaler', StandardScaler()),
            ('clf', svm.SVC(random_state=42))])
# Set grid search params
param_range = [9, 10]
param_range_fl = [1.0, 0.5]

grid_params_lr = [{'clf__penalty': ['l1', 'l2'],
        'clf__C': param_range_fl,
        'clf__solver': ['liblinear']}] 

grid_params_rf = [{'clf__criterion': ['gini', 'entropy'],
        'clf__max_depth': param_range,
        'clf__min_samples_split': param_range[1:]}]

grid_params_svm = [{'clf__kernel': ['linear', 'rbf'], 
        'clf__C': param_range}]

# Construct grid searches
jobs = -1

LR = GridSearchCV(estimator=pipe_lr,
            param_grid=grid_params_lr,
            scoring='accuracy',
            cv=10) 

RF = GridSearchCV(estimator=pipe_rf,
            param_grid=grid_params_rf,
            scoring='accuracy',
            cv=10, 
            n_jobs=jobs)

SVM = GridSearchCV(estimator=pipe_svm,
            param_grid=grid_params_svm,
            scoring='accuracy',
            cv=10,
            n_jobs=jobs)

# List of pipelines for iterating through each of them
grids = [LR,RF,SVM]

# Creating a dict for our reference
grid_dict = {0: 'Logistic Regression', 
        1: 'Random Forest',
        2: 'Support Vector Machine'}

# Fit the grid search objects
print('Performing model optimizations...')
best_acc = 0.0
best_clf = 0
best_gs = ''
for idx, gs in enumerate(grids):
    print('\nEstimator: %s' % grid_dict[idx])
    gs.fit(X_train, y_train)
    print('Best params are : %s' % gs.best_params_)
    # Best training data accuracy
    print('Best training accuracy: %.3f' % gs.best_score_)
    # Predict on test data with best params
    y_pred = gs.predict(X_test)
    # Test data accuracy of model with best params
    print('Test set accuracy score for best params: %.3f ' % accuracy_score(y_test, y_pred))
    # Track best (highest test accuracy) model
    if accuracy_score(y_test, y_pred) > best_acc:
        best_acc = accuracy_score(y_test, y_pred)
        best_gs = gs
        best_clf = idx
print('\nClassifier with best test set accuracy: %s' % grid_dict[best_clf])
best_gs
# last training with best model and parameters

bestModel = best_gs
bestModel.fit(X_train, y_train)
pred = bestModel.predict(X_test)
acc = bestModel.score(X_test,y_test) * 100
print(acc)
print(classification_report(y_test, pred))
# trying some extras with SVC polynomialFeatures
from sklearn.preprocessing import PolynomialFeatures
# SVC 
svc = Pipeline([
    ('poly_trans',PolynomialFeatures()),
    ('scaler', StandardScaler()),
    ('svc', SVC(C=10,kernel='rbf'))
])

svc.fit(X_train, y_train)
pred = svc.predict(X_test)
acc = svc.score(X_test,y_test) * 100
print(acc)
print(classification_report(y_test, pred))<jupyter_output>83.46938775510205
              precision    recall  f1-score   support

           0       0.87      0.93      0.90       778
           1       0.64      0.46      0.53       202

    accuracy                           0.83       980
   macro avg       0.75      0.70      0.72       980
weighted avg       0.82      0.83      0.82       980
{'fraction_non_alphanumeric': 0.1125311203319502, 'fraction_numerical': 0.023900414937759337, 'mean_word_length': 3.911165444172779, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '89466', 'license': 'MIT', 'n_tokens_mistral': 2116, 'n_tokens_neox': 2002, 'n_words': 499, 'path': 'WineQualityMultiModelsPipelines/SVM_LogisticReg_RandomForest.ipynb', 'repo_name': 'enesoriginal/Data-Science-Projects', 'size': '6025'}