Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
spark
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cs525-sp18-g07
spark
Commits
04132ea9
Commit
04132ea9
authored
11 years ago
by
Hossein Falaki
Browse files
Options
Downloads
Patches
Plain Diff
Added Rating deserializer
parent
11a93fb5
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala
+8
-1
8 additions, 1 deletion
...spark/mllib/recommendation/MatrixFactorizationModel.scala
python/pyspark/mllib/_common.py
+18
-3
18 additions, 3 deletions
python/pyspark/mllib/_common.py
with
26 additions
and
4 deletions
mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala
+
8
−
1
View file @
04132ea9
...
@@ -67,7 +67,14 @@ class MatrixFactorizationModel(
...
@@ -67,7 +67,14 @@ class MatrixFactorizationModel(
}
}
}
}
def
predictJavaRDD
(
usersProductsJRDD
:
JavaRDD
[
Array
[
Byte
]])
:
JavaRDD
[
Array
[
Byte
]]
=
{
/**
* Predict the rating of many users for many products.
* This is a Java stub for python predictAll()
*
* @param usersProductsJRDD A JavaRDD with serialized tuples (user, product)
* @return JavaRDD of serialized Rating objects.
*/
def
predict
(
usersProductsJRDD
:
JavaRDD
[
Array
[
Byte
]])
:
JavaRDD
[
Array
[
Byte
]]
=
{
val
pythonAPI
=
new
PythonMLLibAPI
()
val
pythonAPI
=
new
PythonMLLibAPI
()
val
usersProducts
=
usersProductsJRDD
.
rdd
.
map
(
xBytes
=>
pythonAPI
.
unpackTuple
(
xBytes
))
val
usersProducts
=
usersProductsJRDD
.
rdd
.
map
(
xBytes
=>
pythonAPI
.
unpackTuple
(
xBytes
))
predict
(
usersProducts
).
map
(
rate
=>
pythonAPI
.
serializeRating
(
rate
))
predict
(
usersProducts
).
map
(
rate
=>
pythonAPI
.
serializeRating
(
rate
))
...
...
This diff is collapsed.
Click to expand it.
python/pyspark/mllib/_common.py
+
18
−
3
View file @
04132ea9
...
@@ -18,6 +18,9 @@
...
@@ -18,6 +18,9 @@
from
numpy
import
ndarray
,
copyto
,
float64
,
int64
,
int32
,
ones
,
array_equal
,
array
,
dot
,
shape
from
numpy
import
ndarray
,
copyto
,
float64
,
int64
,
int32
,
ones
,
array_equal
,
array
,
dot
,
shape
from
pyspark
import
SparkContext
from
pyspark
import
SparkContext
from
pyspark.serializers
import
Serializer
import
struct
# Double vector format:
# Double vector format:
#
#
# [8-byte 1] [8-byte length] [length*8 bytes of data]
# [8-byte 1] [8-byte length] [length*8 bytes of data]
...
@@ -213,9 +216,21 @@ def _serialize_rating(r):
...
@@ -213,9 +216,21 @@ def _serialize_rating(r):
intpart
[
0
],
intpart
[
1
],
doublepart
[
0
]
=
r
intpart
[
0
],
intpart
[
1
],
doublepart
[
0
]
=
r
return
ba
return
ba
def
_deserialize_rating
(
ba
):
class
RatingDeserializer
(
Serializer
):
ar
=
ndarray
(
shape
=
(
3
,
),
buffer
=
ba
,
dtype
=
"
float64
"
,
order
=
'
C
'
)
def
loads
(
self
,
stream
):
return
ar
.
copy
()
length
=
struct
.
unpack
(
"
!i
"
,
stream
.
read
(
4
))[
0
]
ba
=
stream
.
read
(
length
)
res
=
ndarray
(
shape
=
(
3
,
),
buffer
=
ba
,
dtype
=
"
float64
"
,
offset
=
4
)
return
int
(
res
[
0
]),
int
(
res
[
1
]),
res
[
2
]
def
load_stream
(
self
,
stream
):
while
True
:
try
:
yield
self
.
loads
(
stream
)
except
struct
.
error
:
return
except
EOFError
:
return
def
_serialize_tuple
(
t
):
def
_serialize_tuple
(
t
):
ba
=
bytearray
(
8
)
ba
=
bytearray
(
8
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment