Beispiel #1
0
    def test_min_max_scaler_multi_deserializer(self):

        scaler = MinMaxScaler()
        scaler.mlinit(input_features=['a', 'b'],
                      output_features=['a_scaled', 'b_scaled'])

        scaler.fit(self.df[['a']])

        scaler.serialize_to_bundle(self.tmp_dir, scaler.name)

        # Deserialize the MinMaxScaler
        node_name = "{}.node".format(scaler.name)
        min_max_scaler_tf = MinMaxScaler()
        min_max_scaler_tf.deserialize_from_bundle(self.tmp_dir, node_name)

        # Transform some sample data
        res_a = scaler.transform(self.df[['a', 'b']])
        res_b = min_max_scaler_tf.transform(self.df[['a', 'b']])

        self.assertEqual(res_a[0][0], res_b[0][0])
        self.assertEqual(res_a[0][1], res_b[0][1])

        self.assertEqual(scaler.name, min_max_scaler_tf.name)
        self.assertEqual(scaler.op, min_max_scaler_tf.op)
Beispiel #2
0
    def test_min_max_scaler_multi_deserializer(self):

        extract_features = ['a', 'b']
        feature_extractor = FeatureExtractor(
            input_scalars=['a', 'b'],
            output_vector='extracted_multi_outputs',
            output_vector_items=["{}_out".format(x) for x in extract_features])

        scaler = MinMaxScaler()
        scaler.mlinit(prior_tf=feature_extractor,
                      output_features=['a_scaled', 'b_scaled'])

        scaler.fit(self.df[['a']])

        scaler.serialize_to_bundle(self.tmp_dir, scaler.name)

        # Deserialize the MinMaxScaler
        node_name = "{}.node".format(scaler.name)
        min_max_scaler_tf = MinMaxScaler()
        min_max_scaler_tf.deserialize_from_bundle(self.tmp_dir, node_name)

        # Transform some sample data
        res_a = scaler.transform(self.df[['a', 'b']])
        res_b = min_max_scaler_tf.transform(self.df[['a', 'b']])

        self.assertEqual(res_a[0][0], res_b[0][0])
        self.assertEqual(res_a[0][1], res_b[0][1])

        self.assertEqual(scaler.name, min_max_scaler_tf.name)
        self.assertEqual(scaler.op, min_max_scaler_tf.op)
Beispiel #3
0
    def test_min_max_scaler_serializer(self):

        scaler = MinMaxScaler()
        scaler.mlinit(input_features='a', output_features='a_scaled')

        scaler.fit(self.df[['a']])

        scaler.serialize_to_bundle(self.tmp_dir, scaler.name)

        expected_min = self.df.a.min()
        expected_max = self.df.a.max()

        expected_model = {
            "op": "min_max_scaler",
            "attributes": {
                "min": {
                    "type": {
                        "type": "tensor",
                        "tensor": {
                            "base": "double"
                        }
                    },
                    "value": {
                        "values": [expected_min],
                        "dimensions": [1]
                    }
                },
                "max": {
                    "type": {
                        "type": "tensor",
                        "tensor": {
                            "base": "double"
                        }
                    },
                    "value": {
                        "values": [expected_max],
                        "dimensions": [1]
                    }
                }
            }
        }

        self.assertAlmostEqual(expected_min,
                               scaler.data_min_.tolist()[0],
                               places=7)
        self.assertAlmostEqual(expected_max,
                               scaler.data_max_.tolist()[0],
                               places=7)

        # Test model.json
        with open("{}/{}.node/model.json".format(self.tmp_dir,
                                                 scaler.name)) as json_data:
            model = json.load(json_data)

        self.assertEqual(scaler.op, expected_model['op'])
        self.assertEqual(
            expected_model['attributes']['min']['value']['dimensions'][0],
            model['attributes']['min']['value']['dimensions'][0])
        self.assertEqual(
            expected_model['attributes']['max']['value']['dimensions'][0],
            model['attributes']['max']['value']['dimensions'][0])
        self.assertAlmostEqual(
            expected_model['attributes']['min']['value']['values'][0],
            model['attributes']['min']['value']['values'][0])
        self.assertAlmostEqual(
            expected_model['attributes']['max']['value']['values'][0],
            model['attributes']['max']['value']['values'][0])

        # Test node.json
        with open("{}/{}.node/node.json".format(self.tmp_dir,
                                                scaler.name)) as json_data:
            node = json.load(json_data)

        self.assertEqual(scaler.name, node['name'])
        self.assertEqual(scaler.input_features,
                         node['shape']['inputs'][0]['name'])
        self.assertEqual(scaler.output_features,
                         node['shape']['outputs'][0]['name'])
Beispiel #4
0
    def test_min_max_scaler_serializer(self):

        extract_features = ['a']
        feature_extractor = FeatureExtractor(
            input_scalars=['a'],
            output_vector='extracted_a_output',
            output_vector_items=["{}_out".format(x) for x in extract_features])

        scaler = MinMaxScaler()
        scaler.mlinit(prior_tf=feature_extractor, output_features='a_scaled')

        scaler.fit(self.df[['a']])

        scaler.serialize_to_bundle(self.tmp_dir, scaler.name)

        expected_min = self.df.a.min()
        expected_max = self.df.a.max()

        expected_model = {
            "op": "min_max_scaler",
            "attributes": {
                "min": {
                    "double": [expected_min],
                    "shape": {
                        "dimensions": [{
                            "size": 1,
                            "name": ""
                        }]
                    },
                    "type": "tensor"
                },
                "max": {
                    "double": [expected_max],
                    "shape": {
                        "dimensions": [{
                            "size": 1,
                            "name": ""
                        }]
                    },
                    "type": "tensor"
                }
            }
        }

        self.assertEqual(expected_min, scaler.data_min_.tolist()[0])
        self.assertEqual(expected_max, scaler.data_max_.tolist()[0])

        # Test model.json
        with open("{}/{}.node/model.json".format(self.tmp_dir,
                                                 scaler.name)) as json_data:
            model = json.load(json_data)

        self.assertEqual(scaler.op, expected_model['op'])
        self.assertEqual(
            expected_model['attributes']['min']['shape']['dimensions'][0]
            ['size'],
            model['attributes']['min']['shape']['dimensions'][0]['size'])
        self.assertEqual(
            expected_model['attributes']['max']['shape']['dimensions'][0]
            ['size'],
            model['attributes']['max']['shape']['dimensions'][0]['size'])
        self.assertEqual(expected_model['attributes']['min']['double'][0],
                         model['attributes']['min']['double'][0])
        self.assertEqual(expected_model['attributes']['max']['double'][0],
                         model['attributes']['max']['double'][0])

        # Test node.json
        with open("{}/{}.node/node.json".format(self.tmp_dir,
                                                scaler.name)) as json_data:
            node = json.load(json_data)

        self.assertEqual(scaler.name, node['name'])
        self.assertEqual(scaler.input_features,
                         node['shape']['inputs'][0]['name'])
        self.assertEqual(scaler.output_features,
                         node['shape']['outputs'][0]['name'])