2015-07-22 15 views
8

Ich habe einen Datenrahmen in Apache Spark mit einem Array von ganzen Zahlen, die Quelle ist eine Reihe von Bildern. Ich möchte schließlich PCA machen, aber ich habe Probleme, nur eine Matrix aus meinen Arrays zu erstellen. Wie erstelle ich eine Matrix von einer RDD?Apache Spark: Wie erstelle ich eine Matrix aus einem DataFrame?

> imagerdd = traindf.map(lambda row: map(float, row.image)) 
> mat = DenseMatrix(numRows=206456, numCols=10, values=imagerdd) 
Traceback (most recent call last): 

    File "<ipython-input-21-6fdaa8cde069>", line 2, in <module> 
mat = DenseMatrix(numRows=206456, numCols=10, values=imagerdd) 

    File "/usr/local/spark/current/python/lib/pyspark.zip/pyspark/mllib/linalg.py", line 815, in __init__ 
values = self._convert_to_array(values, np.float64) 

    File  "/usr/local/spark/current/python/lib/pyspark.zip/pyspark/mllib/linalg.py", line 806, in _convert_to_array 
    return np.asarray(array_like, dtype=dtype) 

    File "/usr/local/python/conda/lib/python2.7/site-  packages/numpy/core/numeric.py", line 462, in asarray 
    return array(a, dtype, copy=False, order=order) 

TypeError: float() argument must be a string or a number 

Ich bekomme die gleichen Fehler aus jeder möglichen Anordnung kann ich mich vorstellen:

imagerdd = traindf.map(lambda row: Vectors.dense(row.image)) 
imagerdd = traindf.map(lambda row: row.image) 
imagerdd = traindf.map(lambda row: np.array(row.image)) 

Wenn ich versuche,

> imagedf = traindf.select("image") 
> mat = DenseMatrix(numRows=206456, numCols=10, values=imagedf) 

Traceback (jüngste Aufforderung zuletzt):

File "<ipython-input-26-a8cbdad10291>", line 2, in <module> 
mat = DenseMatrix(numRows=206456, numCols=10, values=imagedf) 

    File "/usr/local/spark/current/python/lib/pyspark.zip/pyspark/mllib/linalg.py", line 815, in __init__ 
    values = self._convert_to_array(values, np.float64) 

    File "/usr/local/spark/current/python/lib/pyspark.zip/pyspark/mllib/linalg.py", line 806, in _convert_to_array 
    return np.asarray(array_like, dtype=dtype) 

    File "/usr/local/python/conda/lib/python2.7/site-packages/numpy/core/numeric.py", line 462, in asarray 
    return array(a, dtype, copy=False, order=order) 

ValueError: setting an array element with a sequence. 

Antwort

7

Da Sie keine zur Verfügung gestellt haben Beispiel Eingabe Ich nehme an, es sieht mehr oder weniger so aus, wo id eine Zeilennummer ist und image Werte enthält.

traindf = sqlContext.createDataFrame([ 
    (1, [1, 2, 3]), 
    (2, [4, 5, 6]), 
    (3, (7, 8, 9)) 
], ("id", "image")) 

Das erste, was Sie verstehen müssen, ist, dass die DenseMatrix eine Struktur lokalen Daten vorhanden sind. Um genau zu sein ist es ein Wrapper um numpy.ndarray. Derzeit (Spark 1.4.1) gibt es keine verteilten Äquivalente in PySpark MLlib.

Dichte Matrix nehmen drei obligatorische Argumente numRows, numCols, values wo values ist eine lokale Datenstruktur. In Ihrem Fall müssen Sie sammeln erste:

values = (traindf. 
    rdd. 
    map(lambda r: (r.id, r.image)). # Extract row id and data 
    sortByKey(). # Sort by row id 
    flatMap(lambda (id, image): image). 
    collect()) 


ncol = len(traindf.rdd.map(lambda r: r.image).first()) 
nrow = traindf.count() 

dm = DenseMatrix(nrow, ncol, values) 

Endlich:

> print dm.toArray() 
[[ 1. 4. 7.] 
[ 2. 5. 8.] 
[ 3. 6. 9.]] 

bearbeiten:

In Funken 1.5+ können Sie mllib.linalg.distributed wie folgt verwenden:

from pyspark.mllib.linalg.distributed import IndexedRow, IndexedRowMatrix 

mat = IndexedRowMatrix(traindf.map(lambda row: IndexedRow(*row))) 
mat.numRows() 
## 4 
mat.numCols() 
## 3 

obwohl API derzeit noch auf b beschränkt ist In der Praxis nützlich.

+0

Wissen Sie, wie man dasselbe in scala macht? https://stackoverflow.com/questions/47010126/calculate-cosine-simility-spark-dataframe –

Verwandte Themen