Last time University of Tsukuba Machine Learning Course: Study sklearn while creating the Python script part of the assignment (11) https://github.com/legacyworld/sklearn-basic
This task could not be exactly the same because I did not know the random values of the original data (linear separation, moons, circles), but I think I could grasp the general tendency. Commentary on Youtube: 7th (2) per 48 minutes 30 seconds
The lecture shows that changing the value of C does not change the tendency so much. What is that tendency?
The program was designed to drop everything changed by $ C = 0.01,0.1,0.5,1,10,100 $ into an image.
python:Homework_6.2_linear.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 = [0.01,0.1,0.5,1,10,100]
#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
#Classify by C and dataset
def learn_test_plot(clf_models):
for clf in clf_models:
plt.clf()
#Draw Train Error and Test Error for each of 3 types of data (6 types in total)
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:
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)
dec = clf.decision_function(X_val)
predict = clf.predict(X_val)
train_acc = metrics.accuracy_score(y_val,predict)
test_predict = clf.predict(X_test)
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], 30)
yy = np.linspace(ylim[0], ylim[1], 30)
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"Training 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"Test Accuracy = {test_acc} C = {c_value}")
plt.savefig(f"6.2_{c_value}.png ")
clf_models = [svm.SVC(kernel='linear',C=c_value) for c_value in c_values]
learn_test_plot(clf_models)
Click here for the result of $ C = 0.01,1,100 $ Well, you can say the same result even if you change C
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) https://github.com/legacyworld/sklearn-basic https://ocw.tsukuba.ac.jp/course/systeminformation/machine_learning/
Recommended Posts