# Path: splearn/preprocessing/label.py
# class SparkLabelEncoder(LabelEncoder, SparkTransformerMixin,
#                         SparkBroadcasterMixin):
# 
#     """Encode labels with value between 0 and n_classes-1.
#     Read more in the :ref:`User Guide <preprocessing_targets>`.
#     Attributes
#     ----------
#     classes_ : array of shape (n_class,)
#         Holds the label for each class.
#     Examples
#     --------
#     `SparkLabelEncoder` can be used to normalize labels.
#     >>> from splearn.preprocessing import SparkLabelEncoder
#     >>> from splearn import BlockRDD
#     >>>
#     >>> data = ["paris", "paris", "tokyo", "amsterdam"]
#     >>> y = BlockRDD(sc.parallelize(data))
#     >>>
#     >>> le = SparkLabelEncoder()
#     >>> le.fit(y)
#     >>> le.classes_
#     array(['amsterdam', 'paris', 'tokyo'],
#           dtype='|S9')
#     >>>
#     >>> test = ["tokyo", "tokyo", "paris"]
#     >>> y_test = BlockRDD(sc.parallelize(test))
#     >>>
#     >>> le.transform(y_test).toarray()
#     array([2, 2, 1])
#     >>>
#     >>> test = [2, 2, 1]
#     >>> y_test = BlockRDD(sc.parallelize(test))
#     >>>
#     >>> le.inverse_transform(y_test).toarray()
#     array(['tokyo', 'tokyo', 'paris'],
#           dtype='|S9')
#     """
# 
#     __transient__ = ['classes_']
# 
#     def fit(self, y):
#         """Fit label encoder
#         Parameters
#         ----------
#         y : ArrayRDD (n_samples,)
#             Target values.
#         Returns
#         -------
#         self : returns an instance of self.
#         """
# 
#         def mapper(y):
#             y = column_or_1d(y, warn=True)
#             _check_numpy_unicode_bug(y)
#             return np.unique(y)
# 
#         def reducer(a, b):
#             return np.unique(np.concatenate((a, b)))
# 
#         self.classes_ = y.map(mapper).reduce(reducer)
# 
#         return self
# 
#     def fit_transform(self, y):
#         """Fit label encoder and return encoded labels
#         Parameters
#         ----------
#         y : ArrayRDD [n_samples]
#             Target values.
#         Returns
#         -------
#         y : ArrayRDD [n_samples]
#         """
#         return self.fit(y).transform(y)
# 
#     def transform(self, y):
#         """Transform labels to normalized encoding.
#         Parameters
#         ----------
#         y : ArrayRDD [n_samples]
#             Target values.
#         Returns
#         -------
#         y : ArrayRDD [n_samples]
#         """
#         mapper = super(SparkLabelEncoder, self).transform
#         mapper = self.broadcast(mapper, y.context)
#         return y.transform(mapper)
# 
#     def inverse_transform(self, y):
#         """Transform labels back to original encoding.
#         Parameters
#         ----------
#         y : numpy array of shape [n_samples]
#             Target values.
#         Returns
#         -------
#         y : ArrayRDD [n_samples]
#         """
#         mapper = super(SparkLabelEncoder, self).inverse_transform
#         mapper = self.broadcast(mapper, y.context)
#         return y.transform(mapper)
# 
# Path: splearn/utils/testing.py
# def assert_tuple_equal(tpl1, tpl2):
# def assert_multiple_tuples_equal(tpls1, tpls2):
#     def setUp(self):
#     def tearDown(self):
#     def make_blobs(self, centers, n_samples, blocks=-1):
#     def make_regression(self, n_targets, n_samples, blocks=-1):
#     def make_classification(self, n_classes, n_samples, blocks=-1,
#                             nonnegative=False):
#     def make_text_rdd(self, blocks=-1):
#     def make_dense_rdd(self, shape=(1e3, 10), block_size=-1):
#     def make_dense_randint_rdd(self, low, high=None, shape=(1e3, 10),
#                                block_size=-1):
#     def make_dense_range_rdd(self, shape=(1e3, 10), block_size=-1):
#     def make_sparse_rdd(self, shape=(1e3, 10), block_size=-1):
# class SplearnTestCase(unittest.TestCase):
#         Z = DictRDD([X_rdd, y_rdd], columns=('X', 'y'), bsize=blocks)
#             X = np.abs(X)
#         Z = DictRDD([X_rdd, y_rdd], columns=('X', 'y'), bsize=blocks)
#         X = ALL_FOOD_DOCS
#         X = rng.randn(*shape)
#         X = np.random.randint(low, high, size=shape)
#         X = np.arange(np.prod(shape)).reshape(shape)
#         X = sp.rand(shape[0], shape[1], random_state=42, density=0.3)


from sklearn.preprocessing import LabelEncoder
from splearn.preprocessing import SparkLabelEncoder
from splearn.utils.testing import SplearnTestCase, assert_array_equal


class TestSparkLabelEncoder(SplearnTestCase):

    def test_same_fit_transform(self):
        Y, Y_rdd = self.make_dense_randint_rdd(low=0, high=10, shape=(1000,))

        local = LabelEncoder()
        dist = SparkLabelEncoder()

        assert_array_equal(local.fit_transform(Y),
                           dist.fit_transform(Y_rdd).toarray())

    def test_same_classes(self):
        Y, Y_rdd = self.make_dense_randint_rdd(low=0, high=10, shape=(1000,))

        local = LabelEncoder().fit(Y)
        dist = SparkLabelEncoder().fit(Y_rdd)

        assert_array_equal(local.classes_, dist.classes_)

    def test_same_inverse_transform(self):
        Y, Y_rdd = self.make_dense_randint_rdd(low=0, high=10, shape=(1000,))

        local = LabelEncoder().fit(Y)
        dist = SparkLabelEncoder().fit(Y_rdd)


# Path: splearn/preprocessing/label.py
# class SparkLabelEncoder(LabelEncoder, SparkTransformerMixin,
#                         SparkBroadcasterMixin):
# 
#     """Encode labels with value between 0 and n_classes-1.
#     Read more in the :ref:`User Guide <preprocessing_targets>`.
#     Attributes
#     ----------
#     classes_ : array of shape (n_class,)
#         Holds the label for each class.
#     Examples
#     --------
#     `SparkLabelEncoder` can be used to normalize labels.
#     >>> from splearn.preprocessing import SparkLabelEncoder
#     >>> from splearn import BlockRDD
#     >>>
#     >>> data = ["paris", "paris", "tokyo", "amsterdam"]
#     >>> y = BlockRDD(sc.parallelize(data))
#     >>>
#     >>> le = SparkLabelEncoder()
#     >>> le.fit(y)
#     >>> le.classes_
#     array(['amsterdam', 'paris', 'tokyo'],
#           dtype='|S9')
#     >>>
#     >>> test = ["tokyo", "tokyo", "paris"]
#     >>> y_test = BlockRDD(sc.parallelize(test))
#     >>>
#     >>> le.transform(y_test).toarray()
#     array([2, 2, 1])
#     >>>
#     >>> test = [2, 2, 1]
#     >>> y_test = BlockRDD(sc.parallelize(test))
#     >>>
#     >>> le.inverse_transform(y_test).toarray()
#     array(['tokyo', 'tokyo', 'paris'],
#           dtype='|S9')
#     """
# 
#     __transient__ = ['classes_']
# 
#     def fit(self, y):
#         """Fit label encoder
#         Parameters
#         ----------
#         y : ArrayRDD (n_samples,)
#             Target values.
#         Returns
#         -------
#         self : returns an instance of self.
#         """
# 
#         def mapper(y):
#             y = column_or_1d(y, warn=True)
#             _check_numpy_unicode_bug(y)
#             return np.unique(y)
# 
#         def reducer(a, b):
#             return np.unique(np.concatenate((a, b)))
# 
#         self.classes_ = y.map(mapper).reduce(reducer)
# 
#         return self
# 
#     def fit_transform(self, y):
#         """Fit label encoder and return encoded labels
#         Parameters
#         ----------
#         y : ArrayRDD [n_samples]
#             Target values.
#         Returns
#         -------
#         y : ArrayRDD [n_samples]
#         """
#         return self.fit(y).transform(y)
# 
#     def transform(self, y):
#         """Transform labels to normalized encoding.
#         Parameters
#         ----------
#         y : ArrayRDD [n_samples]
#             Target values.
#         Returns
#         -------
#         y : ArrayRDD [n_samples]
#         """
#         mapper = super(SparkLabelEncoder, self).transform
#         mapper = self.broadcast(mapper, y.context)
#         return y.transform(mapper)
# 
#     def inverse_transform(self, y):
#         """Transform labels back to original encoding.
#         Parameters
#         ----------
#         y : numpy array of shape [n_samples]
#             Target values.
#         Returns
#         -------
#         y : ArrayRDD [n_samples]
#         """
#         mapper = super(SparkLabelEncoder, self).inverse_transform
#         mapper = self.broadcast(mapper, y.context)
#         return y.transform(mapper)
# 
# Path: splearn/utils/testing.py
# def assert_tuple_equal(tpl1, tpl2):
# def assert_multiple_tuples_equal(tpls1, tpls2):
#     def setUp(self):
#     def tearDown(self):
#     def make_blobs(self, centers, n_samples, blocks=-1):
#     def make_regression(self, n_targets, n_samples, blocks=-1):
#     def make_classification(self, n_classes, n_samples, blocks=-1,
#                             nonnegative=False):
#     def make_text_rdd(self, blocks=-1):
#     def make_dense_rdd(self, shape=(1e3, 10), block_size=-1):
#     def make_dense_randint_rdd(self, low, high=None, shape=(1e3, 10),
#                                block_size=-1):
#     def make_dense_range_rdd(self, shape=(1e3, 10), block_size=-1):
#     def make_sparse_rdd(self, shape=(1e3, 10), block_size=-1):
# class SplearnTestCase(unittest.TestCase):
#         Z = DictRDD([X_rdd, y_rdd], columns=('X', 'y'), bsize=blocks)
#             X = np.abs(X)
#         Z = DictRDD([X_rdd, y_rdd], columns=('X', 'y'), bsize=blocks)
#         X = ALL_FOOD_DOCS
#         X = rng.randn(*shape)
#         X = np.random.randint(low, high, size=shape)
#         X = np.arange(np.prod(shape)).reshape(shape)
#         X = sp.rand(shape[0], shape[1], random_state=42, density=0.3)


# Path: splearn/preprocessing/tests/test_label.py
from sklearn.preprocessing import LabelEncoder
from splearn.preprocessing import SparkLabelEncoder
from splearn.utils.testing import SplearnTestCase, assert_array_equal



class TestSparkLabelEncoder(SplearnTestCase):

    def test_same_fit_transform(self):
        Y, Y_rdd = self.make_dense_randint_rdd(low=0, high=10, shape=(1000,))

        local = LabelEncoder()
        dist = SparkLabelEncoder()

        assert_array_equal(local.fit_transform(Y),
                           dist.fit_transform(Y_rdd).toarray())

    def test_same_classes(self):
        Y, Y_rdd = self.make_dense_randint_rdd(low=0, high=10, shape=(1000,))

        local = LabelEncoder().fit(Y)
        dist = SparkLabelEncoder().fit(Y_rdd)

        assert_array_equal(local.classes_, dist.classes_)

    def test_same_inverse_transform(self):
        Y, Y_rdd = self.make_dense_randint_rdd(low=0, high=10, shape=(1000,))

        local = LabelEncoder().fit(Y)
        dist = SparkLabelEncoder().fit(Y_rdd)


        assert_array_equal(local.inverse_transform(Y),