MCPcopy
hub / github.com/scikit-learn/scikit-learn / transform

Method transform

sklearn/kernel_approximation.py:184–245  ·  view source on GitHub ↗

Generate the feature map approximation for X. Parameters ---------- X : {array-like}, shape (n_samples, n_features) New data, where `n_samples` is the number of samples and `n_features` is the number of features. Returns -------

(self, X)

Source from the content-addressed store, hash-verified

182 return self
183
184 def transform(self, X):
185 """Generate the feature map approximation for X.
186
187 Parameters
188 ----------
189 X : {array-like}, shape (n_samples, n_features)
190 New data, where `n_samples` is the number of samples
191 and `n_features` is the number of features.
192
193 Returns
194 -------
195 X_new : array-like, shape (n_samples, n_components)
196 Returns the instance itself.
197 """
198
199 check_is_fitted(self)
200 X = validate_data(self, X, accept_sparse="csc", reset=False)
201
202 X_gamma = np.sqrt(self.gamma) * X
203
204 if sp.issparse(X_gamma) and self.coef0 != 0:
205 X_gamma = sp.hstack(
206 [X_gamma, np.sqrt(self.coef0) * np.ones((X_gamma.shape[0], 1))],
207 format="csc",
208 )
209
210 elif not sp.issparse(X_gamma) and self.coef0 != 0:
211 X_gamma = np.hstack(
212 [X_gamma, np.sqrt(self.coef0) * np.ones((X_gamma.shape[0], 1))]
213 )
214
215 if X_gamma.shape[1] != self.indexHash_.shape[1]:
216 raise ValueError(
217 "Number of features of test samples does not"
218 " match that of training samples."
219 )
220
221 count_sketches = np.zeros((X_gamma.shape[0], self.degree, self.n_components))
222
223 if sp.issparse(X_gamma):
224 for j in range(X_gamma.shape[1]):
225 for d in range(self.degree):
226 iHashIndex = self.indexHash_[d, j]
227 iHashBit = self.bitHash_[d, j]
228 count_sketches[:, d, iHashIndex] += (
229 (iHashBit * X_gamma[:, [j]]).toarray().ravel()
230 )
231
232 else:
233 for j in range(X_gamma.shape[1]):
234 for d in range(self.degree):
235 iHashIndex = self.indexHash_[d, j]
236 iHashBit = self.bitHash_[d, j]
237 count_sketches[:, d, iHashIndex] += iHashBit * X_gamma[:, j]
238
239 # For each same, compute a count sketch of phi(x) using the polynomial
240 # multiplication (via FFT) of p count sketches of x.
241 count_sketches_fft = fft(count_sketches, axis=2, overwrite_x=True)

Calls 5

check_is_fittedFunction · 0.90
validate_dataFunction · 0.90
fftFunction · 0.90
ifftFunction · 0.90
hstackMethod · 0.45