MCPcopy Create free account
hub / github.com/makelove/OpenCV-Python-Tutorial / adjust_SVM

Method adjust_SVM

官方samples/digits_adjust.py:73–101  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

71 return ires
72
73 def adjust_SVM(self):
74 Cs = np.logspace(0, 10, 15, base=2)
75 gammas = np.logspace(-7, 4, 15, base=2)
76 scores = np.zeros((len(Cs), len(gammas)))
77 scores[:] = np.nan
78
79 print('adjusting SVM (may take a long time) ...')
80 def f(job):
81 i, j = job
82 samples, labels = self.get_dataset()
83 params = dict(C = Cs[i], gamma=gammas[j])
84 score = cross_validate(SVM, params, samples, labels)
85 return i, j, score
86
87 ires = self.run_jobs(f, np.ndindex(*scores.shape))
88 for count, (i, j, score) in enumerate(ires):
89 scores[i, j] = score
90 print('%d / %d (best error: %.2f %%, last: %.2f %%)' %
91 (count+1, scores.size, np.nanmin(scores)*100, score*100))
92 print(scores)
93
94 print('writing score table to "svm_scores.npz"')
95 np.savez('svm_scores.npz', scores=scores, Cs=Cs, gammas=gammas)
96
97 i, j = np.unravel_index(scores.argmin(), scores.shape)
98 best_params = dict(C = Cs[i], gamma=gammas[j])
99 print('best params:', best_params)
100 print('best error: %.2f %%' % (scores.min()*100))
101 return best_params
102
103 def adjust_KNearest(self):
104 print('adjusting KNearest ...')

Callers 1

digits_adjust.pyFile · 0.80

Calls 1

run_jobsMethod · 0.95

Tested by

no test coverage detected