<jupyter_start><jupyter_text>sklearn intro> Classification, Clustering and Regression- layout: post- toc: false- comments: false- hide: false- search_exclude: true- categories: [fastpages, jupyter] Repeating the contents of http://appliedprogramming.net/machine-learning/introduction.html<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
from sklearn import datasets

import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Supervised classification<jupyter_code>iris = datasets.load_iris()
x_iris, y_iris = iris.data, iris.target
print(f'x.shape: {x_iris.shape}, y.shape - {y_iris.shape}')
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.linear_model import SGDClassifier
x_train, x_test, y_train, y_test = train_test_split(x_iris[:,:2], y_iris, test_size=0.25, random_state=33)

# scale the features
scaler = StandardScaler()
x_train = scaler.fit_transform(x_train)
labels = [iris.target_names[o] for o in y_train]

fig, ax = plt.subplots(figsize=((5,5)))
sns.scatterplot(x_train[:,0], x_train[:, 1], hue=labels)
ax.set_xlabel('length')
ax.set_ylabel('width')
classifier = SGDClassifier()
classifier
classifier.fit(x_train, y_train)
classifier.coef_<jupyter_output><empty_output><jupyter_text>The classifier learnt coefficients for 3 planes, as it does a one-vs-many classification. Let us plot these planes<jupyter_code>x_min, x_max = x_train[:,0].min() - 0.5, x_train[:,0].max() + 0.5
y_min, y_max = x_train[:,1].min() - 0.5, x_train[:,1].max() + 0.5
def plot_x(idx, ax):
    xs = x_train[:,0][y_train == idx]
    ys = (-classifier.intercept_[idx]-xs*classifier.coef_[idx,0])/classifier.coef_[idx,1]
    sns.scatterplot(x_train[:,0], x_train[:,1], hue=labels, ax=ax)
    sns.lineplot(xs, ys, ax=ax)
    
fig, axs = plt.subplots(1, 3, figsize=(15,5))
for idx in range(len(iris.target_names)):
    plot_x(idx, axs[idx])<jupyter_output><empty_output><jupyter_text>Evaluating our results<jupyter_code>from sklearn import metrics
y_pred = classifier.predict(x_test)

print(f'accuracy - {metrics.accuracy_score(y_test, y_pred)}')
print(metrics.classification_report(y_test, y_pred, target_names=iris.target_names))
print(metrics.confusion_matrix(y_test, y_pred))<jupyter_output>[[ 0  0  8]
 [ 0  0 11]
 [ 0  0 19]]<jupyter_text>Cross Validation<jupyter_code>from sklearn.cross_validation import cross_val_score, KFold

clf = Pipeline([
    ('scaler', StandardScaler()),
    ('linear_model', SGDClassifier())
])

# create a kfold cross validation iterator
cv = KFold(x_iris.shape[0], n_folds=5, shuffle=True, random_state=33)
cv
scores = cross_val_score(clf, x_iris, y_iris, cv=cv)
scores<jupyter_output><empty_output><jupyter_text>Clustering<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(x_iris, y_iris, test_size=0.25)

scaler = StandardScaler().fit(x_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
from sklearn import cluster
clf_sepal = cluster.KMeans(init='k-means++', n_clusters=3, random_state=33)
clf_sepal.fit(x_train[:, 0:2])<jupyter_output><empty_output><jupyter_text>Print the labels assigned<jupyter_code>print(clf_sepal.labels_)
colormarkers = [['red', 's'], ['greenyellow', 'o'], ['blue', 'x']]
step = 0.01
margin = 0.1
sl_min, sl_max = x_train[:, 0].min() - margin, x_train[:, 0].max() + margin
sw_min, sw_max = x_train[:, 1].min() - margin, x_train[:, 1].max() + margin

# meshgrid returns two vectors one for x and y of length xy, which reprsent coordinates for the xy points. 
sl, sw = np.meshgrid(np.arange(sl_min, sl_max, step),
                     np.arange(sw_min, sw_max, step))
zs = clf_sepal.predict(np.c_[sl.ravel(), sw.ravel()]).reshape(sl.shape)
centroids_s = clf_sepal.cluster_centers_<jupyter_output><empty_output><jupyter_text>Display the data points and the calculated regions<jupyter_code>fig, ax = plt.subplots(figsize=(6,6))
ax.imshow(zs, interpolation='nearest',  extent=(sl.min(), sl.max(), sw.min(), sw.max()))
ax.set_xlim(sl_min, sl_max)
ax.set_ylim(sw_min, sw_max)
ax.set(xlabel='Sepal Length', ylabel='Sepal Width', title='KMeans clustering')
hues = [colormarkers[o][0] for o in y_train]
markers = [colormarkers[o][1] for o in y_train]
sns.scatterplot(x_train[:,0], x_train[:,1], hue=hues, markers=markers, legend=False)<jupyter_output><empty_output><jupyter_text>Supervised Learning: Regression<jupyter_code>from sklearn.datasets import load_boston
from sklearn.linear_model import SGDRegressor
boston = load_boston()
print(f'shape - {boston.data.shape}')
print(f'boston feature names - {boston.feature_names}')
x_train = boston.data
y_train = boston.target
x_train = StandardScaler().fit_transform(x_train)
y_train = StandardScaler().fit_transform(np.expand_dims(y_train, axis=1))
y_train.shape
import sklearn
def train_and_evaluate(model, x_train, y_train, folds):
    model.fit(x_train, y_train)
    print(f'score on training set {model.score(x_train, y_train):.2f}')
    cv = sklearn.cross_validation.KFold(x_train.shape[0], folds, shuffle=True, random_state=33)
    scores = sklearn.cross_validation.cross_val_score(model, x_train, y_train, cv=cv)
    print(f'Avg score using {folds} cross validation {np.mean(scores):.2f}')
model = SGDRegressor(loss='squared_loss', penalty='l2', random_state=33)
train_and_evaluate(model, x_train, y_train, 5)<jupyter_output>score on training set 0.73
Avg score using 5 cross validation 0.70
{'fraction_non_alphanumeric': 0.13918552036199094, 'fraction_numerical': 0.015927601809954752, 'mean_word_length': 6.905579399141631, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 36, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '93194', 'license': 'Apache-2.0', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2106, 'n_words': 421, 'path': '_notebooks/2021-11-28-sklearn-intro.ipynb', 'repo_name': 'achinta/notes', 'size': '5525'}