Esempio n. 1
0
class GaussianProcess_(ProbabilisticModel):

    """GaussianProcess Classifier
    """

    def __init__(self, *args, **kwargs):
        self.model = GaussianProcessClassifier(*args, **kwargs)
        self.name = "gpc"        

    def train(self, dataset, *args, **kwargs):
        return self.model.fit(*(dataset.format_sklearn() + args), **kwargs)

    def predict(self, feature, *args, **kwargs):
        return self.model.predict(feature, *args, **kwargs)

    def score(self, testing_dataset, *args, **kwargs):
        return self.model.score(*(testing_dataset.format_sklearn() + args),
                                **kwargs)
    def predict_proba(self, feature, *args, **kwargs):
        return self.model.predict_proba(feature, *args, **kwargs)
    
    def feature_importances_(self):
        LOGGER.warn("GPC model does not support feature_importance")
        return None
    
    def get_params(self):
        return self.model.get_params()
Esempio n. 2
0
class GaussianProcess(ClassicalModel):
    def __init__(self,
                 input_size,
                 output_size,
                 labels,
                 class_weights=None,
                 **kwargs):
        super().__init__(input_size, output_size, labels, class_weights)
        self.model = GaussianProcessClassifier(**kwargs)
        self.name = "Gaussian Process Classifier: \n" + str(
            self.model.get_params())
def GPRTraining(XEstimate, XValidate, Parameters, class_labels):
    kernel = RBF(length_scale=1.0, length_scale_bounds=(1e-05, 100000.0))
    # clf = GaussianProcessClassifier(kernel=kernel, n_restarts_optimizer=1)
    clf = GaussianProcessClassifier(kernel=RBF(length_scale=1.0),
                                    optimizer=None,
                                    multi_class='one_vs_one',
                                    n_jobs=1)

    clf.fit(XEstimate, class_labels)
    Yvalidate = clf.predict(XValidate)
    EstParameters = clf.get_params()

    return {"Yvalidate": Yvalidate, "EstParameters": EstParameters, "clf": clf}
Esempio n. 4
0
                y_pred = model.predict(X_test)
                errorTst = mean_squared_error(y_test, y_pred)
                r2Tst = model.score(X_test, y_test)

                now = datetime.now()
                current_time = now.strftime("%H:%M:%S")
                print(current_time, ' ', exp_num, data_file, ' ', runOpt,
                      ' test accuracy:', r2Tst)
                #collection
                error = [
                    errorTrn, errorTst, r2Trn, r2Tst, 'none', funcEvltime,
                    'empty'
                ]
                data_results_coll.update({
                    str(data_file.split('.')[0]) + "_" + str(exp_num) + "_" + runOpt:
                    error
                })
            #collect all SGDs
            #end for all solve
        #end for each runs
        data_results_coll.update({'model_config': model.get_params()})
        np.save(
            os.getcwd() + os.sep + 'outputs' + os.sep +
            data_file.split('.')[0] + '_Ep_' + str(EPOCHS) + '_B_All_' +
            runOpt, data_results_coll)
    #end for each dataum
    #%% save np.load
print('All experiments end. for')
print(data_file_set)
#a = np.load('outputs'+os.sep+'mlp_friedman_Ep_500_B1Sig_l2.npy.npy').item()
Esempio n. 5
0
class GaussianProcess(Classifier):
    r"""Implementation of gaussian process classifier.
    
    Date:
        2020

    Author:
        Luka Pečnik

    License:
        MIT
    
    Reference:
        Rasmussen, Carl Edward, and Hannes Nickisch. "Gaussian processes for machine learning (GPML) toolbox." The Journal of Machine Learning Research 11 (2010): 3011-3015.
    
    Documentation:
        https://scikit-learn.org/stable/modules/generated/sklearn.gaussian_process.GaussianProcessClassifier.html

    See Also:
        * :class:`niaaml.classifiers.Classifier`
    """
    Name = 'Gaussian Process Classifier'

    def __init__(self, **kwargs):
        r"""Initialize GaussianProcess instance.
        """
        warnings.filterwarnings(action='ignore',
                                category=ChangedBehaviorWarning)
        warnings.filterwarnings(action='ignore', category=ConvergenceWarning)
        warnings.filterwarnings(action='ignore',
                                category=DataConversionWarning)
        warnings.filterwarnings(action='ignore',
                                category=DataDimensionalityWarning)
        warnings.filterwarnings(action='ignore', category=EfficiencyWarning)
        warnings.filterwarnings(action='ignore', category=FitFailedWarning)
        warnings.filterwarnings(action='ignore', category=NonBLASDotWarning)
        warnings.filterwarnings(action='ignore',
                                category=UndefinedMetricWarning)

        self._params = dict(
            max_iter_predict=ParameterDefinition(MinMax(50, 200), np.uint),
            warm_start=ParameterDefinition([True, False]),
            multi_class=ParameterDefinition(['one_vs_rest', 'one_vs_one']))
        self.__gaussian_process = GPC()

    def set_parameters(self, **kwargs):
        r"""Set the parameters/arguments of the algorithm.
        """
        self.__gaussian_process.set_params(**kwargs)

    def fit(self, x, y, **kwargs):
        r"""Fit GaussianProcess.

        Arguments:
            x (pandas.core.frame.DataFrame): n samples to classify.
            y (pandas.core.series.Series): n classes of the samples in the x array.

        Returns:
            None
        """
        self.__gaussian_process.fit(x, y)

    def predict(self, x, **kwargs):
        r"""Predict class for each sample (row) in x.

        Arguments:
            x (pandas.core.frame.DataFrame): n samples to classify.

        Returns:
            pandas.core.series.Series: n predicted classes.
        """
        return self.__gaussian_process.predict(x)

    def to_string(self):
        r"""User friendly representation of the object.

        Returns:
            str: User friendly representation of the object.
        """
        return Classifier.to_string(self).format(
            name=self.Name,
            args=self._parameters_to_string(
                self.__gaussian_process.get_params()))