Пример #1
0
    def makeGaussianProcess(self):

        tempNuggs = nm.reshape(nm.array(self.allNuggets), len(self.allNuggets))

        if self.corrFixed:
            self.gaussProcess = slgp.GaussianProcess(nugget=tempNuggs,
                                                     theta0=self.bestTheta)
            self.gaussProcess.fit(self.allParams, self.allCosts)
            self.gaussProcessParticles = []
            self.gaussProcessParticles.append(self.gaussProcess)

        else:
            tempThetaParticles = self.logThetaParticles
            self.logThetaParticles = []
            self.logWeights = []
            self.gaussProcessParticles = []

            for ttp in tempThetaParticles:
                testTheta = nm.exp(ttp)
                #print("saved currTestTheta:" + str(testTheta))
                self.gaussProcess = slgp.GaussianProcess(nugget=tempNuggs,
                                                         theta0=testTheta,
                                                         thetaL=self.minTheta,
                                                         thetaU=self.maxTheta)
                self.gaussProcess.fit(self.allParams, self.allCosts)
                self.saveParticles()
            for _ in range(self.thetaSearchNumber):
                testTheta = nm.power(
                    10, self.minLogTheta + nr.rand(self.numParams) *
                    (self.maxLogTheta - self.minLogTheta))
                #print("random currTestTheta:" + str(testTheta))
                self.gaussProcess = slgp.GaussianProcess(nugget=tempNuggs,
                                                         theta0=testTheta,
                                                         thetaL=self.minTheta,
                                                         thetaU=self.maxTheta)
                self.gaussProcess.fit(self.allParams, self.allCosts)
                self.saveParticles()

            self.dropSmallParticles()

            self.normWeights = nm.exp(nm.array(self.logWeights))
            self.normWeights = self.normWeights / nm.sum(self.normWeights)
            self.numParticles = len(self.normWeights)

            #Diagnostics
            maxLogWeight = max(self.logWeights)
            thetaRef = self.logWeights.index(maxLogWeight)
            self.bestTheta = 10.**self.logThetaParticles[thetaRef]
            self.weightEntropy = -float(
                nm.sum(self.normWeights * nm.log(self.normWeights)))

            self.histBestTheta.append(nm.ravel(self.bestTheta))
            self.histBestRLFV.append(maxLogWeight)
            self.histNumParticles.append(self.numParticles)
            self.histWeightEntropy.append(self.weightEntropy)
def demGaussians(trainData,testData,trainOuts,testOuts):
	clf = gaussian_process.GaussianProcess(theta0=1e-2, thetaL=1e-4, thetaU=1e-1)
	print(clf.fit(trainData,trainOuts))
	predictions = clf.predict(testData)
	print(predictions)
	misses,error = sup.crunchTestResults(predictions,testOuts,.5)
	print(1-error)
 def computeGaussianProcessApproximation(self):
     #For the approximation
     self.x_pred = np.atleast_2d(np.linspace(BOUND_L, BOUND_U, 200)).T
     #GP call
     gp = gaussian_process.GaussianProcess()
     gp.fit(self.x_sample, self.y_sample)  
     self.y_pred, self.sigma2_pred = gp.predict(self.x_pred, eval_MSE=True)
Пример #4
0
    def __gp(self,polls, parties, ax):
        """ TODO

        :param x:
        :param ax:
        :param partyname:
        :return:
        """
        for party in parties.parties.values():
            polls_party = polls.get_party(party)
            
            dates = [x[0] for x in polls_party]
            votes = [x[1] for x in polls_party]           
        
            x = dates
            y = votes

            # + 0.5 - 0.5?
            
            x_dense = np.atleast_2d(np.linspace(time.mktime(x[0].timetuple()),
                                                time.mktime(x[-1].timetuple()), 1000)).T
            #x_dense = np.atleast_2d(np.linspace(x[0], x[-1], 1000)).T

            np.random.seed(1)            
            gp = gaussian_process.GaussianProcess(corr='squared_exponential', theta0=1e-1,
                                                  thetaL=1e-3, thetaU=1,
                                                  random_start=100, nugget=10 - 8)
            x = [time.mktime(xi.timetuple()) for xi in x]
            gp.fit(np.reshape(x, (-1, 1)) + np.random.randn(len(x),1)*0.01, np.reshape(y,(-1, 1)))
            y_pred, mse = gp.predict(x_dense, eval_MSE=True)
            sigma = np.sqrt(mse)
            x_dense = [datetime.datetime.fromtimestamp(xi) for xi in x_dense]
            ax.plot(x_dense, y_pred, 'b-', label=u'Prediction', c=party.color, linewidth=3)
Пример #5
0
def get_model(target,features,test_features,test_target):
	"""
		the list of models to try
	"""
	model_names = [
		linear_model.LinearRegression(),		
		linear_model.RidgeCV(alphas=[0.1, 1.0, 10.0]),
		tree.DecisionTreeRegressor(),
		gaussian_process.GaussianProcess(theta0=1e-2, thetaL=1e-4, thetaU=1e-1),
		SVR(kernel='linear', C=1e3),
		linear_model.BayesianRidge(),
		linear_model.SGDRegressor()
		]
	
	i = 0
	
	best_model = None
	best_score = 0
	
	for model in model_names:
		mf = Model_fitter(model)
		if i >= 4:
			target=target.ravel()
			test_target = test_target.ravel()
		i+=1
		mf.fit_data(features,target)
		mf.compute_score(test_features,test_target)		
		# choose the model with  least error
		if best_model is None:
			best_model = mf
		elif best_model.residual_error > mf.residual_error:
			best_model = mf
	return best_model 
Пример #6
0
def bayesopt(f, initial_x, acquisition, niter=100, debug=False):
    """The actual bayesian optimization function. 

    f is the very expensive function we want to optimize. 

    initial_x is a matrix of at least two data points (preferrably
    more, randomly sampled). 

    acquisition is the acquisiton function we want to use to find
    query points. 

    Note that doing simulated annealing as done here is a very bad
    idea, as we should be very careful about the domain."""
    X = initial_x
    y = [f(x) for x in initial_x]
    best_x = initial_x[np.argmax(y)]
    best_f = y[np.argmax(y)]
    print y
    gp = gaussian_process.GaussianProcess()
    for i in xrange(niter):
        gp.fit(np.array(X), np.array(y))
        new_x = scipy.optimize.anneal(acquisition(gp, best_f), best_x)[0]
        new_f = f(new_x)
        X.append(new_x)
        y.append(new_f)
        if new_f > best_f:
            best_f = new_f
            best_x = new_x
        if debug:
            print "iter", i, "best_x", best_x, best_f
    return best_x, best_f
Пример #7
0
    def learnProb(self):

        ndlen = len(self.nondomObs)
        dlen = len(self.domObs)

        if ndlen + dlen < 3:
            return

        if self.kernel == None:
            self.kernel = gaussian_process.GaussianProcess(corr='cubic',
                                                           theta0=1e-2,
                                                           thetaL=1e-1,
                                                           thetaU=1e-1)

        X = np.zeros((ndlen + dlen, self.dimNum))
        Y = np.zeros((ndlen + dlen, 1))
        for i in range(ndlen):
            for d in range(self.dimNum):
                X[i, d] = self.nondomObs[i][0, d]
            Y[i, 0] = 0.8
        for i in range(dlen):
            for d in range(self.dimNum):
                X[i + ndlen, d] = self.domObs[i][0, d]
            Y[i + ndlen, 0] = 1e-4

        #print X
        #print Y
        self.kernel.fit(X, Y)
Пример #8
0
def tune_GP(X_tr, y_tr):
    param_space = {}

    regressor = gaussian_process.GaussianProcess(theta0=0.1,
                                                 thetaL=.001,
                                                 thetaU=1.)
    return tune(regressor, param_space, X_tr, y_tr)
Пример #9
0
def play_gpy(ntrain=10):


    xtrain_ = np.random.uniform(low=-1.0, high=2.0, size=ntrain)
    X_train = xtrain_.reshape(len(xtrain_), 1)

    y_train = objective(X_train)

    x_test = np.linspace(-1, 2, 1000)
    gp = gaussian_process.GaussianProcess(theta0=1e-2, thetaL=1e-4, thetaU=1e-1)
    gp.fit(X_train, y_train)
    y_pred, sigma2_pred = gp.predict(x_test, eval_MSE=True)
    sigma = np.sqrt(sigma2_pred)

    fig = pl.figure()
    pl.plot(x_test, f(x_test), 'r:', label=u'$f(x) = x\,\sin(x)$')
    pl.plot(X_train, y_train, 'r.', markersize=10, label=u'Observations')
    pl.plot(x_test, y_pred, 'b-', label=u'Prediction')
    pl.fill(np.concatenate([x_test, x_test[::-1]]),
            np.concatenate([y_pred - 1.9600 * sigma,
                           (y_pred + 1.9600 * sigma)[::-1]]),
            alpha=.5, fc='b', ec='None', label='95% confidence interval')
    pl.xlabel('$x$')
    pl.ylabel('$f(x)$')
    pl.ylim(-10, 20)
    pl.legend(loc='upper left')
    pl.show()
    def test_Gaussian2D(self):
        # http://scikit-learn.org/stable/auto_examples/gaussian_process/plot_gp_probabilistic_classification_after_regression.html

        def g(x):
            """The function to predict (classification will then consist in predicting
            whether g(x) <= 0 or not)"""
            return 5. - x[:, 1] - .5 * x[:, 0]**2.

        # Design of experiments
        X = np.array([[-4.61611719, -6.00099547], [4.10469096, 5.32782448],
                      [0.00000000, -0.50000000], [-6.17289014, -4.6984743],
                      [1.3109306, -6.93271427], [-5.03823144, 3.10584743],
                      [-2.87600388, 6.74310541], [5.21301203, 4.26386883]])
        y = g(X)

        df = pdml.ModelFrame(X, target=y)
        gpm1 = df.gaussian_process.GaussianProcess(theta0=5e-1)
        df.fit(gpm1)
        result, result_MSE = df.predict(gpm1, eval_MSE=True)

        gpm2 = gp.GaussianProcess(theta0=5e-1)
        gpm2.fit(X, y)
        expected, expected_MSE = gpm2.predict(X, eval_MSE=True)

        self.assert_numpy_array_almost_equal(result.values, expected)
        self.assert_numpy_array_almost_equal(result_MSE.values, expected_MSE)
Пример #11
0
    def build_surrogate(self):
        """Builds a surrogate based on sample evalations using a Guassian process.

        Assumptions:
        None

        Source:
        N/A

        Inputs:
        self.training.
          coefficients     [-] CL and CD
          grid_points      [radians,-] angles of attack and mach numbers 

        Outputs:
        self.surrogates.
          lift_coefficient <Guassian process surrogate>
          drag_coefficient <Guassian process surrogate>

        Properties Used:
        No others
        """
        # Unpack data
        training = self.training
        AoA_data = training.angle_of_attack
        mach_data = training.Mach
        CL_data = training.coefficients[:, 0]
        CD_data = training.coefficients[:, 1]
        xy = training.grid_points

        # Gaussian Process New
        regr_cl = gaussian_process.GaussianProcess()
        regr_cd = gaussian_process.GaussianProcess()
        cl_surrogate = regr_cl.fit(xy, CL_data)
        cd_surrogate = regr_cd.fit(xy, CD_data)
        self.surrogates.lift_coefficient = cl_surrogate
        self.surrogates.drag_coefficient = cd_surrogate

        AoA_points = np.linspace(-3., 11., 100) * Units.deg
        mach_points = np.linspace(.02, .9, 100)

        AoA_mesh, mach_mesh = np.meshgrid(AoA_points, mach_points)

        CL_sur = np.zeros(np.shape(AoA_mesh))
        CD_sur = np.zeros(np.shape(AoA_mesh))

        return
Пример #12
0
def gaussianProcessing(trainFeature, trainLabel, testFeature, testLabel):
    gp = gaussian_process.GaussianProcess(theta0=1e-2,
                                          thetaL=1e-4,
                                          thetaU=1e-1)
    gp.fit(trainFeature, trainLabel)
    predictLabel, sigma2_pred = gp.predict(testFeature, eval_MSE=True)
    return (list(predictLabel * testLabel > 0).count(True) * 1.0 /
            len(testLabel))
Пример #13
0
    def __init__(self, isTrain):
        super(RegressionGaussianProcess, self).__init__(isTrain)
        # data preprocessing
        #self.dataPreprocessing()

        # Create Gaussian process object
        self.gp = gaussian_process.GaussianProcess(theta0=1e-2,
                                                   thetaL=1e-4,
                                                   thetaU=1e-1)
Пример #14
0
def gpTestcase():
    X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T
    y = f(X).ravel()
    x = np.atleast_2d(np.linspace(0, 10, 1000)).T
    gp = gaussian_process.GaussianProcess(theta0=1e-2,
                                          thetaL=1e-4,
                                          thetaU=1e-1)
    gp.fit(X, y)
    logging.debug(str(gp))
Пример #15
0
def validate_gp_scikitlearn():
    X = np.atleast_2d(np.linspace(-1, 1, 50)).T
    y = f(X).ravel() + 0.2 * np.random.standard_normal()
    x = np.atleast_2d(np.linspace(-1, 1, 1000)).T
    gp = gaussian_process.GaussianProcess(theta0=1e-2, thetaL=1e-4, thetaU=1e-1)
    gp.fit(X, y)
    y_pred, sigma2_pred = gp.predict(x, eval_MSE=True)
    print("MSE: ", np.mean( (f(x).ravel()-y_pred) ** 2))
    print("Sigma: ", np.mean(sigma2_pred))
Пример #16
0
    def fit_gaussian_process_baseline(self,
                                      fit_fraction=0.2,
                                      theta0=5.0,
                                      nugget=1.0,
                                      plot=True):
        """
        Gaussian Process fit of baseline.

        fit_fraction : float, default 0.2
            fraction of baseline to use for fitting the GP.
        theta0 : float 5.0, default 5.0
            The parameters in the autocorrelation model.
        nugget : float, default 1.0
            Introduce a nugget effect to allow smooth predictions from noisy data.
        plot : bool, default True
            Generate plots of the baseline fit

        :return:
        :rtype:
        """
        from sklearn import gaussian_process

        # Retrieve a reduced set of data
        # (data up until first injection and x percent before every injection)
        fit_indices, x, y = self._retrieve_fit_indices(fit_fraction)

        # sklearn requires a 2d array, so make it pseudo 2d
        full_x = numpy.atleast_2d(self.filter_period_end_time).T
        x = numpy.atleast_2d(x).T

        full_y = numpy.array(self.differential_power).T
        y = numpy.array(y).T

        # TODO look into GaussianProcessRegressor http://bit.ly/2kpUs0b
        # current API will be deprecated as of scikit learn 0.8
        gp = gaussian_process.GaussianProcess(regr='quadratic',
                                              corr='squared_exponential',
                                              theta0=theta0,
                                              nugget=nugget,
                                              random_start=100)

        # Fit only based on the reduced set of the data
        gp.fit(x, y)
        y_pred, mean_squared_error = gp.predict(full_x, eval_MSE=True)
        sigma = numpy.sqrt(mean_squared_error)

        self.baseline_power = Quantity(y_pred, 'microcalories per second')
        self.baseline_fit_data = {
            'x': full_x,
            'y': y_pred,
            'indices': fit_indices
        }
        self.baseline_subtracted = self.differential_power - self.baseline_power

        if plot:
            self._plot_gaussian_baseline(full_x, full_y, sigma, x, y, y_pred)
            self._plot_baseline_subtracted(full_x, self.baseline_subtracted)
Пример #17
0
def surrogate_search():
    ## sample three times

    iterations = 300
    epsilon = 0.1

    X, y = load_train_set()
    #X,y = sample(X,y,0.1)
    sc = Scorer(X, y)
    min_v = [5, 0.001, 0.1]
    max_v = [20, 0.1, 0.8]
    root_state = State(min_v, max_v, K=3)

    X = []
    y = []
    n_lims = np.array([min_v, max_v])
    extremes = list(itertools.product(*n_lims.T))
    for i, extreme in enumerate(extremes):
        #draw = root_state.sample()

        val = sc.score(extreme)
        print "Extreme", i, extreme, val
        X.append(extreme)
        y.append(val)

    for iteration in xrange(0, iterations):
        ## fit a regressor over the samples
        #clf = DecisionTreeRegressor()

        clf = gaussian_process.GaussianProcess()

        clf.fit(X, y)
        #print X, y, clf.predict(X)
        #exit()
        #plot_gp(X,y,root_state,clf)
        best_node = stoSOO(root_state,
                           10000,
                           lambda x: clf.predict([x])[0],
                           verbose=False,
                           det=False,
                           output_dir="./Optimisation")
        r = random.random()
        if (r > epsilon):
            draw = best_node.state.sample()
        else:
            draw = root_state.sample()

        val = sc.score(draw)
        X.append(draw)
        y.append(val)
        print "DRAW", draw, val

        print "Best So Far", best_node, best_node.depth, best_node.state._n_splits
        print "Surrogate Best", calc_best_from_node(best_node)
Пример #18
0
def GaussianProcessRegression(x_train, t_train, x_test, t_test):
    deDupe(x_train, t_train)
    gp = gaussian_process.GaussianProcess(theta0=1e-2,
                                          thetaL=1e-4,
                                          thetaU=1e-1)
    gp.fit(x_train, t_train)
    pred, sigma2_pred = gp.predict(x_test, eval_MSE=True)
    error = computeError(pred, t_test)
    save_model(gp, "GaussianProcessRegression", len(x_train), cal_only)
    return {
        "error": error,
        "sigma2": sum(sigma2_pred.tolist()) / len(sigma2_pred.tolist())
    }
Пример #19
0
def get_model(model_str):
    if model_str == 'ET':
        return ensemble.ExtraTreesRegressor(random_state=RANDOM_STATE)
    elif model_str == 'RF':
        return ensemble.RandomForestRegressor(random_state=RANDOM_STATE)
    elif model_str == 'GP':
        return gaussian_process.GaussianProcess(theta0=1e-2,
                                                thetaL=1e-4,
                                                thetaU=8e-1,
                                                random_state=RANDOM_STATE)
    elif model_str == 'KNN':
        return neighbors.KNeighborsRegressor()
    elif model_str == 'SVR':
        return svm.SVR()
Пример #20
0
 def __init__(self,
              n_outputs,
              regr='constant',
              corr='squared_exponential',
              storage_mode='full',
              verbose=False,
              theta0=1e-1):
     self.gps = [
         gaussian_process.GaussianProcess(regr=regr,
                                          corr=corr,
                                          storage_mode=storage_mode,
                                          verbose=verbose,
                                          theta0=theta0)
         for i in range(n_outputs)
     ]
Пример #21
0
def gp_grade(trn_data, test_data, L1_FEATURES=False, seed=100):
  gp = gaussian_process.GaussianProcess ( theta0=1e-2, 
                                          thetaL=1e-4, 
                                          thetaU=1e-1, 
                                          nugget = 0.2)
  
  def map(i):
    if i ==107:
      j = 0
    elif i == 24:
      j = 1
    elif i == 32:
      j = 2
    elif i == 4:
      j = 3
    elif i == 73:
      j = 4
    elif i == 88:
      j = 5
    elif i == 98:
      j = 7
    return j

  #L1=trn_data[2]
  #test_L1 = test_data[2]
  #L1_one_hot = np.zeros((L1.shape[0], 113), dtype=np.float32)
  #test_L1_one_hot  = np.zeros((test_L1.shape[0], 113), dtype=np.float32)
  
  #for i in L1:
  #  L1_one_hot[int(i)]=1.0
  
  
  #for i in test_L1:
  #  test_L1_one_hot[int(i)] = 1.0
  
  if L1_FEATURES:
    features = np.concatenate((trn_data[1], L1_one_hot), axis=1)
    print features.shape
    test_features = np.concatenate((test_data[1], test_L1_one_hot), axis=1)
    gp.fit(features, trn_data[0])
    predictions, variances = gp.predict (test_features, eval_MSE = True)
  else:
    gp.fit(trn_data[1], trn_data[0])
    predictions, variances = gp.predict (test_data[1], eval_MSE = True)
  
  
  variances = variances[:, np.newaxis]
  return predictions, variances    
Пример #22
0
def validate_gp_scikitlearn2():
    D = 1
    n_range = range(510, 9, -500)
    n_heldout = 1000
    fixed_x, fixed_y, z = parabola(n_heldout, D)

    for i in xrange(len(n_range)):
        n = n_range[i]
        training_x, training_y, z = parabola(n, D)
        y = training_y.flatten()
        gp = gaussian_process.GaussianProcess(theta0=1e-2, thetaL=1e-4, thetaU=1e-1)
        gp.fit(training_x, training_y)
        y_pred, sigma2_pred = gp.predict(fixed_x, eval_MSE=True)

        print("MSE: ", np.mean( (fixed_y.flatten()-y_pred) ** 2))
        print("Sigma: ", sigma2_pred)
Пример #23
0
def gaussProcPred(xTrain, yTrain, xTest, covar):
    xTrainAlter = []
    for i in range(1, len(xTrain)):
        tvec = xTrain[i - 1] + xTrain[i]
        xTrainAlter.append(tvec)
    xTestAlter = []
    xTestAlter.append(xTrain[len(xTrain) - 1] + xTest[0])
    for i in range(1, len(xTest)):
        tvec = xTest[i - 1] + xTest[i]
        xTestAlter.append(tvec)
    clfr = gaussian_process.GaussianProcess(theta0=1e-2,
                                            thetaL=1e-4,
                                            thetaU=1e-1,
                                            corr=covar)
    clfr.fit(xTrainAlter, yTrain[1:])
    return clfr.predict(xTestAlter, eval_MSE=True)[0]
Пример #24
0
def get_spatial_density(all_sites, dates, day_need):
    X = np.empty((0, 3))
    y = np.zeros((len(all_sites), 4))

    for i, (key, info) in enumerate(all_sites.iteritems()):
        utm_location = np.array([np.sqrt(info['utm'][0]**2 + info['utm'][1]**2), info['elev'], info['aspect']])
        X = np.vstack((X, utm_location))
        for j, temp_date in enumerate(dates):
            y[i, j] = info['data']['density'][temp_date]

    dem = gdal.Open('3m_data/Merced_500m_DEM.tif')
    dem_georeference = dem.GetGeoTransform()
    dem_raster = dem.ReadAsArray()
    dem_xRasterSize = dem.RasterXSize
    dem_yRasterSize = dem.RasterYSize
    aspect_raster = gdal.Open('3m_data/Merced_500m_ASP.tif').ReadAsArray()
    y_range = np.arange(dem_georeference[3] + 0.5 * dem_georeference[5], 
                        dem_georeference[3] + (dem_xRasterSize + 0.5) * dem_georeference[5], 
                        dem_georeference[5])
    x_range = np.arange(dem_georeference[0] + 0.5 * dem_georeference[1], 
                        dem_georeference[0] + (dem_yRasterSize + 0.5) * dem_georeference[1], 
                        dem_georeference[1])
    x_grid, y_grid = np.meshgrid(x_range, y_range)
    x_predict = np.column_stack((np.column_stack((np.sqrt((x_grid.flatten()**2 + y_grid.flatten()**2)), 
                                                  dem_raster.flatten())), 
                                 aspect_raster.flatten()))
    x_predict_not_nan_idx = np.where(x_predict[:, 1] > 100.)
    x_predict_new = x_predict[x_predict[:, 1] > 100.]

    imp = Imputer()
    y = imp.fit_transform(y)

    gp = gaussian_process.GaussianProcess(theta0=1e-2, thetaL=1e-4, 
                                          thetaU=1., nugget=0.01, optimizer='Welch', random_start=100)
    gp.fit(X, y[:, 2])
    april_y_total = np.zeros(len(x_predict))
    april_y_total[x_predict_not_nan_idx] = gp.predict(x_predict_new)
    april_y_total = april_y_total.reshape((dem_yRasterSize, dem_xRasterSize))
    
    gp.fit(X, y[:, 3])
    may_y_total = np.zeros(len(x_predict))
    may_y_total[x_predict_not_nan_idx] = gp.predict(x_predict_new)
    may_y_total = may_y_total.reshape((dem_yRasterSize, dem_xRasterSize))
    
    date_y_total = april_y_total + (may_y_total - april_y_total) / 30. * day_need
    
    return date_y_total
Пример #25
0
def main():
    # Create the dataset
    x_train = np.atleast_2d(np.linspace(-2, 2, num=50)).T
    y_train = f(x_train).ravel()
    x_test = np.atleast_2d(np.linspace(-5, 5, 1000)).T

    # Define the Regression Modell and fit it
    gp = gaussian_process.GaussianProcess(theta0=1e-2,
                                          thetaL=1e-4,
                                          thetaU=1e-1)
    gp.fit(x_train, y_train)

    # Evaluate the result
    y_pred, mse = gp.predict(x_test, eval_MSE=True)
    print("MSE: %0.4f" % sum(mse))
    print("max MSE: %0.4f" % max(mse))
    plot_graph(x_test, x_train, y_pred, mse, "x^2")
Пример #26
0
    def __init__(self, params, starts, goal, controller, state_dim=3):
        self._params = params
        self._controller = controller
        self._starts = starts
        self._goal = goal

        # setup the graph structure and internal variables
        self._g = StateGraph(state_dim=state_dim)
        self._best_trajs = []
        self._node_id = 0
        self._max_conc = 1.0
        self._max_es = 1.0
        self._min_es = 0.0

        self._gp = gaussian_process.GaussianProcess(corr='squared_exponential',
                                                    theta0=1e-2,
                                                    thetaL=1e-4,
                                                    thetaU=1e-1)
Пример #27
0
def fit(X, y, regr_model, initial_theta, adaptive):
    MACHINE_EPSILON = np.finfo(np.double).eps
    #注意theta初始化范围,若theta最小值太小,可能造成估计方差太大
    #将random_state调成10,从而提高使用cobyla对theta估计的准确性
    if adaptive == True:
        thetaL = 0.5 * initial_theta
        thetaU = 2 * initial_theta
    else:
        thetaL = 1e-8 * np.ones([1, DIMESSION], dtype=float)
        thetaU = 100 * np.ones([1, DIMESSION], dtype=float)
    gp = gaussian_process.GaussianProcess(regr=regr_model,
                                          normalize=True,
                                          theta0=initial_theta,
                                          thetaL=thetaL,
                                          thetaU=thetaU,
                                          nugget=50 * MACHINE_EPSILON,
                                          random_start=25)  # 不能使用Welch
    gp.fit(X, y)
    return gp
Пример #28
0
    def fit_gaussian_process_baseline(self, frac=0.1, theta0=4.7, nugget=1.0, plot=True):
        """
        Gaussian Process fit of baseline.

        frac = fraction of baseline to use for fit

        :return:
        :rtype:
        """
        from sklearn import gaussian_process

        # Retrieve a reduced set of data
        # (data up until first injection and x percent before every injection)
        fit_indices, x, y = self._retrieve_fit_indices(frac)

        # sklearn requires a 2d array, so make it pseudo 2d
        full_x = numpy.atleast_2d(self.filter_period_end_time).T
        x = numpy.atleast_2d(x).T

        full_y = numpy.array(self.differential_power).T
        y = numpy.array(y).T

        gp = gaussian_process.GaussianProcess(regr='quadratic', corr='squared_exponential', theta0=theta0,
                                              nugget=nugget,
                                              random_start=100)

        # Fit only based on the reduced set of the data
        gp.fit(x, y)
        y_pred, mean_squared_error = gp.predict(full_x, eval_MSE=True)
        sigma = numpy.sqrt(mean_squared_error)

        self.baseline_power = Quantity(y_pred, 'microcalories per second')
        self.baseline_fit_data = {'x': full_x, 'y': y_pred, 'indices': fit_indices}
        self.baseline_subtracted = self.differential_power - self.baseline_power

        if plot:
            self._plot_gaussian_baseline(full_x, full_y, sigma, x, y, y_pred)
            self._plot_baseline_subtracted(full_x, self.baseline_subtracted)
    def test_GaussianProcess_lt_017(self):
        # http://scikit-learn.org/stable/modules/gaussian_process.html
        X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T
        y = np.sin(X).ravel()
        df = pdml.ModelFrame(X, target=y)

        g1 = df.gaussian_process.GaussianProcess(theta0=1e-2,
                                                 thetaL=1e-4,
                                                 thetaU=1e-1)
        g2 = gp.GaussianProcess(theta0=1e-2, thetaL=1e-4, thetaU=1e-1)

        g1.fit(X, y)
        g2.fit(X, y)

        x = np.atleast_2d(np.linspace(0, 10, 1000)).T
        tdf = pdml.ModelFrame(x)

        y_result, sigma2_result = tdf.predict(g1, eval_MSE=True)
        y_expected, sigma2_expected = g2.predict(x, eval_MSE=True)

        self.assertIsInstance(y_result, pdml.ModelSeries)
        tm.assert_index_equal(y_result.index, tdf.index)

        self.assertIsInstance(sigma2_result, pdml.ModelSeries)
        tm.assert_index_equal(sigma2_result.index, tdf.index)

        self.assert_numpy_array_almost_equal(y_result.values, y_expected)
        self.assert_numpy_array_almost_equal(sigma2_result.values,
                                             sigma2_expected)

        y_result = tdf.predict(g1)
        y_expected = g2.predict(x)

        self.assertIsInstance(y_result, pdml.ModelSeries)
        tm.assert_index_equal(y_result.index, tdf.index)

        self.assert_numpy_array_almost_equal(y_result, y_expected)
Пример #30
0
#pop=[]
#fit=[]
#results=[]
#counts=[]

dim = 66
X = np.concatenate(
    (nprand.uniform(10.0, 30.0, [2, 6]), nprand.uniform(-0.3, 0.3, [2, 42]),
     nprand.uniform(0.0, 30.0, [2, 1]), [[-3.5] * 4] * 2,
     nprand.uniform(-30.0, 30.0, [2, 1]), nprand.uniform(
         0.0, 0.3, [2, 6]), nprand.uniform(5.0, 20.0, [2, 6])),
    axis=1)
maxprev = X[0]
y = np.asarray([f1(X[0]), f1(X[1])])
gp = gaussian_process.GaussianProcess(corr='linear',
                                      theta0=1e-2,
                                      thetaL=1e-4,
                                      thetaU=1e-1)
gp.fit(X, y)
param = genfromtxt('param.csv', delimiter=',')
best = f1(param)


def findMin():
    countFunc = 0
    prec = 16
    noVar = 66
    upper = 5.12
    lower = -5.12
    noIndi = 20
    windowSize = 5
    fprev = [0.0] * windowSize