def pca_transform_daal(pca_result, X, n_components, fit_n_samples, eigenvalues, eigenvectors, whiten=False, scale_eigenvalues=False): fptype = getFPType(X) tr_data = {} tr_data['mean'] = pca_result.dataForTransform['mean'] if whiten: if scale_eigenvalues: tr_data['eigenvalue'] = (fit_n_samples - 1) \ * pca_result.eigenvalues else: tr_data['eigenvalue'] = pca_result.eigenvalues elif scale_eigenvalues: tr_data['eigenvalue'] = np.full((1, pca_result.eigenvalues.size), fit_n_samples - 1, dtype=X.dtype) transform_algorithm = pca_transform(fptype=fptype, nComponents=n_components) transform_result = transform_algorithm.compute(X, pca_result.eigenvectors, tr_data) return transform_result.transformedData
def _transform_daal4py(self, X, whiten=False, scale_eigenvalues=True, check_X=True): check_is_fitted(self) X = check_array(X, dtype=[np.float64, np.float32], force_all_finite=check_X) fpType = getFPType(X) tr_data = dict() if self.mean_ is not None: tr_data['mean'] = self.mean_.reshape((1, -1)) if whiten: if scale_eigenvalues: tr_data['eigenvalue'] = (self.n_samples_ - 1) * self.explained_variance_.reshape((1, -1)) else: tr_data['eigenvalue'] = self.explained_variance_.reshape((1, -1)) elif scale_eigenvalues: tr_data['eigenvalue'] = np.full( (1, self.explained_variance_.shape[0]), self.n_samples_ - 1.0, dtype=X.dtype) if X.shape[1] != self.n_features_: raise ValueError("The number of features of the input data, {}, is not " "equal to the number of features of the training data, {}".format( X.shape[1], self.n_features_)) tr_res = daal4py.pca_transform( fptype=fpType ).compute(X, self.components_, tr_data) return tr_res.transformedData
def compute(data, nComponents): # configure a PCA object and perform PCA pca_algo = d4p.pca(isDeterministic=True, resultsToCompute="mean|variance|eigenvalue") pca_res = pca_algo.compute(data) # Apply transform with whitening because means and eigenvalues are provided pcatrans_algo = d4p.pca_transform(nComponents=nComponents) return pcatrans_algo.compute(data, pca_res.eigenvectors, pca_res.dataForTransform)
def main(readcsv=read_csv, method='svdDense'): dataFileName = "data/batch/pca_transform.csv" nComponents = 2 # read data data = readcsv(dataFileName, range(3)) # configure a PCA object and perform PCA pca_algo = d4p.pca(isDeterministic=True, resultsToCompute="mean|variance|eigenvalue") pca_res = pca_algo.compute(data) # Apply transform with whitening because means and eigenvalues are provided pcatrans_algo = d4p.pca_transform(nComponents=nComponents) pcatrans_res = pcatrans_algo.compute(data, pca_res.eigenvectors, pca_res.dataForTransform) # pca_transform_result objects provides transformedData return (pca_res, pcatrans_res)
def run_pca_daal4py_corr(X, Y): algo = d4p.pca(resultsToCompute="mean|variance|eigenvalue", isDeterministic=True, method="correlationDense") result1 = algo.compute(X) pcatrans_algo = d4p.pca_transform(nComponents=X.shape[1] // 2) transform = pcatrans_algo.compute(X, result1.eigenvectors, result1.dataForTransform).transformedData res = [ transform, result1.eigenvalues, result1.eigenvectors, result1.means, result1.variances ] name = [ "transform", "result1.eigenvalues", "result1.eigenvectors", "result1.means", "result1.variances" ] return res, name
def _transform_daal4py(self, X, whiten=False, scale_eigenvalues=True, check_X=True): if sklearn_check_version('0.22'): check_is_fitted(self) else: check_is_fitted(self, ['mean_', 'components_'], all_or_any=all) if sklearn_check_version("1.0"): self._check_feature_names(X, reset=False) X = check_array(X, dtype=[np.float64, np.float32], force_all_finite=check_X) fpType = getFPType(X) tr_data = dict() if self.mean_ is not None: tr_data['mean'] = self.mean_.reshape((1, -1)) if whiten: if scale_eigenvalues: tr_data['eigenvalue'] = \ (self.n_samples_ - 1) * self.explained_variance_.reshape((1, -1)) else: tr_data['eigenvalue'] = self.explained_variance_.reshape( (1, -1)) elif scale_eigenvalues: tr_data['eigenvalue'] = np.full( (1, self.explained_variance_.shape[0]), self.n_samples_ - 1.0, dtype=X.dtype) if X.shape[1] != self.n_features_: raise ValueError( (f'X has {X.shape[1]} features, ' f'but PCA is expecting {self.n_features_} features as input')) tr_res = daal4py.pca_transform(fptype=fpType).compute( X, self.components_, tr_data) return tr_res.transformedData