yolobot Δημοσ. 12 Ιουνίου 2016 Δημοσ. 12 Ιουνίου 2016 πως μπορω να καλεσω εναν classifier perception και να τα απεικονισω σε ενα sublot 2x2?
Dr.Fuzzy Δημοσ. 12 Ιουνίου 2016 Δημοσ. 12 Ιουνίου 2016 βιβλιοθήκη; υλοποίηση; Η ερώτηση σου είναι πάρα πολύ γενική.
yolobot Δημοσ. 12 Ιουνίου 2016 Μέλος Δημοσ. 12 Ιουνίου 2016 εχω τον παρακατω κωδικα και θελω να κανω sublot ωστε να κανω 4 διαφορετικά πειράματα με διαφορετική ποσόστωση μεταξύ train και test δεδομένων (10%, 20%,30%, 40%) και να απεικονίσω τις αποφάσεις που πήρε ο classifier σε έναsubplot 2x2 # -*- coding: utf-8 -*-""" """from sklearn import datasetsimport numpy as np iris = datasets.load_iris() # fortosi datasetprint 'ena mikro deigma tou dataset'print 'attribute values = ' , iris.data[0:5]print 'class values = ' , iris.target[0:5]print 'onomasia twn atributes'print iris.feature_namesprint 'onomasia twn katigoriwn'print iris.target_namesprint ' 'print 'diastaseis tou dataset = ' , iris.data.shapeprint type(iris), type(iris.data), type(iris.target) # xrhsh mono ton xaraktiristikon 3 kai 4 (2,3 gia python)X = iris.data[:, [2, 3]]y = iris.targetprint ' 'print('Class labels:', np.unique(y)) # xorismos dedomenon se 70% train kai 30% testfrom sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split( X, y, test_size = 0.1, random_state = 0) X_train, X_test, y_train, y_test = train_test_split( X, y, test_size = 0.2, random_state = 0) # kanonikopoihsh ton dedomenonfrom sklearn.preprocessing import StandardScaler sc = StandardScaler()sc.fit(X_train)X_train_std = sc.transform(X_train)X_test_std = sc.transform(X_test) from sklearn.linear_model import Perceptron ppn = Perceptron(n_iter=100, eta0=0.1, random_state=0)ppn.fit(X_train_std, y_train)y_test.shapey_pred = ppn.predict(X_test_std)print('Misclassified samples: %d' % (y_test != y_pred).sum()) from sklearn.metrics import accuracy_score print('Accuracy: %.2f' % accuracy_score(y_test, y_pred)) from matplotlib.colors import ListedColormapimport matplotlib.pyplot as plt #matplotlib inline def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02): # setup marker generator and color map markers = ('s', 'x', 'o', '^', 'v') colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan') cmap = ListedColormap(colors[:len(np.unique(y))]) # plot the decision surface x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1 x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution)) Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T) Z = Z.reshape(xx1.shape) plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap) plt.xlim(xx1.min(), xx1.max()) plt.ylim(xx2.min(), xx2.max()) # plot all samples X_test, y_test = X[test_idx, :], y[test_idx] for idx, cl in enumerate(np.unique(y)): plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1], alpha=0.8, c=cmap(idx), marker=markers[idx], label=cl) # highlight test samples if test_idx: X_test, y_test = X[test_idx, :], y[test_idx] plt.scatter(X_test[:, 0], X_test[:, 1], c='', alpha=1.0, linewidth=1, marker='o', s=55, label='test set') X_combined_std = np.vstack((X_train_std, X_test_std))y_combined = np.hstack((y_train, y_test)) plot_decision_regions(X=X_combined_std, y=y_combined, classifier=ppn, test_idx=range(105,150))#plt.figure(num=None, figsize=(10,8 ), dpi=300, facecolor='w', edgecolor='k') plt.xlabel('petal length [standardized]' , fontsize = 20)plt.ylabel('petal width [standardized]', fontsize = 20)plt.legend(loc='upper left' , fontsize = 20)plt.title('100 iterations eta = 0.1', fontsize = 24) plt.tight_layout()# plt.savefig('./figures/iris_perceptron_scikit.png', dpi=300)plt.show()
Dr.Fuzzy Δημοσ. 12 Ιουνίου 2016 Δημοσ. 12 Ιουνίου 2016 Δες εδώ έχει ότι χρειάζεσαι http://matplotlib.org/examples/pylab_examples/subplots_demo.html
yolobot Δημοσ. 12 Ιουνίου 2016 Μέλος Δημοσ. 12 Ιουνίου 2016 πως θα βαλω να τα εμφανιζει μεσα στα 4 παραθυρα που μου εμφανιζει ασπρα απλα αλλαζοντας το test size 0.1, 0.2, 0.3, .04 μου εμφανιζει αυτο τωρα ιδου και ο παρακατω κωδικας # -*- coding: utf-8 -*- """@author: stkarlos"""from sklearn import datasetsimport numpy as np iris = datasets.load_iris() # fortosi datasetprint 'ena mikro deigma tou dataset'print 'attribute values = ' , iris.data[0:5]print 'class values = ' , iris.target[0:5]print 'onomasia twn atributes'print iris.feature_namesprint 'onomasia twn katigoriwn'print iris.target_namesprint ' 'print 'diastaseis tou dataset = ' , iris.data.shapeprint type(iris), type(iris.data), type(iris.target) # xrhsh mono ton xaraktiristikon 3 kai 4 (2,3 gia python)X = iris.data[:, [2, 3]]y = iris.targetprint ' 'print('Class labels:', np.unique(y)) # xorismos dedomenon se 70% train kai 30% testfrom sklearn.cross_validation import train_test_split X_train, X_test, y_train, y_test = train_test_split( X, y, test_size = 0.3, random_state = 0) # kanonikopoihsh ton dedomenonfrom sklearn.preprocessing import StandardScaler sc = StandardScaler()sc.fit(X_train)X_train_std = sc.transform(X_train)X_test_std = sc.transform(X_test) from sklearn.linear_model import Perceptron ppn = Perceptron(n_iter=100, eta0=0.1, random_state=0)ppn.fit(X_train_std, y_train)y_test.shapey_pred = ppn.predict(X_test_std)print('Misclassified samples: %d' % (y_test != y_pred).sum()) from sklearn.metrics import accuracy_score print('Accuracy: %.2f' % accuracy_score(y_test, y_pred)) from matplotlib.colors import ListedColormapimport matplotlib.pyplot as plt #matplotlib inline def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02): # setup marker generator and color map markers = ('s', 'x', 'o', '^', 'v') colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan') cmap = ListedColormap(colors[:len(np.unique(y))]) # plot the decision surface x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1 x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution)) Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T) Z = Z.reshape(xx1.shape) plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap) plt.xlim(xx1.min(), xx1.max()) plt.ylim(xx2.min(), xx2.max()) # plot all samples X_test, y_test = X[test_idx, :], y[test_idx] for idx, cl in enumerate(np.unique(y)): plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1], alpha=0.8, c=cmap(idx), marker=markers[idx], label=cl) # highlight test samples if test_idx: X_test, y_test = X[test_idx, :], y[test_idx] plt.scatter(X_test[:, 0], X_test[:, 1], c='', alpha=1.0, linewidth=1, marker='o', s=55, label='test set') X_combined_std = np.vstack((X_train_std, X_test_std))y_combined = np.hstack((y_train, y_test)) plot_decision_regions(X=X_combined_std, y=y_combined, classifier=ppn, test_idx=range(105,150))#plt.figure(num=None, figsize=(10,8 ), dpi=300, facecolor='w', edgecolor='k')# Four axes, returned as a 2-d arrayf, axarr = plt.subplots(2, 2)axarr[0, 0].plot(x, y)axarr[0, 0].set_title('Axis [0,0]')axarr[0, 1].scatter(x, y)axarr[0, 1].set_title('Axis [0,1]')axarr[1, 0].plot(x, y ** 2)axarr[1, 0].set_title('Axis [1,0]')axarr[1, 1].scatter(x, y ** 2)axarr[1, 1].set_title('Axis [1,1]')# Fine-tune figure; hide x ticks for top plots and y ticks for right plotsplt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)plt.xlabel('petal length [standardized]' , fontsize = 20)plt.ylabel('petal width [standardized]', fontsize = 20)plt.legend(loc='upper left' , fontsize = 20)plt.title('100 iterations eta = 0.1', fontsize = 24) plt.tight_layout()# plt.savefig('./figures/iris_perceptron_scikit.png', dpi=300)plt.show()
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα