MCPcopy Create free account
hub / github.com/Xtra-Computing/thundersvm / fit

Method fit

python/thundersvm/thundersvm.py:91–163  ·  view source on GitHub ↗
(self, X, y)

Source from the content-addressed store, hash-verified

89 thundersvm.model_free(c_void_p(self.model))
90
91 def fit(self, X, y):
92 if self.model is not None:
93 thundersvm.model_free(c_void_p(self.model))
94 self.model = None
95 sparse = sp.isspmatrix(X)
96 self._sparse = sparse and not callable(self.kernel)
97 X, y = check_X_y(X, y, dtype=np.float64, order='C', accept_sparse='csr')
98 y = column_or_1d(y, warn=True).astype(np.float64)
99
100 solver_type = SVM_TYPE.index(self._impl)
101
102 if self.gamma == 'auto':
103 self._gamma = 1.0 / X.shape[1]
104 else:
105 self._gamma = self.gamma
106 if self.kernel not in KERNEL_TYPE:
107 print("The kernel parameter not recognized, please refer to the document.")
108 exit()
109 else:
110 kernel = KERNEL_TYPE.index(self.kernel)
111
112 fit = self._sparse_fit if self._sparse else self._dense_fit
113 thundersvm.model_new.restype = c_void_p
114 self.model = thundersvm.model_new(solver_type)
115 if self.max_mem_size != -1:
116 thundersvm.set_memory_size(c_void_p(self.model), self.max_mem_size)
117 fit(X, y, solver_type, kernel)
118 if self._train_succeed[0] == -1:
119 print("Training failed!")
120 return
121 self.n_sv = thundersvm.n_sv(c_void_p(self.model))
122 csr_row = (c_int * (self.n_sv + 1))()
123 csr_col = (c_int * (self.n_sv * self.n_features))()
124 csr_data = (c_float * (self.n_sv * self.n_features))()
125 data_size = (c_int * 1)()
126 sv_indices = (c_int * self.n_sv)()
127 thundersvm.get_sv(csr_row, csr_col, csr_data, data_size, sv_indices, c_void_p(self.model))
128 self.row = np.frombuffer(csr_row, dtype=np.int32)
129 self.col = np.frombuffer(csr_col, dtype=np.int32)[:data_size[0]]
130 self.data = np.frombuffer(csr_data, dtype=np.float32)[:data_size[0]]
131
132 self.support_vectors_ = sp.csr_matrix((self.data, self.col, self.row))
133 if not self._sparse:
134 self.support_vectors_ = self.support_vectors_.toarray(order='C')
135 self.support_ = np.frombuffer(sv_indices, dtype=np.int32).astype(int)
136
137 dual_coef = (c_float * ((self.n_classes - 1) * self.n_sv))()
138 thundersvm.get_coef(dual_coef, self.n_classes, self.n_sv, c_void_p(self.model))
139
140 self.dual_coef_ = np.frombuffer(dual_coef, dtype=np.float32)\
141 .astype(float)\
142 .reshape((self.n_classes - 1, self.n_sv))
143
144 rho_size = int(self.n_classes * (self.n_classes - 1) / 2)
145 self.n_binary_model = rho_size
146 rho = (c_float * rho_size)()
147 thundersvm.get_rho(rho, rho_size, c_void_p(self.model))
148 self.intercept_ = np.frombuffer(rho, dtype=np.float32).astype(float)

Callers 1

fitMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected