def test_univariate_svd():
    w_exp = np.array([[9.1]])
    b_exp = np.array([-34.7])
    svd_lr = LinearRegression(method='svd')
    svd_lr.fit(X_rm, y)
    assert_almost_equal(svd_lr.w_, w_exp, decimal=1)
    assert_almost_equal(svd_lr.b_, b_exp, decimal=1)
Пример #2
0
def test_multivariate_normal_equation():
    w_exp = np.array([[5.1], [-0.6]])
    b_exp = np.array([-1.5])
    ne_lr = LinearRegression(minibatches=None)
    ne_lr.fit(X_rm_lstat, y)
    assert_almost_equal(ne_lr.w_, w_exp, decimal=1)
    assert_almost_equal(ne_lr.b_, b_exp, decimal=1)
Пример #3
0
def test_univariate_normal_equation():
    w_exp = np.array([[9.1]])
    b_exp = np.array([-34.7])
    ne_lr = LinearRegression(minibatches=None)
    ne_lr.fit(X_rm, y)
    assert_almost_equal(ne_lr.w_, w_exp, decimal=1)
    assert_almost_equal(ne_lr.b_, b_exp, decimal=1)
Пример #4
0
def test_progress_3():
    gd_lr = LinearRegression(minibatches=1,
                             eta=0.001,
                             epochs=1,
                             print_progress=2,
                             random_seed=0)
    gd_lr.fit(X_rm_std, y_std)
Пример #5
0
def test_univariate_stochastic_gradient_descent():
    sgd_lr = LinearRegression(minibatches=len(y),
                              eta=0.0001,
                              epochs=100,
                              random_seed=0)
    sgd_lr.fit(X_rm_std, y_std)
    assert_almost_equal(sgd_lr.w_, expect_rm_std, decimal=2)
Пример #6
0
def test_multivariate_stochastic_gradient_descent():
    sgd_lr = LinearRegression(eta=0.0001,
                              epochs=500,
                              solver='sgd',
                              random_seed=0)
    sgd_lr.fit(X_rm_lstat_std, y_std)
    assert_almost_equal(sgd_lr.w_, expect_rm_lstat_std, decimal=2)
Пример #7
0
def test_univariate_normal_equation_std():
    w_exp = np.array([[0.7]])
    b_exp = np.array([0.0])
    ne_lr = LinearRegression(minibatches=None)
    ne_lr.fit(X_rm_std, y_std)
    assert_almost_equal(ne_lr.w_, w_exp, decimal=1)
    assert_almost_equal(ne_lr.b_, b_exp, decimal=1)
Пример #8
0
def test_multivariate_gradient_descent():
    gd_lr = LinearRegression(eta=0.001,
                             epochs=500,
                             minibatches=1,
                             random_seed=0)
    gd_lr.fit(X_rm_lstat_std, y_std)
    assert_almost_equal(gd_lr.w_, expect_rm_lstat_std, decimal=3)
def test_multivariate_svd():
    w_exp = np.array([[5.1], [-0.6]])
    b_exp = np.array([-1.5])
    svd_lr = LinearRegression(method='svd')
    svd_lr.fit(X_rm_lstat, y)
    assert_almost_equal(svd_lr.w_, w_exp, decimal=1)
    assert_almost_equal(svd_lr.b_, b_exp, decimal=1)
Пример #10
0
def test_ary_persistency_in_shuffling():
    orig = X_rm_lstat_std.copy()
    sgd_lr = LinearRegression(eta=0.0001,
                              epochs=500,
                              minibatches=len(y),
                              random_seed=0)
    sgd_lr.fit(X_rm_lstat_std, y_std)
    np.testing.assert_almost_equal(orig, X_rm_lstat_std, 6)
Пример #11
0
def test_multivariate_gradient_descent():
    w_exp = np.array([[0.4], [-0.5]])
    b_exp = np.array([0.0])
    gd_lr = LinearRegression(eta=0.001,
                             epochs=500,
                             minibatches=1,
                             random_seed=0)
    gd_lr.fit(X_rm_lstat_std, y_std)
    assert_almost_equal(gd_lr.w_, w_exp, decimal=1)
    assert_almost_equal(gd_lr.b_, b_exp, decimal=1)
Пример #12
0
def test_univariate_stochastic_gradient_descent():
    w_exp = np.array([[0.7]])
    b_exp = np.array([0.0])
    sgd_lr = LinearRegression(minibatches=len(y),
                              eta=0.0001,
                              epochs=150,
                              random_seed=0)
    sgd_lr.fit(X_rm_std, y_std)
    assert_almost_equal(sgd_lr.w_, w_exp, decimal=1)
    assert_almost_equal(sgd_lr.b_, b_exp, decimal=1)
Пример #13
0
def test_multivariate_stochastic_gradient_descent():
    w_exp = np.array([[0.389], [-0.499]])
    b_exp = np.array([0.000])
    sgd_lr = LinearRegression(eta=0.0001,
                              epochs=500,
                              minibatches=len(y),
                              random_seed=0)
    sgd_lr.fit(X_rm_lstat_std, y_std)
    assert_almost_equal(sgd_lr.w_, w_exp, decimal=3)
    assert_almost_equal(sgd_lr.b_, b_exp, decimal=3)
Пример #14
0
def test_univariate_gradient_descent():
    w_exp = np.array([[0.695]])
    b_exp = np.array([0.00])
    gd_lr = LinearRegression(minibatches=1,
                             eta=0.001,
                             epochs=500,
                             random_seed=0)
    gd_lr.fit(X_rm_std, y_std)
    assert_almost_equal(gd_lr.w_, w_exp, decimal=3)
    assert_almost_equal(gd_lr.b_, b_exp, decimal=3)
def test_clone():
    regr = LinearRegression()
    clone(regr)
Пример #16
0
def test_multivariate_normal_equation():
    ne_lr = LinearRegression(solver='normal equation')
    ne_lr.fit(X_rm_lstat, y)
    assert_almost_equal(ne_lr.w_, expect_rm_lstat, decimal=3)
Пример #17
0
def test_univariate_gradient_descent():
    gd_lr = LinearRegression(solver='gd', eta=0.001, epochs=500, random_seed=0)
    gd_lr.fit(X_rm_std, y_std)
    assert_almost_equal(gd_lr.w_, expect_rm_std, decimal=3)
Пример #18
0
rowdata = pd.read_csv('data/normal/爱情字典-287637.csv')
year = rowdata.year
month = rowdata.month
day = rowdata.day

date = {}
for i in range(len(year)):
    da = f'{year[i]}-{month[i]}-{day[i]}'
    if da not in date.keys():
        date[da] = 1
    else:
        date[da] += 1

points = [(key, value) for key, value in date.items()][::-1]
gd_lr = LinearRegression()
x_ = [float(time.mktime(time.strptime(x[0], "%Y-%m-%d"))) for x in points]
y_ = [float(y[1]) for y in points]
gd_lr.fit(np.array(x_)[:, np.newaxis], np.array(y_))
x_axis = [time.strftime("%Y-%m-%d", time.localtime(i)) for i in x_]

print(x_axis[::18])
plt.rcParams['font.sans-serif'] = ['simhei']  #设置字体
plt.figure(figsize=[12, 8])
plt.title('回归模型')
plt.scatter(x_axis, y_, alpha=0.4, edgecolors='white')
#plt.xticks(range(7), [2013,2014,2015,2016,2017,2018,2019])
#plt.yticks(y_, fontsize=9)
plt.plot(x_axis, gd_lr.predict(np.array(x_)[:, np.newaxis]), color='gray')
ax = plt.gca()
ax.spines['right'].set_color('none')
Пример #19
0
                        usecols=[0, 1, 2, 3],
                        nrows=data_size)

X = dataframe[["Feature 1", "Feature 2", "Feature 3"]]
Y = dataframe["Target"]

print(set_sizes[0])
print('here', set_sizes[nrows] * 0.7)

X_train = X.head(int(set_sizes[nrows] * 0.7))
X_test = X.tail(int(set_sizes[nrows] * 0.3))

Y_train = Y.head(int(set_sizes[nrows] * 0.7))
Y_test = Y.tail(int(set_sizes[nrows] * 0.3))

ne_lr = LinearRegression(minibatches=None)
Y2 = pd.to_numeric(Y, downcast='float')
print("here", type((Y2)))

print(type(Y_train))

ne_lr.fit(X_train, pd.to_numeric(Y_train, downcast='float'))

print(ne_lr)

y_pred = ne_lr.predict(X_test)

res = mean_squared_error(Y_test, y_pred)
#res = scoring(y_target=Y_test, y_predicted=y_pred, metric='rmse')
print("results: ", res)
Пример #20
0
def test_multivariate_normal_equation():
    ne_lr = LinearRegression(minibatches=None)
    ne_lr.fit(X_rm_lstat, y)
    assert_almost_equal(ne_lr.w_, expect_rm_lstat, decimal=3)
Пример #21
0
and test and compare them on linear regression over your synthetic data-points. 
You need not to perform a cross-validation scheme here; only use the whole data set as your training set. 
"""

X = np.asanyarray(x).reshape(
    -1, 1
)  #x need to be converted into matrix without changing the array values to fit the model
eta1 = 0.0001
eta2 = 0.1

from mlxtend.regressor import LinearRegression
from sklearn import metrics

ada1_bgd = LinearRegression(method='sgd',
                            eta=eta1,
                            epochs=20,
                            random_seed=0,
                            minibatches=1)  #for adalline bgd
ada1_bgd.fit(X, y)
y_pred = ada1_bgd.predict(X)
mse1 = metrics.mean_squared_error(y_pred, y)
ada2_bgd = LinearRegression(method='sgd',
                            eta=eta2,
                            epochs=20,
                            random_seed=0,
                            minibatches=1)  #for adaline bgd
ada2_bgd.fit(X, y)
y_pred = ada2_bgd.predict(X)
mse2 = metrics.mean_squared_error(y_pred, y)
print("Adaline Batch Gradient Descent Regression Algorithm")
print("-----------------------------------------------------")
Пример #22
0
def test_univariate_normal_equation_std():
    ne_lr = LinearRegression(solver='normal equation')
    ne_lr.fit(X_rm_std, y_std)
    assert_almost_equal(ne_lr.w_, expect_rm_std, decimal=3)
Пример #23
0
    predictions[np.where(np.logical_and(predictions > coefs[1], predictions <= coefs[2]))] = 2
    predictions[predictions > coefs[2]] = 3
    
    return predictions.astype('int')




# Define models for the first ensemble (using group k-fold)
model1 = make_regressor(clf='cat')

model2 = make_regressor(clf='xgb')

model3 = make_regressor(clf='lgb')

lr = LinearRegression()

stack_one = StackingCVRegressor(regressors=[model1, model2, model3], 
                            meta_regressor=lr, 
                            cv=5,
                            verbose=0,
                            store_train_meta_features=True,
                            random_state=SEED)

stack_one.fit(X, y, groups=groups)

## Return ordinal predictions
pr1 = stack_one.predict(X)
optR = OptimizedRounder()
coefs_one = get_coef(stack_one, X, y)
#print(coefs_one)
Пример #24
0
def test_univariate_normal_equation_std():
    ne_lr = LinearRegression(minibatches=None)
    ne_lr.fit(X_rm_std, y_std)
    assert_almost_equal(ne_lr.w_, expect_rm_std, decimal=3)