-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-16328][ML][MLLIB][PYSPARK] Add 'asML' and 'fromML' conversion methods to PySpark linalg #13997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
[SPARK-16328][ML][MLLIB][PYSPARK] Add 'asML' and 'fromML' conversion methods to PySpark linalg #13997
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ | |
| import numpy as np | ||
|
|
||
| from pyspark import since | ||
| from pyspark.ml import linalg as newlinalg | ||
| from pyspark.sql.types import UserDefinedType, StructField, StructType, ArrayType, DoubleType, \ | ||
| IntegerType, ByteType, BooleanType | ||
|
|
||
|
|
@@ -247,6 +248,15 @@ def toArray(self): | |
| """ | ||
| raise NotImplementedError | ||
|
|
||
| def asML(self): | ||
| """ | ||
| Convert this vector to the new mllib-local representation. | ||
| This does NOT copy the data; it copies references. | ||
|
|
||
| :return: :py:class:`pyspark.ml.linalg.Vector` | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| class DenseVector(Vector): | ||
| """ | ||
|
|
@@ -408,6 +418,17 @@ def toArray(self): | |
| """ | ||
| return self.array | ||
|
|
||
| def asML(self): | ||
| """ | ||
| Convert this vector to the new mllib-local representation. | ||
| This does NOT copy the data; it copies references. | ||
|
|
||
| :return: :py:class:`pyspark.ml.linalg.DenseVector` | ||
|
|
||
| .. versionadded:: 2.0.0 | ||
| """ | ||
| return newlinalg.DenseVector(self.array) | ||
|
|
||
| @property | ||
| def values(self): | ||
| """ | ||
|
|
@@ -737,6 +758,17 @@ def toArray(self): | |
| arr[self.indices] = self.values | ||
| return arr | ||
|
|
||
| def asML(self): | ||
| """ | ||
| Convert this vector to the new mllib-local representation. | ||
| This does NOT copy the data; it copies references. | ||
|
|
||
| :return: :py:class:`pyspark.ml.linalg.SparseVector` | ||
|
|
||
| .. versionadded:: 2.0.0 | ||
| """ | ||
| return newlinalg.SparseVector(self.size, self.indices, self.values) | ||
|
|
||
| def __len__(self): | ||
| return self.size | ||
|
|
||
|
|
@@ -845,6 +877,24 @@ def dense(*elements): | |
| elements = elements[0] | ||
| return DenseVector(elements) | ||
|
|
||
| @staticmethod | ||
| def fromML(vec): | ||
| """ | ||
| Convert a vector from the new mllib-local representation. | ||
| This does NOT copy the data; it copies references. | ||
|
|
||
| :param vec: a :py:class:`pyspark.ml.linalg.Vector` | ||
| :return: a :py:class:`pyspark.mllib.linalg.Vector` | ||
|
|
||
| .. versionadded:: 2.0.0 | ||
| """ | ||
| if isinstance(vec, newlinalg.DenseVector): | ||
| return DenseVector(vec.array) | ||
| elif isinstance(vec, newlinalg.SparseVector): | ||
| return SparseVector(vec.size, vec.indices, vec.values) | ||
| else: | ||
| raise TypeError("Unsupported vector type %s" % type(vec)) | ||
|
|
||
| @staticmethod | ||
| def stringify(vector): | ||
| """ | ||
|
|
@@ -945,6 +995,13 @@ def toArray(self): | |
| """ | ||
| raise NotImplementedError | ||
|
|
||
| def asML(self): | ||
| """ | ||
| Convert this matrix to the new mllib-local representation. | ||
| This does NOT copy the data; it copies references. | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
| @staticmethod | ||
| def _convert_to_array(array_like, dtype): | ||
| """ | ||
|
|
@@ -1044,6 +1101,17 @@ def toSparse(self): | |
|
|
||
| return SparseMatrix(self.numRows, self.numCols, colPtrs, rowIndices, values) | ||
|
|
||
| def asML(self): | ||
| """ | ||
| Convert this matrix to the new mllib-local representation. | ||
| This does NOT copy the data; it copies references. | ||
|
|
||
| :return: :py:class:`pyspark.ml.linalg.DenseMatrix` | ||
|
|
||
| .. versionadded:: 2.0.0 | ||
| """ | ||
| return newlinalg.DenseMatrix(self.numRows, self.numCols, self.values, self.isTransposed) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "> 79 ;) |
||
|
|
||
| def __getitem__(self, indices): | ||
| i, j = indices | ||
| if i < 0 or i >= self.numRows: | ||
|
|
@@ -1216,6 +1284,18 @@ def toDense(self): | |
| densevals = np.ravel(self.toArray(), order='F') | ||
| return DenseMatrix(self.numRows, self.numCols, densevals) | ||
|
|
||
| def asML(self): | ||
| """ | ||
| Convert this matrix to the new mllib-local representation. | ||
| This does NOT copy the data; it copies references. | ||
|
|
||
| :return: :py:class:`pyspark.ml.linalg.SparseMatrix` | ||
|
|
||
| .. versionadded:: 2.0.0 | ||
| """ | ||
| return newlinalg.SparseMatrix(self.numRows, self.numCols, self.colPtrs, self.rowIndices, | ||
| self.values, self.isTransposed) | ||
|
|
||
| # TODO: More efficient implementation: | ||
| def __eq__(self, other): | ||
| return np.all(self.toArray() == other.toArray()) | ||
|
|
@@ -1236,6 +1316,25 @@ def sparse(numRows, numCols, colPtrs, rowIndices, values): | |
| """ | ||
| return SparseMatrix(numRows, numCols, colPtrs, rowIndices, values) | ||
|
|
||
| @staticmethod | ||
| def fromML(mat): | ||
| """ | ||
| Convert a matrix from the new mllib-local representation. | ||
| This does NOT copy the data; it copies references. | ||
|
|
||
| :param mat: a :py:class:`pyspark.ml.linalg.Matrix` | ||
| :return: a :py:class:`pyspark.mllib.linalg.Matrix` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. versionadded |
||
|
|
||
| .. versionadded:: 2.0.0 | ||
| """ | ||
| if isinstance(mat, newlinalg.DenseMatrix): | ||
| return DenseMatrix(mat.numRows, mat.numCols, mat.values, mat.isTransposed) | ||
| elif isinstance(mat, newlinalg.SparseMatrix): | ||
| return SparseMatrix(mat.numRows, mat.numCols, mat.colPtrs, mat.rowIndices, | ||
| mat.values, mat.isTransposed) | ||
| else: | ||
| raise TypeError("Unsupported matrix type %s" % type(mat)) | ||
|
|
||
|
|
||
| class QRDecomposition(object): | ||
| """ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
versionadded