University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (13)

Last time University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the assignment (12) https://github.com/legacyworld/sklearn-basic

Challenge 6.2 Kernel and SVM

Last time, I couldn't do the Gaussian kernel part, so I will continue to solve Problem 6.2. The Gaussian kernel tunes two parameters.

--C of regularization parameter --In scikit-learn's svm.SVC, the effect of regularization increases in inverse proportion to C. --Hyperparameters - k(x,x') = \exp(- \frac{\||x-x'\||}{\sigma^2})of\frac{1}{\sigma^2}of部分 --The program says $ \ gamma $ (gamma)

In the lecture, if you select gamma from the pull-down menu for hyperparameters, you can move from 0.01 to 300 in 10 steps and select the optimum C (= minimum test error) for drawing. The program saves all gamma (1000,100,1,0.1,0.01) in PNG at once.

python:Homework_6.2_rbf.py


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import matplotlib.colors as mcolors
from sklearn import svm,metrics
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_circles,make_moons,make_blobs

datanames = ['linear_separation','moons','circles']
samples = 200
c_values = [i/100 for i in range(1,30000,1000)]
#3 types of data creation
def datasets(dataname):
    if dataname == 'linear_separation':
        X,y = make_blobs(n_samples=samples,centers=2,random_state=64)
    elif dataname == 'moons':
        X,y = make_moons(n_samples=samples,noise=0.3,random_state=74)
    elif dataname == 'circles':
        X,y = make_circles(n_samples=samples,noise=0.3,random_state=70)
    
    X = preprocessing.MinMaxScaler(feature_range=(-1,1)).fit_transform(X)
    return X,y
#Accuracy for all Cs_Function that returns score
def learn_test_plot(clf_models):
    accs = {}
    for clf in clf_models:
        for dataname in datanames:
            if dataname not in accs:
                accs[dataname] = []
            X,y = datasets(dataname)
            X_tr_val,X_test,y_tr_val,y_test = train_test_split(X,y,test_size=0.3,random_state=42)
            X_tr,X_val,y_tr,y_val = train_test_split(X_tr_val,y_tr_val,test_size=0.2,random_state=42)
            clf.fit(X_tr,y_tr)
            predict = clf.predict(X_val)
            train_acc = metrics.accuracy_score(y_val,predict)
            accs[dataname].append(train_acc)
    return accs
#Move gamma to draw
gamma_list = [1000,100,1,0.1,0.01]
for gamma in gamma_list:
    plt.clf()
    clf_models = [svm.SVC(kernel='rbf',gamma=gamma,C=c_value) for c_value in c_values]
    accs = learn_test_plot(clf_models)
    fig = plt.figure(figsize=(20,10))
    ax = [fig.add_subplot(2,3,i+1) for i in range(6)]
    for a in ax:
        a.set_xlim(-1.5,1.5)
        a.set_ylim(-1.5,1.5)
    for dataname in datanames:
        best_c_index = np.argmax(accs[dataname])
        X,y = datasets(dataname)
        X_tr_val,X_test,y_tr_val,y_test = train_test_split(X,y,test_size=0.3,random_state=42)
        X_tr,X_val,y_tr,y_val = train_test_split(X_tr_val,y_tr_val,test_size=0.2,random_state=42)
        clf = clf_models[best_c_index]
        
        clf.fit(X_tr,y_tr)
        train_predict = clf.predict(X_tr_val)
        test_predict = clf.predict(X_test)
        train_acc = metrics.accuracy_score(y_tr_val,train_predict) 
        test_acc = metrics.accuracy_score(y_test,test_predict)
        c_value = clf.get_params()['C']
        
        #Mesh data
        xlim = [-1.5,1.5]
        ylim = [-1.5,1.5]
        xx = np.linspace(xlim[0], xlim[1], 300)
        yy = np.linspace(ylim[0], ylim[1], 300)
        YY, XX = np.meshgrid(yy, xx)
        xy = np.vstack([XX.ravel(), YY.ravel()]).T
        Z = clf.decision_function(xy).reshape(XX.shape)
        #Color for fill
        blue_rgb = mcolors.to_rgb("tab:blue")
        red_rgb = mcolors.to_rgb("tab:red")
        #Arrange vertically for each dataset
        index = datanames.index(dataname)
        # decision_The larger the function, the darker the color
        ax[index].contourf(XX, YY, Z,levels=[-2,-1,-0.1,0.1,1,2],colors=[red_rgb+(0.5,),red_rgb+(0.3,),(1,1,1),blue_rgb+(0.3,),blue_rgb+(0.5,)],extend='both')
        ax[index].contour(XX,YY,Z,levels=[0],linestyles=["--"])
        ax[index].scatter(X_tr_val[:,0],X_tr_val[:,1],c=y_tr_val,edgecolors='k',cmap=ListedColormap(['#FF0000','#0000FF']))
        ax[index].set_title(f"gamma = {gamma}\nTraining Accuracy = {train_acc} C = {c_value}")

        ax[index+3].contourf(XX, YY, Z,levels=[-2,-1,-0.1,0.1,1,2],colors=[red_rgb+(0.5,),red_rgb+(0.3,),(1,1,1),blue_rgb+(0.3,),blue_rgb+(0.5,)],extend='both')
        ax[index+3].contour(XX,YY,Z,levels=[0],linestyles=["--"])
        ax[index+3].scatter(X_test[:,0],X_test[:,1],c=y_test,edgecolors='k',cmap=ListedColormap(['#FF0000','#0000FF']))
        ax[index+3].set_title(f"gamma = {gamma}\nTest Accuracy = {test_acc} C = {c_value}")
    plt.savefig(f"6.2_{gamma}.png ")

The learn_test_plot function returns accuracy_score for all Cs. Draw in C, which returns the largest accuracy_score from it Larger $ \ gamma $ reduces the diameter of the effect of training data = complex shape If $ \ gamma $ is large, the decision boundary has a very complicated shape, but if $ \ gamma $ is small, it becomes a straight line.

For data created with make_blobs, it makes little sense to have decision boundaries cling to each training data. On the contrary, for the data of make_circles, the decision boundary of the straight line is meaningless. 6.2_1000.png 6.2_1.png 6.2_0.01.png

Past posts

University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the assignment (1) University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the assignment (2) University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the assignment (3) University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the assignment (4) University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the assignment (5) University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the assignment (6) University of Tsukuba Machine Learning Course: Study sklearn while making the Python script part of the task (7) Make your own steepest descent method University of Tsukuba Machine Learning Course: Study sklearn while making the Python script part of the task (8) Make your own stochastic steepest descent method University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the assignment (9) University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the assignment (10) University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the assignment (11) https://github.com/legacyworld/sklearn-basic https://ocw.tsukuba.ac.jp/course/systeminformation/machine_learning/

Recommended Posts

University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (17)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (5)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (16)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (10)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (2)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (13)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (9)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (4)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (12)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (1)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (11)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (3)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (14)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (6)
University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the task (15)
University of Tsukuba Machine Learning Course: Study sklearn while making the Python script part of the task (7) Make your own steepest descent method
University of Tsukuba Machine Learning Course: Study sklearn while making the Python script part of the task (8) Make your own stochastic steepest descent method
Python & Machine Learning Study Memo ⑤: Classification of irises
Python & Machine Learning Study Memo ②: Introduction of Library
Image collection Python script for creating datasets for machine learning
Summary of the basic flow of machine learning with Python
The result of Java engineers learning machine learning in Python www
[Machine learning pictorial book] A memo when performing the Python exercise at the end of the book while checking the data
Python learning memo for machine learning by Chainer until the end of Chapter 2
Python & Machine Learning Study Memo: Environment Preparation
Learning notes from the beginning of Python 1
I installed Python 3.5.1 to study machine learning
Python Basic Course (at the end of 15)
Python & Machine Learning Study Memo ③: Neural Network
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
Learning notes from the beginning of Python 2
Python & Machine Learning Study Memo ⑥: Number Recognition
Align the number of samples between classes of data for machine learning with Python
Machine learning memo of a fledgling engineer Part 1
[Python] Read the source code of Bottle Part 2
Classification of guitar images by machine learning Part 1
The story of low learning costs for Python
2016 The University of Tokyo Mathematics Solved with Python
Upgrade the Azure Machine Learning SDK for Python
EV3 x Python Machine Learning Part 2 Linear Regression
[Python] Read the source code of Bottle Part 1
About the development contents of machine learning (Example)
Machine learning memo of a fledgling engineer Part 2
Classification of guitar images by machine learning Part 2
Get a glimpse of machine learning in Python
Python & Machine Learning Study Memo ⑦: Stock Price Forecast
[Python + OpenCV] Whiten the transparent part of the image
Predicting the goal time of a full marathon with machine learning-③: Visualizing data with Python-
The first step of machine learning ~ For those who want to implement with python ~
[CodeIQ] I wrote the probability distribution of dice (from CodeIQ math course for machine learning [probability distribution])