Exemplo n.º 1
0
    def save_standard_scale_model(self, name, namespace):
        """
        Save StandardScaler param and meta
        Parameters
        ----------
        name: str, save DTable table_name
        namespace: str, DTable namespace

        Returns
        ----------
        tuple,
            include meta string name and param string name
        """
        meta_buffer_type = self._save_standard_scale_meta(name, namespace)

        standard_scale_param_dict = {}
        for i in range(len(self.header)):
            if i in self.std_scale_column_idx:
                mean = self.mean[i]
                std = self.std[i]

                param_obj = feature_scale_param_pb2.StandardScaleParam(mean=mean, scale=std)
                standard_scale_param_dict[self.header[i]] = param_obj

        param_protobuf_obj = feature_scale_param_pb2.ScaleParam(standard_scale_param=standard_scale_param_dict)
        param_buffer_type = "{}.param".format(self.class_name)

        model_manager.save_model(buffer_type=param_buffer_type,
                                 proto_buffer=param_protobuf_obj,
                                 name=name,
                                 namespace=namespace)
        return [(meta_buffer_type, param_buffer_type)]
Exemplo n.º 2
0
    def save_min_max_model(self, name, namespace):
        meta_buffer_type = self._save_min_max_meta(name, namespace)

        min_max_scale_param_dict = {}
        if self.cols_scale_value is not None:
            for i in range(len(self.header)):
                feat_lower = self.cols_scale_value[i][0]
                feat_upper = self.cols_scale_value[i][1]
                out_lower = self.cols_scale_value[i][2]
                out_upper = self.cols_scale_value[i][3]
                param_obj = feature_scale_param_pb2.MinMaxScaleParam(
                    feat_upper=feat_upper,
                    feat_lower=feat_lower,
                    out_upper=out_upper,
                    out_lower=out_lower)
                min_max_scale_param_dict[self.header[i]] = param_obj

        param_protobuf_obj = feature_scale_param_pb2.ScaleParam(
            minmax_scale_param=min_max_scale_param_dict)
        param_buffer_type = "{}.param".format(self.class_name)

        model_manager.save_model(buffer_type=param_buffer_type,
                                 proto_buffer=param_protobuf_obj,
                                 name=name,
                                 namespace=namespace)
        return [(meta_buffer_type, param_buffer_type)]
Exemplo n.º 3
0
    def load_model(self, name, namespace, header):
        """
        load Scaler model
        Parameters
        ----------
        name: str, DTable table_name
        namespace: str, DTable namespace

        Returns
        ----------
        list,
            scale information
        """
        self.header = header
        param_buffer_type = "{}.param".format(self.class_name)
        param_obj = feature_scale_param_pb2.ScaleParam()
        model_manager.read_model(buffer_type=param_buffer_type,
                                 proto_buffer=param_obj,
                                 name=name,
                                 namespace=namespace)

        if self.scale_param.method == consts.MINMAXSCALE:
            cols_scale_value = []
            param_dict = param_obj.minmax_scale_param
        
            for idx, header_name in enumerate(self.header):
                if header_name in param_dict:
                    feat_upper = param_dict[header_name].feat_upper
                    feat_lower = param_dict[header_name].feat_lower
                    out_upper = param_dict[header_name].out_upper
                    out_lower = param_dict[header_name].out_lower
                    cols_scale_value.append((feat_lower,
                                             feat_upper,
                                             out_lower,
                                             out_upper))
                else:
                    raise ValueError("Can not find the header name {} in model.".format(header_name))
            
            model_scale_value_results = [ cols_scale_value ]

        elif self.scale_param.method == consts.STANDARDSCALE:
            mean = []
            std = []
            param_dict = param_obj.standard_scale_param
            for idx, header_name in enumerate(self.header):
                if header_name in param_dict:
                    mean.append(param_dict[header_name].mean)
                    std.append(param_dict[header_name].scale)
                else:
                    raise ValueError("Can not find the header name {} in model.".format(header_name))

            model_scale_value_results = [ mean, std ]
        
        else:
            raise ValueError("Unknown scale method:{}".format(self.scale_param.method))

        
        return model_scale_value_results
Exemplo n.º 4
0
    def save_standard_scale_model(self, name, namespace):
        meta_buffer_type = self._save_standard_scale_meta(name, namespace)

        standard_scale_param_dict = {}
        for i in range(len(self.header)):
            mean = self.mean[i]
            std = self.std[i]

            param_obj = feature_scale_param_pb2.StandardScaleParam(mean=mean, scale=std)
            standard_scale_param_dict[self.header[i]] = param_obj

        param_protobuf_obj = feature_scale_param_pb2.ScaleParam(standard_scale_param=standard_scale_param_dict)
        param_buffer_type = "{}.param".format(self.class_name)

        model_manager.save_model(buffer_type=param_buffer_type,
                                 proto_buffer=param_protobuf_obj,
                                 name=name,
                                 namespace=namespace)
        return [(meta_buffer_type, param_buffer_type)]
Exemplo n.º 5
0
    def save_min_max_model(self, name, namespace):
        """
        Save MinMaxScaler param and meta
        Parameters
        ----------
        name: str, save DTable table_name
        namespace: str, DTable namespace

        Returns
        ----------
        tuple,
            include meta string name and param string name
        """
        meta_buffer_type = self._save_min_max_meta(name, namespace)

        cols_scale_value = self.cols_scale_res[0]
        scale_column_idx = self.cols_scale_res[1]

        min_max_scale_param_dict = {}
        if cols_scale_value is not None:
            for i in range(len(self.header)):
                if i in scale_column_idx:
                    feat_lower = cols_scale_value[i][0]
                    feat_upper = cols_scale_value[i][1]
                    out_lower = cols_scale_value[i][2]
                    out_upper = cols_scale_value[i][3]
                    param_obj = feature_scale_param_pb2.MinMaxScaleParam(feat_upper=feat_upper,
                                                                         feat_lower=feat_lower,
                                                                         out_upper=out_upper,
                                                                         out_lower=out_lower)
                    min_max_scale_param_dict[self.header[i]] = param_obj

        param_protobuf_obj = feature_scale_param_pb2.ScaleParam(minmax_scale_param=min_max_scale_param_dict)
        param_buffer_type = "{}.param".format(self.class_name)

        model_manager.save_model(buffer_type=param_buffer_type,
                                 proto_buffer=param_protobuf_obj,
                                 name=name,
                                 namespace=namespace)
        return [(meta_buffer_type, param_buffer_type)]