4 import matplotlib.pyplot
as plt
5 from matplotlib
import style
8 from sklearn
import svm
13 csv_e = np.genfromtxt (
'testdata/set001/CalorimeterResponseHijingCentralRapidity_e_8GeV.csv')
14 csv_pi = np.genfromtxt (
'testdata/set001/CalorimeterResponseHijingCentralRapidity_pi_8GeV.csv')
21 data_e_sub = csv_e[:,2:5]
22 data_pi_sub = csv_pi[:,2:5]
25 data_e_train = data_e_sub[0:500,:]
26 data_e_test = data_e_sub[500:1000,:]
28 data_pi_train = data_pi_sub[0:500,:]
29 data_pi_test = data_pi_sub[500:1000,:]
32 data_train = np.vstack((data_e_train,data_pi_train))
33 data_test = np.vstack((data_pi_test,data_e_test))
38 pid_train = np.vstack(( np.ones((500,1)) , np.zeros((500,1)) ))
39 pid_test = np.vstack(( np.zeros((500,1)) , np.ones((500,1)) ))
43 y = np.ravel(pid_train)
48 svc = svm.SVC(kernel=
'linear', C=C).
fit(X, y)
49 rbf_svc = svm.SVC(kernel=
'rbf', gamma=0.7, C=C).
fit(X, y)
50 poly_svc = svm.SVC(kernel=
'poly', degree=3, C=C).
fit(X, y)
51 lin_svc = svm.LinearSVC(C=C).
fit(X, y)
54 x_min, x_max = X[:, 0].
min() - 0.01, X[:, 0].max() + 0.01
55 y_min, y_max = X[:, 1].
min() - 0.01, X[:, 1].max() + 0.01
56 xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
57 np.arange(y_min, y_max, h))
60 titles = [
'SVC with linear kernel',
61 'LinearSVC (linear kernel)',
62 'SVC with RBF kernel',
63 'SVC with polynomial (degree 3) kernel']
66 for i, clf
in enumerate((svc, lin_svc, rbf_svc, poly_svc)):
69 plt.subplot(2, 2, i + 1)
70 plt.subplots_adjust(wspace=0.4, hspace=0.4)
72 Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
75 Z = Z.reshape(xx.shape)
76 plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
79 plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
80 plt.xlabel(
'energy EMCAL')
81 plt.ylabel(
'energy HCAL (in)')
82 plt.xlim(xx.min(), xx.max())
83 plt.ylim(yy.min(), yy.max())
90 for i, clf
in enumerate((svc, lin_svc, rbf_svc, poly_svc)):
92 pid_predict = clf.predict( data_test[:,1:3] )
96 count_electron_as_electron = 0
97 count_pion_as_pion = 0
98 count_electron_as_pion = 0
99 count_pion_as_electron = 0
100 count_true_electron = 0
103 for pid_pred, pid_true
in itertools.izip(pid_predict, pid_test):
107 if (pid_pred == id_e) & (pid_true == id_e):
108 count_electron_as_electron += 1
109 count_true_electron += 1
110 elif (pid_pred == id_pi) & (pid_true == id_pi):
111 count_pion_as_pion += 1
113 elif (pid_pred == id_pi) & (pid_true == id_e):
114 count_electron_as_pion += 1
115 count_true_electron += 1
116 elif (pid_pred == id_e) & (pid_true == id_pi):
117 count_pion_as_electron += 1
122 print "-----------------------------------"
124 print "-----------------------------------"
125 print "Electrons identified as electrons: %d out of %d" % (count_electron_as_electron, count_true_electron)
126 print "Pions identified as electrons: %d out of %d" % (count_pion_as_electron, count_true_pion)
127 print "-----------------------------------"
128 print "Pions identified as pions: %d out of %d" % (count_pion_as_pion, count_true_pion)
129 print "Electrons identified as pions: %d out of %d" % (count_electron_as_pion, count_true_electron)
130 print "-----------------------------------"