Подтвердить что ты не робот

Pyspark и PCA: Как я могу извлечь собственные векторы этого СПС? Как я могу рассчитать, сколько объяснений они объясняют?

Я уменьшаю размерность a Spark DataFrame с помощью модели PCA с pyspark (используя библиотеку spark ml) следующим образом:

pca = PCA(k=3, inputCol="features", outputCol="pca_features")
model = pca.fit(data)

где data - это Spark DataFrame с одним столбцом, который находится под напряжением features, который является DenseVector трех измерений:

data.take(1)
Row(features=DenseVector([0.4536,-0.43218, 0.9876]), label=u'class1')

После установки я преобразую данные:

transformed = model.transform(data)
transformed.first()
Row(features=DenseVector([0.4536,-0.43218, 0.9876]), label=u'class1', pca_features=DenseVector([-0.33256, 0.8668, 0.625]))

Мой вопрос: как я могу извлечь собственные векторы этого СПС? Как я могу рассчитать, сколько объяснений они объясняют?

4b9b3361

Ответ 1

Ну, это кажется невероятным, но на самом деле нет способа извлечь такую ​​информацию из разложения PCA (по крайней мере, от Spark 1.5). Но опять же, было много подобных "жалоб" - см. здесь, например, за невозможность извлечь лучшие параметры из CrossValidatorModel.

К счастью, несколько месяцев назад я посетил "Масштабируемое машинное обучение" MOOC by AMPLab (Berkeley) и Databricks, т.е. создатели Spark, где мы выполнили полный протокол PCA "вручную" в рамках домашних заданий. Я изменил свои функции с тех пор (будьте уверены, я получил полный кредит:-), чтобы работать с dataframes как входы (вместо RDD), того же формата, что и ваш (т.е. строки DenseVectors, содержащие числовые особенности).

Сначала нам нужно определить промежуточную функцию estimatedCovariance следующим образом:

import numpy as np

def estimateCovariance(df):
    """Compute the covariance matrix for a given dataframe.

    Note:
        The multi-dimensional covariance array should be calculated using outer products.  Don't
        forget to normalize the data by first subtracting the mean.

    Args:
        df:  A Spark dataframe with a column named 'features', which (column) consists of DenseVectors.

    Returns:
        np.ndarray: A multi-dimensional array where the number of rows and columns both equal the
            length of the arrays in the input dataframe.
    """
    m = df.select(df['features']).map(lambda x: x[0]).mean()
    dfZeroMean = df.select(df['features']).map(lambda x:   x[0]).map(lambda x: x-m)  # subtract the mean

    return dfZeroMean.map(lambda x: np.outer(x,x)).sum()/df.count()

Затем мы можем написать основную функцию pca следующим образом:

from numpy.linalg import eigh

def pca(df, k=2):
    """Computes the top `k` principal components, corresponding scores, and all eigenvalues.

    Note:
        All eigenvalues should be returned in sorted order (largest to smallest). `eigh` returns
        each eigenvectors as a column.  This function should also return eigenvectors as columns.

    Args:
        df: A Spark dataframe with a 'features' column, which (column) consists of DenseVectors.
        k (int): The number of principal components to return.

    Returns:
        tuple of (np.ndarray, RDD of np.ndarray, np.ndarray): A tuple of (eigenvectors, `RDD` of
        scores, eigenvalues).  Eigenvectors is a multi-dimensional array where the number of
        rows equals the length of the arrays in the input `RDD` and the number of columns equals
        `k`.  The `RDD` of scores has the same number of rows as `data` and consists of arrays
        of length `k`.  Eigenvalues is an array of length d (the number of features).
     """
    cov = estimateCovariance(df)
    col = cov.shape[1]
    eigVals, eigVecs = eigh(cov)
    inds = np.argsort(eigVals)
    eigVecs = eigVecs.T[inds[-1:-(col+1):-1]]  
    components = eigVecs[0:k]
    eigVals = eigVals[inds[-1:-(col+1):-1]]  # sort eigenvals
    score = df.select(df['features']).map(lambda x: x[0]).map(lambda x: np.dot(x, components.T) )
    # Return the `k` principal components, `k` scores, and all eigenvalues

    return components.T, score, eigVals

Test

Посмотрите сначала результаты с существующим методом, используя данные примера из документации Spark ML PCA (модифицируя их так, чтобы быть всеми DenseVectors):

 from pyspark.ml.feature import *
 from pyspark.mllib.linalg import Vectors
 data = [(Vectors.dense([0.0, 1.0, 0.0, 7.0, 0.0]),),
         (Vectors.dense([2.0, 0.0, 3.0, 4.0, 5.0]),),
         (Vectors.dense([4.0, 0.0, 0.0, 6.0, 7.0]),)]
 df = sqlContext.createDataFrame(data,["features"])
 pca_extracted = PCA(k=2, inputCol="features", outputCol="pca_features")
 model = pca_extracted.fit(df)
 model.transform(df).collect()

 [Row(features=DenseVector([0.0, 1.0, 0.0, 7.0, 0.0]), pca_features=DenseVector([1.6486, -4.0133])),
  Row(features=DenseVector([2.0, 0.0, 3.0, 4.0, 5.0]), pca_features=DenseVector([-4.6451, -1.1168])),
  Row(features=DenseVector([4.0, 0.0, 0.0, 6.0, 7.0]), pca_features=DenseVector([-6.4289, -5.338]))]

Затем с помощью нашего метода:

 comp, score, eigVals = pca(df)
 score.collect()

 [array([ 1.64857282,  4.0132827 ]),
  array([-4.64510433,  1.11679727]),
  array([-6.42888054,  5.33795143])]

Подчеркнем, что мы не используем любые collect() методы в определенных нами функциях - score - это RDD, как и должно быть.

Обратите внимание, что знаки нашего второго столбца противоположны признакам, полученным из существующего метода; но это не проблема: согласно (свободно загружаемому) Введение в статистическое обучение, в соавторстве с Хасти и Тиббирани, стр. 382

Каждый вектор загрузки основного компонента уникален, вплоть до знака. Эта означает, что два разных пакета программного обеспечения приведут к тому же принципу компонентные векторы загрузки, хотя признаки тех нагрузочных векторов может отличаться. Знаки могут различаться, поскольку каждая загрузка основного компонента вектор задает направление в p-мерном пространстве: переворачивание знака не имеет эффект, так как направление не меняется. [...] Точно так же векторы оценки уникальны до знака, так как дисперсия Z совпадает с дисперсией -Z.

Наконец, теперь, когда у нас есть собственные значения, тривиально написать функцию для процента объясненной дисперсии:

 def varianceExplained(df, k=1):
     """Calculate the fraction of variance explained by the top `k` eigenvectors.

     Args:
         df: A Spark dataframe with a 'features' column, which (column) consists of DenseVectors.
         k: The number of principal components to consider.

     Returns:
         float: A number between 0 and 1 representing the percentage of variance explained
             by the top `k` eigenvectors.
     """
     components, scores, eigenvalues = pca(df, k)  
     return sum(eigenvalues[0:k])/sum(eigenvalues)


 varianceExplained(df,1)
 # 0.79439325322305299

В качестве теста мы также проверяем, объясняется ли в нашем примере дисперсия 1,0, при k = 5 (поскольку исходные данные 5-мерные):

 varianceExplained(df,5)
 # 1.0

Это должно выполнять вашу работу эффективно; не стесняйтесь придумывать какие-либо разъяснения, которые могут вам понадобиться.

[Разработано и протестировано с помощью Spark 1.5.0 и 1.5.1]

Ответ 2

EDIT:

PCA и SVD, наконец, доступны в pyspark, начиная искра 2.2.0 в соответствии с этим разрешенным билетом JIRA SPARK-6227.

Оригинальный ответ:

Ответ, данный @desertnaut, на самом деле отлично с теоретической точки зрения, но я хотел представить другой подход к тому, как вычислить SVD и извлечь тогда собственные векторы.

from pyspark.mllib.common import callMLlibFunc, JavaModelWrapper
from pyspark.mllib.linalg.distributed import RowMatrix

class SVD(JavaModelWrapper):
    """Wrapper around the SVD scala case class"""
    @property
    def U(self):
        """ Returns a RowMatrix whose columns are the left singular vectors of the SVD if computeU was set to be True."""
        u = self.call("U")
        if u is not None:
        return RowMatrix(u)

    @property
    def s(self):
        """Returns a DenseVector with singular values in descending order."""
        return self.call("s")

    @property
    def V(self):
        """ Returns a DenseMatrix whose columns are the right singular vectors of the SVD."""
        return self.call("V")

Это определяет наш объект SVD. Теперь мы можем определить наш метод computeSVD с помощью Java Wrapper.

def computeSVD(row_matrix, k, computeU=False, rCond=1e-9):
    """
    Computes the singular value decomposition of the RowMatrix.
    The given row matrix A of dimension (m X n) is decomposed into U * s * V'T where
    * s: DenseVector consisting of square root of the eigenvalues (singular values) in descending order.
    * U: (m X k) (left singular vectors) is a RowMatrix whose columns are the eigenvectors of (A X A')
    * v: (n X k) (right singular vectors) is a Matrix whose columns are the eigenvectors of (A' X A)
    :param k: number of singular values to keep. We might return less than k if there are numerically zero singular values.
    :param computeU: Whether of not to compute U. If set to be True, then U is computed by A * V * sigma^-1
    :param rCond: the reciprocal condition number. All singular values smaller than rCond * sigma(0) are treated as zero, where sigma(0) is the largest singular value.
    :returns: SVD object
    """
    java_model = row_matrix._java_matrix_wrapper.call("computeSVD", int(k), computeU, float(rCond))
    return SVD(java_model)

Теперь применим это к примеру:

from pyspark.ml.feature import *
from pyspark.mllib.linalg import Vectors

data = [(Vectors.dense([0.0, 1.0, 0.0, 7.0, 0.0]),), (Vectors.dense([2.0, 0.0, 3.0, 4.0, 5.0]),), (Vectors.dense([4.0, 0.0, 0.0, 6.0, 7.0]),)]
df = sqlContext.createDataFrame(data,["features"])

pca_extracted = PCA(k=2, inputCol="features", outputCol="pca_features")

model = pca_extracted.fit(df)
features = model.transform(df) # this create a DataFrame with the regular features and pca_features

# We can now extract the pca_features to prepare our RowMatrix.
pca_features = features.select("pca_features").rdd.map(lambda row : row[0])
mat = RowMatrix(pca_features)

# Once the RowMatrix is ready we can compute our Singular Value Decomposition
svd = computeSVD(mat,2,True)
svd.s
# DenseVector([9.491, 4.6253])
svd.U.rows.collect()
# [DenseVector([0.1129, -0.909]), DenseVector([0.463, 0.4055]), DenseVector([0.8792, -0.0968])]
svd.V
# DenseMatrix(2, 2, [-0.8025, -0.5967, -0.5967, 0.8025], 0)

Ответ 3

Самый простой ответ на ваш вопрос - ввести в свою модель идентификационную матрицу.

identity_input = [(Vectors.dense([1.0, .0, 0.0, .0, 0.0]),),(Vectors.dense([.0, 1.0, .0, .0, .0]),), \
              (Vectors.dense([.0, 0.0, 1.0, .0, .0]),),(Vectors.dense([.0, 0.0, .0, 1.0, .0]),),
              (Vectors.dense([.0, 0.0, .0, .0, 1.0]),)]
df_identity = sqlContext.createDataFrame(identity_input,["features"])
identity_features = model.transform(df_identity)

Это должно дать вам основные компоненты.

Я думаю, что ответ eliasah лучше с точки зрения инфраструктуры Spark, потому что desertnaut решает проблему, используя функции numpy вместо действий Spark. Однако ответ eliasah отсутствует, нормализуя данные. Итак, я добавлю следующие строки для ответа eliasah:

from pyspark.ml.feature import StandardScaler
standardizer = StandardScaler(withMean=True, withStd=False,
                          inputCol='features',
                          outputCol='std_features')
model = standardizer.fit(df)
output = model.transform(df)
pca_features = output.select("std_features").rdd.map(lambda row : row[0])
mat = RowMatrix(pca_features)
svd = computeSVD(mat,5,True)

Фактически, svd.V и identity_features.select( "pca_features" ). collect() должны иметь одинаковые значения.

Изменить: я обобщил PCA и его использование в Spark и sklearn в этом здесь

Ответ 4

В искрах 2.2+ теперь вы можете легко получить объясненную дисперсию как:

from pyspark.ml.feature import VectorAssembler
assembler = VectorAssembler(inputCols=<columns of your original dataframe>, outputCol="features")
df = assembler.transform(<your original dataframe>).select("features")
from pyspark.ml.feature import PCA
pca = PCA(k=10, inputCol="features", outputCol="pcaFeatures")
model = pca.fit(df)
sum(model.explainedVariance)