From 60a95d6a5321bd2186b1acaea9e4e765c6d6e2fd Mon Sep 17 00:00:00 2001 From: Fu Zhixiang <52901066+cugfzx@users.noreply.github.com> Date: Tue, 19 Apr 2022 02:42:10 +0800 Subject: [PATCH 1/3] Update rotation_forest.py I removed the loop on line 75. Each rotation tree only extracts features once. There should not be repeated sampling of features --- rotation_forest/rotation_forest.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/rotation_forest/rotation_forest.py b/rotation_forest/rotation_forest.py index 8ed610e..1ee0d8a 100644 --- a/rotation_forest/rotation_forest.py +++ b/rotation_forest/rotation_forest.py @@ -71,15 +71,13 @@ def _fit_rotation_matrix(self, X): n_samples, n_features = X.shape self.rotation_matrix = np.zeros((n_features, n_features), dtype=np.float32) - for i, subset in enumerate( - random_feature_subsets(X, self.n_features_per_subset, - random_state=self.random_state)): - # take a 75% bootstrap from the rows - x_sample = resample(X, n_samples=int(n_samples*0.75), - random_state=10*i) - pca = self.pca_algorithm() - pca.fit(x_sample[:, subset]) - self.rotation_matrix[np.ix_(subset, subset)] = pca.components_ + + subset = random_feature_subsets(X, self.n_features_per_subset, random_state=self.random_state) + # take a 75% bootstrap from the rows + x_sample = resample(X, n_samples=int(n_samples*0.75),random_state=10) + pca = self.pca_algorithm() + pca.fit(x_sample[:, subset]) + self.rotation_matrix[np.ix_(subset, subset)] = pca.components_ def fit(self, X, y, sample_weight=None, check_input=True): self._fit_rotation_matrix(X) From 7f37e269b4ebe98db91dc3087e9ede6d62f5d2f1 Mon Sep 17 00:00:00 2001 From: Fu Zhixiang <52901066+cugfzx@users.noreply.github.com> Date: Wed, 20 Apr 2022 01:34:38 +0800 Subject: [PATCH 2/3] Update rotation_forest.py --- rotation_forest/rotation_forest.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rotation_forest/rotation_forest.py b/rotation_forest/rotation_forest.py index 1ee0d8a..b0ce98e 100644 --- a/rotation_forest/rotation_forest.py +++ b/rotation_forest/rotation_forest.py @@ -2,7 +2,7 @@ from sklearn.tree import DecisionTreeClassifier from sklearn.tree._tree import DTYPE -from sklearn.ensemble.forest import ForestClassifier +from sklearn.ensemble._forest import ForestClassifier from sklearn.utils import resample, gen_batches, check_random_state from sklearn.utils.extmath import fast_dot from sklearn.decomposition import PCA, RandomizedPCA @@ -12,7 +12,7 @@ def random_feature_subsets(array, batch_size, random_state=1234): """ Generate K subsets of the features in X """ random_state = check_random_state(random_state) - features = range(array.shape[1]) + features = list(range(array.shape[1])) random_state.shuffle(features) for batch in gen_batches(len(features), batch_size): yield features[batch] @@ -72,7 +72,8 @@ def _fit_rotation_matrix(self, X): self.rotation_matrix = np.zeros((n_features, n_features), dtype=np.float32) - subset = random_feature_subsets(X, self.n_features_per_subset, random_state=self.random_state) + random_feature=random_feature_subsets(X, self.n_features_per_subset,random_state=self.random_state) + subset=next(random_feature) # take a 75% bootstrap from the rows x_sample = resample(X, n_samples=int(n_samples*0.75),random_state=10) pca = self.pca_algorithm() From 95bf36f557e72ee82d4671b3e9de2cfb00492f29 Mon Sep 17 00:00:00 2001 From: Fu Zhixiang <52901066+cugfzx@users.noreply.github.com> Date: Wed, 27 Apr 2022 00:02:24 +0800 Subject: [PATCH 3/3] fixed a essential but hidden mistake about math In line 81, the pca.components_ is the V that linalg.svd() returns and a row-vector, so it should be transposed --- rotation_forest/rotation_forest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rotation_forest/rotation_forest.py b/rotation_forest/rotation_forest.py index b0ce98e..b6f2bb4 100644 --- a/rotation_forest/rotation_forest.py +++ b/rotation_forest/rotation_forest.py @@ -78,7 +78,7 @@ def _fit_rotation_matrix(self, X): x_sample = resample(X, n_samples=int(n_samples*0.75),random_state=10) pca = self.pca_algorithm() pca.fit(x_sample[:, subset]) - self.rotation_matrix[np.ix_(subset, subset)] = pca.components_ + self.rotation_matrix[np.ix_(subset, subset)] = pca.components_.T #The pca.components_ is the V that linalg.svd() returns and a row-vector,so it should be transposed def fit(self, X, y, sample_weight=None, check_input=True): self._fit_rotation_matrix(X)