""" ................... Two spirals problem ................... """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn.svm import SVC from sklearn.neural_network import MLPClassifier from sklearn.metrics import confusion_matrix import time def spirals(n): angle=np.linspace(0,n,n) angle=np.pi*angle/16 r=6.5*(104-np.linspace(0,n,n))/104 x1=r*np.cos(angle) x2=r*np.sin(angle) A1=np.vstack((x1,x2,np.ones(n,))).T A2=np.vstack((-x1,-x2,np.zeros(n,))).T data=np.concatenate((A1,A2),axis=0) return data n=100 data=spirals(n) plt.plot(data[:n,0],data[:n,1],'bo') plt.plot(data[n:,0],data[n:,1],'go') plt.show() X=data[:,:2] y=data[:,2] ##################################### # Feladat: probaljuk meg elvalasztani a ket halmazt MLP-vel illetve SVM-mel (RBF kernel)! # Az eredmenyt szemleltessuk az RBF_iris kod vegen latott modon, # azaz egy racs felvetelevel, ahol minden racspontot beszinezunk a halozat altal nyujtott ertek alapjan! # Legyen kulon abra MLP-re es SVM-re # mlp-nel hasznalhatjuk a halozat .predict_proba attributumat ############################################## svm=SVC(gamma=1,C=1) # C: jegyzetben lambda, regularizációs paraméter start_time = time.time() svm.fit(X,y) print('Ennyi ideig tartott SVM-mel: {0} mp'.format(time.time()-start_time)) ############################################## mlp = MLPClassifier(hidden_layer_sizes=(10,10,10,10), max_iter=100,solver='lbfgs',learning_rate_init=.1,activation='tanh') start_time = time.time() mlp.fit(X,y) print('Ennyi ideig tartott MLP-vel: {0} mp'.format(time.time()-start_time)) ############################################## x1_test, x2_test = np.meshgrid(np.linspace(-7,7,100), np.linspace(-7,7,100)) Z = svm.decision_function(np.c_[x1_test.ravel(), x2_test.ravel()]) # decision_function: távolság az elválasztó hipersíktól Z = Z.reshape(x1_test.shape) plt.pcolormesh(x1_test, x2_test, Z) plt.scatter(X[:,0], X[:,1],c=y,edgecolors='1') plt.show() x1_test, x2_test = np.meshgrid(np.linspace(-7,7,100), np.linspace(-7,7,100)) test=np.vstack((x1_test.ravel(),x2_test.ravel())).T Z=mlp.predict_proba(test[:,])[:,1] # predict_proba: valsegek osztalyonkent Z = Z.reshape(x1_test.shape) plt.pcolormesh(x1_test, x2_test, Z) plt.scatter(X[:,0], X[:,1], c=y,edgecolors='1') plt.show() ###################################### # Tévesztési mátrix: y_true = y y_pred = mlp.predict(X) print(confusion_matrix(y_true, y_pred)) print('Pontosság a tanítóhalmazon MLP-vel: {0}%'.format(mlp.score(X, y)*100))