LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
learn-grad.py
Go to the documentation of this file.
1 import numpy as np
2 from sklearn import manifold, svm, preprocessing
3 import pylab as pl
4 from mpl_toolkits.mplot3d import Axes3D
5 from matplotlib.ticker import NullFormatter
6 Axes3D
7 def read_data(gradname, scorename):
8  #could use numpy.loadtxt, but that blindly accepts nan values
9  slist = open(scorename, 'r').readlines()
10  glist = open(gradname, 'r').readlines()
11  v = (slist, glist)
12  gkeep = []
13  skeep = []
14  for s, g in zip(slist, glist):
15  sval = float(s.split()[0])
16  if(np.isfinite(sval)):
17  skeep.append(np.fromstring(s, sep=' '))
18  nar = np.fromstring(g, sep=' ')
19  gkeep.append(nar)
20  return (np.vstack(skeep), np.vstack(gkeep))
21 
22 
23 (scores, grads) = read_data("grad_data.out", "score_data.out")
24 (scores2, grads2) = read_data("grad_data2.out", "score_data2.out")
25 n_neighbors=50
26 n_components=15
27 scaler =preprocessing.StandardScaler().fit(grads);
28 
29 grads = scaler.transform(grads)
30 grads2 = scaler.transform(grads2)
31 #Y = manifold.Isomap(n_neighbors, n_components).fit_transform(grads)
32 Y=grads #don't perform dimensionality reduction
33 
34 #perform classification on the dataset, use support vector machine
35 test_svm = svm.SVC(kernel='rbf', C=20, gamma=0.15, probability=True)
36 test_svm=test_svm.fit(Y, scores[:, 1])
37 
38 #get number of stable classified as unstable
39 
40 pl.figure(0)
41 (Y, scores) = grads2, scores2
42 num_stab = sum(1 for s in scores if s[1] == 1)
43 missed_stab_arr = [s[0] for t, s, in zip(Y, scores) if s[1]==1 and test_svm.predict(t) != s[1]]
44 pl.hist(missed_stab_arr, bins=20)
45 pl.title("Frequency of scores for the misclassified stable solutions")
46 pl.xlabel("score")
47 missed_stab = len(missed_stab_arr)
48 ave_score = sum(missed_stab_arr)/len(missed_stab_arr);
49 max_score = max(missed_stab_arr)
50 missed_stab_arr = [s[0] for t, s, in zip(Y, scores) if s[1]==1 and test_svm.predict(t) == s[1]]
51 pl.figure(1)
52 pl.hist(missed_stab_arr, bins=50, color='green')
53 pl.title("Frequency of scores for the correctly classified stable solutions")
54 pl.xlabel("score")
55 ave_good_score = sum(missed_stab_arr)/len(missed_stab_arr);
56 max_good_score = max(missed_stab_arr)
57 
58 print "%d out of %d stable solutions were missclassified" %(missed_stab, num_stab)
59 print "Average score of the missclassified stable variables is %f, maximum score is %f" % (ave_score, max_score)
60 print "Average score of the classified stable variables is %f, maximum score is %f" % (ave_good_score, max_good_score)
61 #calculate average probability of misclassified stable variantsdd
62 #calculate number of missed unstable variants
63 num_unstab = sum(1 for s in scores if s[1] == 0)
64 missed_unstab = sum(1 for t, s in zip(Y, scores) if s[1] == 0 and test_svm.predict(t) != s[1])
65 print "%d out of %d unstable solutions were missclassified" %(missed_unstab, num_unstab)
66 pl.legend()
67 pl.show()
68 """
69 fig = pl.figure(figsize=(15, 8))
70 pl.suptitle("Manifold Learning with %i points, %i neighbors"
71  % (grads.shape[1], n_neighbors), fontsize=14)
72 
73 #perform clustering
74 # compatibility matplotlib < 1.0
75 ax = fig.add_subplot(241, projection='3d')
76 ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], c=scores[:, 0], cmap=pl.cm.Spectral)
77 ax.view_init(4, -72)
78 pl.show()
79 #pl.scatter(Y[:, 0], Y[:, 1], Y[:, 2], c=scores, cmap=pl.cm.Spectral)
80 """