<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn import svm
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.model_selection import train_test_split

# Load Data from DataSet, then Data Pre-Processing, and Selection
wine= load_wine()

df = pd.DataFrame(data= np.c_[wine['data'],
                             wine['target']], columns= wine['feature_names'] + ['targets'])
x = wine.data
y = wine.target

df.head()
# ,'malic_acid','ash','alcalinity_of_ash','magnesium','total_phenols'
X = np.asarray(df[['alcohol','malic_acid','ash','alcalinity_of_ash','magnesium','total_phenols']])
df.shape
df['flavanoids'] = df['flavanoids'].astype('int')
X[0:5]
y = np.asarray(df['flavanoids'])
y [0:5]
from sklearn import preprocessing
X = preprocessing.StandardScaler().fit(X).transform(X)
X[0:5]<jupyter_output><empty_output><jupyter_text>Train/Test dataset<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=4)
print ('Train set:', X_train.shape,  y_train.shape)
print ('Test set:', X_test.shape,  y_test.shape)<jupyter_output>Train set: (142, 6) (142,)
Test set: (36, 6) (36,)<jupyter_text>Modeling (Logistic Regression with Scikit-learn)<jupyter_code># instantiate the model (using the default parameters)
logreg = LogisticRegression()

# fit the model with data
logreg.fit(X_train,y_train)

#
y_pred=logreg.predict(X_test)<jupyter_output><empty_output><jupyter_text>SVC Decision Tree   Model Evaluation using Confusion Matrix<jupyter_code># import the metrics class
from sklearn import metrics
cnf_matrix = metrics.confusion_matrix(y_test, y_pred)
cnf_matrix
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
LR = LogisticRegression(C=0.01, solver='liblinear').fit(X_train,y_train)
yhat = LR.predict(X_test)
yhat
yhat_prob = LR.predict_proba(X_test)
yhat_prob
from sklearn.metrics import jaccard_similarity_score
jaccard_similarity_score(y_test, yhat)
from sklearn.metrics import classification_report, confusion_matrix
import itertools
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
print(confusion_matrix(y_test, yhat, labels=[1,0]))
# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test, yhat, labels=[1,0])
np.set_printoptions(precision=2)


# Plot non-normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=['feature_names=1','target=0'],normalize= False,  title='Confusion matrix')
print (classification_report(y_test, yhat))

################
import numpy as np
from sklearn import svm
X=x[:,6:8]
x_train, x_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=1,stratify=y)

 
X = np.array([[0, 0], [1, 1]])
y = [0, 1]
clf = svm.SVC(kernel='precomputed')

# kernel computation
K = np.zeros(shape = (n, n))

# "At the moment, the kernel values between all training vectors 
#  and the test vectors must be provided." 
#  according to scikit learn web page. 
#  -- This is the problem!
# v1: array, shape (n, d)
# w1: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(v1, v1)
mu = 1.0 / numpy.mean(chi)
K += w1 * numpy.exp(-mu * chi)

# v2: array, shape (n, d)
# w2: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(v2, v2)
mu = 1.0 / numpy.mean(chi)
K += w2 * numpy.exp(-mu * chi)

# v3: array, shape (n, d)
# w3: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(v3, v3)
mu = 1.0 / numpy.mean(chi)
K += w3 * numpy.exp(-mu * chi)

# v4: array, shape (n, d)
# w4: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(v4, v4)
mu = 1.0 / numpy.mean(chi)
K += w4 * numpy.exp(-mu * chi)

# scikit-learn is a wrapper LIBSVM and looking at the LIBSVM Readme file
# it seems you need kernel values for test data something like this:    

Kt = numpy.zeros(shape = (nt, n))
# t1: array, shape (nt, d)
# w1: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(t1, v1)
mu = 1.0 / numpy.mean(chi)
Kt += w1 * numpy.exp(-mu * chi)

# v2: array, shape (n, d)
# w2: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(t2, v2)
mu = 1.0 / numpy.mean(chi)
Kt += w2 * numpy.exp(-mu * chi)

# v3: array, shape (n, d)
# w3: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(t3, v3)
mu = 1.0 / numpy.mean(chi)
Kt += w3 * numpy.exp(-mu * chi)

# v4: array, shape (n, d)
# w4: float in [0, 1)
chi = sklearn.metrics.pairwise.chi2_kernel(t4, v4)
mu = 1.0 / numpy.mean(chi)
Kt += w4 * numpy.exp(-mu * chi)

clf.fit(K, y) 

# predict on testing examples
probas_ = clf.predict_proba(Kt)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13860558435044307, 'fraction_numerical': 0.02156829961544892, 'mean_word_length': 4.458029197080292, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '125089', 'license': 'CC0-1.0', 'n_tokens_mistral': 2271, 'n_tokens_neox': 2193, 'n_words': 547, 'path': 'MachineLearningClassification-WineQuality.ipynb', 'repo_name': 'ArmandoJM/learning-area', 'size': '5981'}