Exemple #1
0
def test_PolynomialRegression_simple():
    x = np.arange(10.).reshape((10, 1))
    y = np.arange(10.)
    dy = 1

    clf = PolynomialRegression(2).fit(x, y, dy)
    y_true = clf.predict(x)

    assert_allclose(y, y_true, atol=1E-10)
def test_PolynomialRegression_simple():
    x = np.arange(10.).reshape((10, 1))
    y = np.arange(10.)
    dy = 1

    clf = PolynomialRegression(2).fit(x, y, dy)
    y_true = clf.predict(x)

    assert_allclose(y, y_true, atol=1E-10)
Exemple #3
0
def gen_vdata_poly(entry, npoly):

    data = entry.Data
    Rd = entry.Rdisk
    Rp = entry.Vb_peak_location

    z_sample = data.R
    z_fit = np.array([0.5, 1, 2, 0.5 * Rd, Rd, 2 * Rd, 0.5 * Rp, Rp, 2 * Rp])
    sample = data.V
    error = np.sqrt(data.e_V**2 + 2**2)

    V_spline = InterpolatedUnivariateSpline(z_sample, sample)
    V_spline_fit = V_spline(z_fit)

    n_poly = min(npoly, len(sample) - 2)
    clf = PolynomialRegression(n_poly)
    clf.fit(z_sample[:, None], sample, error)
    y_fit = clf.predict(z_fit[:, None])
    sample_fit = clf.predict(z_sample[:, None])

    z_fine = np.logspace(np.log10(data.R[0]), np.log10(data.R[-1]), 100)
    y_fine = clf.predict(z_fine[:, None])
    pos = np.argmax(y_fine)
    vmax = y_fine[pos]
    rmax = z_fine[pos]

    chi2_dof = (np.sum(
        ((sample_fit - sample) / error)**2) / (len(sample) - n_poly - 1))

    dy_pred = (clf.predict(z_fit[:, None] * 1.05) -
               clf.predict(z_fit[:, None] * 0.95)) / (z_fit * 0.1)

    ddy_pred = (clf.predict(z_fit[:, None] * 1.05) +
                clf.predict(z_fit[:, None] * 0.95) - 2 * y_fit) / (
                    (z_fit * 0.05)**2)

    s2 = sample_fit**2 - data.Gas**2 - entry.ML_maxbulge * data.Bulge**2 - min(
        entry.ML_maxdisk, 0.2) * data.Disk**2
    s2 = np.array([max(item, 0.0) for item in s2])
    Vdm_max = InterpolatedUnivariateSpline(z_sample, np.sqrt(s2))
    s2 = sample_fit**2 - data.Gas**2 - entry.ML_maxbulge * data.Bulge**2 - min(
        entry.ML_maxdisk, 0.4) * data.Disk**2
    s2 = np.array([max(item, 0.0) for item in s2])
    Vdm_med = InterpolatedUnivariateSpline(z_sample, np.sqrt(s2))
    s2 = sample_fit**2 - data.Gas**2 - entry.ML_maxbulge * data.Bulge**2 - min(
        entry.ML_maxdisk, 0.6) * data.Disk**2
    s2 = np.array([max(item, 0.0) for item in s2])
    Vdm_min = InterpolatedUnivariateSpline(z_sample, np.sqrt(s2))

    return vmax,rmax,chi2_dof,y_fit,dy_pred,ddy_pred,sample_fit,\
            Vdm_med,Vdm_max,Vdm_min,V_spline,V_spline_fit
Exemple #4
0
# Generate data
z_sample, mu_sample, dmu = generate_mu_z(100, random_state=0)

cosmo = Cosmology()
z = np.linspace(0.01, 2, 1000)
mu_true = np.asarray(map(cosmo.mu, z))

#------------------------------------------------------------
# Define our classifiers
basis_mu = np.linspace(0, 2, 15)[:, None]
basis_sigma = 3 * (basis_mu[1] - basis_mu[0])

subplots = [221, 222, 223, 224]
classifiers = [
    LinearRegression(),
    PolynomialRegression(4),
    BasisFunctionRegression('gaussian', mu=basis_mu, sigma=basis_sigma),
    NadarayaWatson('gaussian', h=0.1)
]
text = [
    'Straight-line Regression', '4th degree Polynomial\n Regression',
    'Gaussian Basis Function\n Regression', 'Gaussian Kernel\n Regression'
]

# number of constraints of the model.  Because
# Nadaraya-watson is just a weighted mean, it has only one constraint
n_constraints = [2, 5, len(basis_mu) + 1, 1]

#------------------------------------------------------------
# Plot the results
fig = plt.figure(figsize=(8, 8))
def fit_Polynomial(features_train, labels_train, features_pred, order=3):
	model = PolynomialRegression(order)
	model.fit(features_train, labels_train)
	labels_pred = model.predict(features_pred)
	return labels_pred