<commit_before>from __future__ import division

from sklearn.decomposition.pca import PCA, _infer_dimension_
from ut.ml.sk.utils.validation import weighted_data
from wpca import WPCA
from numpy import reshape, tile

from numpy import reshape, sqrt, average

import numpy as np
from scipy import linalg
# from scipy.special import gammaln

from sklearn.utils.validation import as_float_array
from sklearn.utils.validation import check_array
# from sklearn.utils.extmath import fast_dot, fast_logdet, randomized_svd
# from sklearn.utils.validation import check_is_fitted

__author__ = 'thor'


class WeightedPCA(WPCA):
    """
    Weighted version of sklearn.decomposition.pca.PCA
>>> from sklearn.decomposition.pca import PCA
>>> from ut.ml.sk.decomposition.pca import WeightedPCA
>>> from sklearn.datasets import make_blobs
>>> from ut.ml.sk.preprocessing import WeightedStandardScaler
>>> from numpy import ones, vstack, hstack, random
>>> from ut.ml.sk.utils.validation import compare_model_attributes, repeat_rows
>>>
>>> model_1 = PCA()
>>> model_2 = WeightedPCA()
>>>
>>> X, y = make_blobs(100, 5, 4)
>>> w = ones(len(X))
>>> compare_model_attributes(model_1.fit(X), model_2.fit(X))
all fitted attributes were close
>>>
>>> X, y = make_blobs(100, 5, 4)
>>> w = ones(len(X))
>>>
>>> XX = vstack((X, X))
>>> wX = (X, 2 * ones(len(X)))
>>> compare_model_attributes(model_1.fit(XX), model_2.fit(wX))
all fitted attributes were close
>>>
>>> X, y = make_blobs(100, 5, 4)
>>> w = ones(len(X))
>>>
>>> XX = vstack((X, X[-2:, :], X[-1, :]))
>>> wX = (X, hstack((ones(len(X)-2), [2, 3])))
>>> compare_model_attributes(model_1.fit(XX), model_2.fit(wX))
all fitted attributes were close
>>>
>>> w = random.randint(1, 5, len(X))
>>> compare_model_attributes(model_1.fit(repeat_rows(X, w)), model_2.fit((X, w)))
all fitted attributes were close
    """
    def fit(self, X, y=None):
        X, w = weighted_data(X)
        return super(self.__class__, self).fit(X, weights=tile(reshape(w, (len(w), 1)), X.shape[1]))


class MyWeightedPCA(PCA):
    def _fit(self, X):
        X, w = weighted_data(X)

        X = check_array(X)
        n_samples, n_features = X.shape
        n_samples_weighted = sum(w)
        X = as_float_array(X, copy=self.copy)
        # Center data
        # self.mean_ = average(X, axis=0, weights=w)
        # X -= self.mean_
        # U, S, V = linalg.svd((X.T * reshape(sqrt(w), (1, len(X)))).T, full_matrices=True)
        U, S, V = linalg.svd((X.T * reshape(sqrt(w), (1, len(X)))).T, full_matrices=False)
        # ww = tile(reshape(w, (len(w), 1)), X.shape[1])
        explained_variance_ = (S ** 2) / n_samples_weighted
        explained_variance_ratio_ = (explained_variance_ /
                                     explained_variance_.sum())

        components_ = V

        n_components = self.n_components<commit_msg>Trying out the older version of weighted PCA, again.<commit_after>from __future__ import division

from sklearn.decomposition.pca import PCA, _infer_dimension_
from ut.ml.sk.utils.validation import weighted_data
from wpca import WPCA
from numpy import reshape, tile

from numpy import reshape, sqrt, average

import numpy as np
from scipy import linalg
# from scipy.special import gammaln

from sklearn.utils.validation import as_float_array
from sklearn.utils.validation import check_array
# from sklearn.utils.extmath import fast_dot, fast_logdet, randomized_svd
# from sklearn.utils.validation import check_is_fitted

__author__ = 'thor'

#
# class WeightedPCA(WPCA):
#     """
#     Weighted version of sklearn.decomposition.pca.PCA
# >>> from sklearn.decomposition.pca import PCA
# >>> from ut.ml.sk.decomposition.pca import WeightedPCA
# >>> from sklearn.datasets import make_blobs
# >>> from ut.ml.sk.preprocessing import WeightedStandardScaler
# >>> from numpy import ones, vstack, hstack, random
# >>> from ut.ml.sk.utils.validation import compare_model_attributes, repeat_rows
# >>>
# >>> model_1 = PCA()
# >>> model_2 = WeightedPCA()
# >>>
# >>> X, y = make_blobs(100, 5, 4)
# >>> w = ones(len(X))
# >>> compare_model_attributes(model_1.fit(X), model_2.fit(X))
# all fitted attributes were close
# >>>
# >>> X, y = make_blobs(100, 5, 4)
# >>> w = ones(len(X))
# >>>
# >>> XX = vstack((X, X))
# >>> wX = (X, 2 * ones(len(X)))
# >>> compare_model_attributes(model_1.fit(XX), model_2.fit(wX))
# all fitted attributes were close
# >>>
# >>> X, y = make_blobs(100, 5, 4)
# >>> w = ones(len(X))
# >>>
# >>> XX = vstack((X, X[-2:, :], X[-1, :]))
# >>> wX = (X, hstack((ones(len(X)-2), [2, 3])))
# >>> compare_model_attributes(model_1.fit(XX), model_2.fit(wX))
# all fitted attributes were close
# >>>
# >>> w = random.randint(1, 5, len(X))
# >>> compare_model_attributes(model_1.fit(repeat_rows(X, w)), model_2.fit((X, w)))
# all fitted attributes were close
#     """
#     def fit(self, X, y=None):
#         X, w = weighted_data(X)
#         return super(self.__class__, self).fit(X, weights=tile(reshape(w, (len(w), 1)), X.shape[1]))
#

class MyWeightedPCA(PCA):
    def _fit(self, X):
        X, w = weighted_data(X)

        X = check_array(X)
        n_samples, n_features = X.shape
        n_samples_weighted = sum(w)
        X = as_float_array(X, copy=self.copy)
        # Center data
        # self.mean_ = average(X, axis=0, weights=w)
        # X -= self.mean_
        U, S, V = linalg.svd((X.T * reshape(sqrt(w), (1, len(X)))).T, full_matrices=True)
        explained_variance_ = (S ** 2) / n_samples_weighted
        explained_variance_ratio_ = (explained_variance_ /
                                     explained_variance_.sum())

        components_ = V

        n_components = self.n_components
{'fraction_non_alphanumeric': 0.1567414737586146, 'fraction_numerical': 0.013960063615479766, 'mean_word_length': 3.7844463229078613, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 183, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2236340', 'n_tokens_mistral': 2088, 'n_tokens_neox': 2003, 'n_words': 476}