2013-12-20 01:55:03 -05:00
|
|
|
#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
2013-12-25 00:53:48 -05:00
|
|
|
from numpy import ndarray, copyto, float64, int64, int32, ones, array_equal, array, dot, shape
|
2013-12-20 01:33:32 -05:00
|
|
|
from pyspark import SparkContext
|
2013-12-19 01:22:18 -05:00
|
|
|
|
|
|
|
# Double vector format:
|
|
|
|
#
|
|
|
|
# [8-byte 1] [8-byte length] [length*8 bytes of data]
|
|
|
|
#
|
|
|
|
# Double matrix format:
|
|
|
|
#
|
|
|
|
# [8-byte 2] [8-byte rows] [8-byte cols] [rows*cols*8 bytes of data]
|
2013-12-20 01:33:32 -05:00
|
|
|
#
|
2013-12-19 01:22:18 -05:00
|
|
|
# This is all in machine-endian. That means that the Java interpreter and the
|
|
|
|
# Python interpreter must agree on what endian the machine is.
|
|
|
|
|
2013-12-19 03:40:57 -05:00
|
|
|
def _deserialize_byte_array(shape, ba, offset):
|
2013-12-20 01:33:32 -05:00
|
|
|
"""Wrapper around ndarray aliasing hack.
|
|
|
|
|
|
|
|
>>> x = array([1.0, 2.0, 3.0, 4.0, 5.0])
|
|
|
|
>>> array_equal(x, _deserialize_byte_array(x.shape, x.data, 0))
|
|
|
|
True
|
|
|
|
>>> x = array([1.0, 2.0, 3.0, 4.0]).reshape(2,2)
|
|
|
|
>>> array_equal(x, _deserialize_byte_array(x.shape, x.data, 0))
|
|
|
|
True
|
|
|
|
"""
|
2013-12-19 03:40:57 -05:00
|
|
|
ar = ndarray(shape=shape, buffer=ba, offset=offset, dtype="float64",
|
|
|
|
order='C')
|
|
|
|
return ar.copy()
|
2013-12-19 01:22:18 -05:00
|
|
|
|
2013-12-19 03:40:57 -05:00
|
|
|
def _serialize_double_vector(v):
|
2013-12-20 01:48:44 -05:00
|
|
|
"""Serialize a double vector into a mutually understood format."""
|
2013-12-20 01:33:32 -05:00
|
|
|
if type(v) != ndarray:
|
|
|
|
raise TypeError("_serialize_double_vector called on a %s; "
|
|
|
|
"wanted ndarray" % type(v))
|
|
|
|
if v.dtype != float64:
|
|
|
|
raise TypeError("_serialize_double_vector called on an ndarray of %s; "
|
|
|
|
"wanted ndarray of float64" % v.dtype)
|
|
|
|
if v.ndim != 1:
|
|
|
|
raise TypeError("_serialize_double_vector called on a %ddarray; "
|
|
|
|
"wanted a 1darray" % v.ndim)
|
|
|
|
length = v.shape[0]
|
|
|
|
ba = bytearray(16 + 8*length)
|
|
|
|
header = ndarray(shape=[2], buffer=ba, dtype="int64")
|
|
|
|
header[0] = 1
|
|
|
|
header[1] = length
|
|
|
|
copyto(ndarray(shape=[length], buffer=ba, offset=16,
|
|
|
|
dtype="float64"), v)
|
|
|
|
return ba
|
2013-12-19 01:22:18 -05:00
|
|
|
|
2013-12-19 03:40:57 -05:00
|
|
|
def _deserialize_double_vector(ba):
|
2013-12-20 01:33:32 -05:00
|
|
|
"""Deserialize a double vector from a mutually understood format.
|
|
|
|
|
|
|
|
>>> x = array([1.0, 2.0, 3.0, 4.0, -1.0, 0.0, -0.0])
|
|
|
|
>>> array_equal(x, _deserialize_double_vector(_serialize_double_vector(x)))
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
if type(ba) != bytearray:
|
|
|
|
raise TypeError("_deserialize_double_vector called on a %s; "
|
|
|
|
"wanted bytearray" % type(ba))
|
|
|
|
if len(ba) < 16:
|
|
|
|
raise TypeError("_deserialize_double_vector called on a %d-byte array, "
|
|
|
|
"which is too short" % len(ba))
|
|
|
|
if (len(ba) & 7) != 0:
|
|
|
|
raise TypeError("_deserialize_double_vector called on a %d-byte array, "
|
|
|
|
"which is not a multiple of 8" % len(ba))
|
|
|
|
header = ndarray(shape=[2], buffer=ba, dtype="int64")
|
|
|
|
if header[0] != 1:
|
|
|
|
raise TypeError("_deserialize_double_vector called on bytearray "
|
|
|
|
"with wrong magic")
|
|
|
|
length = header[1]
|
|
|
|
if len(ba) != 8*length + 16:
|
|
|
|
raise TypeError("_deserialize_double_vector called on bytearray "
|
|
|
|
"with wrong length")
|
|
|
|
return _deserialize_byte_array([length], ba, 16)
|
2013-12-19 01:22:18 -05:00
|
|
|
|
2013-12-19 03:40:57 -05:00
|
|
|
def _serialize_double_matrix(m):
|
2013-12-20 01:48:44 -05:00
|
|
|
"""Serialize a double matrix into a mutually understood format."""
|
2013-12-19 03:40:57 -05:00
|
|
|
if (type(m) == ndarray and m.dtype == float64 and m.ndim == 2):
|
|
|
|
rows = m.shape[0]
|
|
|
|
cols = m.shape[1]
|
|
|
|
ba = bytearray(24 + 8 * rows * cols)
|
|
|
|
header = ndarray(shape=[3], buffer=ba, dtype="int64")
|
|
|
|
header[0] = 2
|
|
|
|
header[1] = rows
|
|
|
|
header[2] = cols
|
|
|
|
copyto(ndarray(shape=[rows, cols], buffer=ba, offset=24,
|
|
|
|
dtype="float64", order='C'), m)
|
|
|
|
return ba
|
|
|
|
else:
|
|
|
|
raise TypeError("_serialize_double_matrix called on a "
|
|
|
|
"non-double-matrix")
|
2013-12-19 01:22:18 -05:00
|
|
|
|
2013-12-19 03:40:57 -05:00
|
|
|
def _deserialize_double_matrix(ba):
|
2013-12-20 01:48:44 -05:00
|
|
|
"""Deserialize a double matrix from a mutually understood format."""
|
2013-12-20 01:33:32 -05:00
|
|
|
if type(ba) != bytearray:
|
|
|
|
raise TypeError("_deserialize_double_matrix called on a %s; "
|
|
|
|
"wanted bytearray" % type(ba))
|
|
|
|
if len(ba) < 24:
|
|
|
|
raise TypeError("_deserialize_double_matrix called on a %d-byte array, "
|
|
|
|
"which is too short" % len(ba))
|
|
|
|
if (len(ba) & 7) != 0:
|
|
|
|
raise TypeError("_deserialize_double_matrix called on a %d-byte array, "
|
|
|
|
"which is not a multiple of 8" % len(ba))
|
|
|
|
header = ndarray(shape=[3], buffer=ba, dtype="int64")
|
|
|
|
if (header[0] != 2):
|
|
|
|
raise TypeError("_deserialize_double_matrix called on bytearray "
|
|
|
|
"with wrong magic")
|
|
|
|
rows = header[1]
|
|
|
|
cols = header[2]
|
|
|
|
if (len(ba) != 8*rows*cols + 24):
|
|
|
|
raise TypeError("_deserialize_double_matrix called on bytearray "
|
|
|
|
"with wrong length")
|
|
|
|
return _deserialize_byte_array([rows, cols], ba, 24)
|
2013-12-19 03:40:57 -05:00
|
|
|
|
2013-12-20 00:12:48 -05:00
|
|
|
def _linear_predictor_typecheck(x, coeffs):
|
2013-12-20 01:33:32 -05:00
|
|
|
"""Check that x is a one-dimensional vector of the right shape.
|
|
|
|
This is a temporary hackaround until I actually implement bulk predict."""
|
2013-12-20 00:12:48 -05:00
|
|
|
if type(x) == ndarray:
|
|
|
|
if x.ndim == 1:
|
|
|
|
if x.shape == coeffs.shape:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Got array of %d elements; wanted %d"
|
2013-12-25 00:08:05 -05:00
|
|
|
% (shape(x)[0], shape(coeffs)[0]))
|
2013-12-20 00:12:48 -05:00
|
|
|
else:
|
|
|
|
raise RuntimeError("Bulk predict not yet supported.")
|
|
|
|
elif (type(x) == RDD):
|
|
|
|
raise RuntimeError("Bulk predict not yet supported.")
|
|
|
|
else:
|
2013-12-25 00:53:48 -05:00
|
|
|
raise TypeError("Argument of type " + type(x).__name__ + " unsupported")
|
2013-12-20 00:12:48 -05:00
|
|
|
|
2013-12-25 00:08:05 -05:00
|
|
|
def _get_unmangled_rdd(data, serializer):
|
|
|
|
dataBytes = data.map(serializer)
|
|
|
|
dataBytes._bypass_serializer = True
|
|
|
|
dataBytes.cache()
|
|
|
|
return dataBytes
|
|
|
|
|
|
|
|
# Map a pickled Python RDD of numpy double vectors to a Java RDD of
|
|
|
|
# _serialized_double_vectors
|
|
|
|
def _get_unmangled_double_vector_rdd(data):
|
|
|
|
return _get_unmangled_rdd(data, _serialize_double_vector)
|
|
|
|
|
2013-12-19 22:45:16 -05:00
|
|
|
class LinearModel(object):
|
2013-12-20 02:05:15 -05:00
|
|
|
"""Something that has a vector of coefficients and an intercept."""
|
2013-12-19 03:40:57 -05:00
|
|
|
def __init__(self, coeff, intercept):
|
|
|
|
self._coeff = coeff
|
|
|
|
self._intercept = intercept
|
|
|
|
|
2013-12-20 00:12:48 -05:00
|
|
|
class LinearRegressionModelBase(LinearModel):
|
2013-12-20 01:33:32 -05:00
|
|
|
"""A linear regression model.
|
|
|
|
|
|
|
|
>>> lrmb = LinearRegressionModelBase(array([1.0, 2.0]), 0.1)
|
|
|
|
>>> abs(lrmb.predict(array([-1.03, 7.777])) - 14.624) < 1e-6
|
|
|
|
True
|
|
|
|
"""
|
2013-12-19 03:40:57 -05:00
|
|
|
def predict(self, x):
|
2013-12-20 00:12:48 -05:00
|
|
|
"""Predict the value of the dependent variable given a vector x"""
|
|
|
|
"""containing values for the independent variables."""
|
2013-12-20 01:33:32 -05:00
|
|
|
_linear_predictor_typecheck(x, self._coeff)
|
|
|
|
return dot(self._coeff, x) + self._intercept
|
2013-12-19 01:22:18 -05:00
|
|
|
|
2013-12-19 22:45:16 -05:00
|
|
|
# If we weren't given initial weights, take a zero vector of the appropriate
|
|
|
|
# length.
|
|
|
|
def _get_initial_weights(initial_weights, data):
|
|
|
|
if initial_weights is None:
|
|
|
|
initial_weights = data.first()
|
|
|
|
if type(initial_weights) != ndarray:
|
|
|
|
raise TypeError("At least one data element has type "
|
2013-12-25 00:53:48 -05:00
|
|
|
+ type(initial_weights).__name__ + " which is not ndarray")
|
2013-12-19 22:45:16 -05:00
|
|
|
if initial_weights.ndim != 1:
|
|
|
|
raise TypeError("At least one data element has "
|
|
|
|
+ initial_weights.ndim + " dimensions, which is not 1")
|
2013-12-25 00:53:48 -05:00
|
|
|
initial_weights = ones([initial_weights.shape[0] - 1])
|
2013-12-20 02:05:55 -05:00
|
|
|
return initial_weights
|
2013-12-19 22:45:16 -05:00
|
|
|
|
|
|
|
# train_func should take two parameters, namely data and initial_weights, and
|
|
|
|
# return the result of a call to the appropriate JVM stub.
|
|
|
|
# _regression_train_wrapper is responsible for setup and error checking.
|
|
|
|
def _regression_train_wrapper(sc, train_func, klass, data, initial_weights):
|
|
|
|
initial_weights = _get_initial_weights(initial_weights, data)
|
|
|
|
dataBytes = _get_unmangled_double_vector_rdd(data)
|
|
|
|
ans = train_func(dataBytes, _serialize_double_vector(initial_weights))
|
|
|
|
if len(ans) != 2:
|
2013-12-20 02:05:55 -05:00
|
|
|
raise RuntimeError("JVM call result had unexpected length")
|
2013-12-19 22:45:16 -05:00
|
|
|
elif type(ans[0]) != bytearray:
|
|
|
|
raise RuntimeError("JVM call result had first element of type "
|
2013-12-25 00:53:48 -05:00
|
|
|
+ type(ans[0]).__name__ + " which is not bytearray")
|
2013-12-19 22:45:16 -05:00
|
|
|
elif type(ans[1]) != float:
|
|
|
|
raise RuntimeError("JVM call result had second element of type "
|
2013-12-25 00:53:48 -05:00
|
|
|
+ type(ans[0]).__name__ + " which is not float")
|
2013-12-20 02:05:55 -05:00
|
|
|
return klass(_deserialize_double_vector(ans[0]), ans[1])
|
2013-12-19 22:45:16 -05:00
|
|
|
|
2013-12-21 14:54:01 -05:00
|
|
|
def _serialize_rating(r):
|
|
|
|
ba = bytearray(16)
|
|
|
|
intpart = ndarray(shape=[2], buffer=ba, dtype=int32)
|
|
|
|
doublepart = ndarray(shape=[1], buffer=ba, dtype=float64, offset=8)
|
|
|
|
intpart[0], intpart[1], doublepart[0] = r
|
|
|
|
return ba
|
|
|
|
|
2013-12-20 01:33:32 -05:00
|
|
|
def _test():
|
|
|
|
import doctest
|
|
|
|
globs = globals().copy()
|
|
|
|
globs['sc'] = SparkContext('local[4]', 'PythonTest', batchSize=2)
|
|
|
|
(failure_count, test_count) = doctest.testmod(globs=globs,
|
2013-12-21 14:54:01 -05:00
|
|
|
optionflags=doctest.ELLIPSIS)
|
2013-12-20 01:33:32 -05:00
|
|
|
globs['sc'].stop()
|
|
|
|
if failure_count:
|
|
|
|
exit(-1)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
_test()
|