本文整理汇总了Python中sklearn.linear_model.PassiveAggressiveRegressor.partial_fit方法的典型用法代码示例。如果您正苦于以下问题:Python PassiveAggressiveRegressor.partial_fit方法的具体用法?Python PassiveAggressiveRegressor.partial_fit怎么用?Python PassiveAggressiveRegressor.partial_fit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model.PassiveAggressiveRegressor
的用法示例。
在下文中一共展示了PassiveAggressiveRegressor.partial_fit方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_regressor_partial_fit
# 需要导入模块: from sklearn.linear_model import PassiveAggressiveRegressor [as 别名]
# 或者: from sklearn.linear_model.PassiveAggressiveRegressor import partial_fit [as 别名]
def test_regressor_partial_fit():
y_bin = y.copy()
y_bin[y != 1] = -1
for data in (X, X_csr):
reg = PassiveAggressiveRegressor(C=1.0,
fit_intercept=True,
random_state=0)
for t in range(50):
reg.partial_fit(data, y_bin)
pred = reg.predict(data)
assert_less(np.mean((pred - y_bin) ** 2), 1.7)
示例2: test_regressor_partial_fit
# 需要导入模块: from sklearn.linear_model import PassiveAggressiveRegressor [as 别名]
# 或者: from sklearn.linear_model.PassiveAggressiveRegressor import partial_fit [as 别名]
def test_regressor_partial_fit():
y_bin = y.copy()
y_bin[y != 1] = -1
for data in (X, X_csr):
for average in (False, True):
reg = PassiveAggressiveRegressor(
C=1.0, fit_intercept=True, random_state=0,
average=average, max_iter=100)
for t in range(50):
reg.partial_fit(data, y_bin)
pred = reg.predict(data)
assert_less(np.mean((pred - y_bin) ** 2), 1.7)
if average:
assert hasattr(reg, 'average_coef_')
assert hasattr(reg, 'average_intercept_')
assert hasattr(reg, 'standard_intercept_')
assert hasattr(reg, 'standard_coef_')
示例3: refit_from_scratch
# 需要导入模块: from sklearn.linear_model import PassiveAggressiveRegressor [as 别名]
# 或者: from sklearn.linear_model.PassiveAggressiveRegressor import partial_fit [as 别名]
def refit_from_scratch(self):
temp_model = PassiveAggressiveRegressor()
temp_enc = CountVectorizer()
X = [] # binary matrix the presence of tags
Z = [] # additional numerical data
Y = [] # target (to predict) values
db_size = self.db.size()
for data in self.db.yield_all():
feedback = data["feedback"]
tags = data[ "tags" ]
if feedback and tags:
Y.append( feedback )
X.append(" ".join(tags))
Z.append(self.fmt_numerical(data))
X = temp_enc.fit_transform(X)
X = hstack((X, coo_matrix(Z)))
self.allX = X
for i in range(X.shape[0]):
temp_model.partial_fit(X.getrow(i), [Y[0]])
self.model = temp_model
self.enc = temp_enc
示例4: main
# 需要导入模块: from sklearn.linear_model import PassiveAggressiveRegressor [as 别名]
# 或者: from sklearn.linear_model.PassiveAggressiveRegressor import partial_fit [as 别名]
def main():
X, y, coef = make_regression(1000, 200, 10, 1, noise=0.05, coef=True,
random_state=42)
# X = np.column_stack((X, np.ones(X.shape[0])))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
# sca = StandardScaler()
# sca.fit(X_train)
# X_train = sca.transform(X_train)
# X_test = sca.transform(X_test)
# print X.shape
# print y.shape
# print coef.shape
param_grid = {
"C": [0.0000001, 0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 10,
100, 1000],
"epsilon": [0.0001, 0.001, 0.01, 0.1]}
param_grid_kern = {
"C": [0.0000001, 0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 10,
100, 1000],
"epsilon": [0.0001, 0.001, 0.01, 0.1],
"gamma": [0.00001, 0.0001, 0.001, 0.01, 0.1, 1, 10, 100]}
# "loss": ["pa", "pai", "paii"]}}
my_pa = PARegressor(loss="paii", C=1, epsilon=0.001, n_iter=1,
fit_intercept=False)
#
# search = GridSearchCV(my_pa, param_grid,
# scoring='mean_absolute_error', n_jobs=8, iid=True, refit=True, cv=5,
# verbose=1)
# search.fit(X_train, y_train)
# print search.best_params_
my_pa.fit(X_train, y_train)
print my_pa.coef_
# y_preds = search.predict(X_test)
y_preds = my_pa.predict(X_test)
mae_my_pa = mean_absolute_error(y_test, y_preds)
print "My PA MAE = %2.4f" % mae_my_pa
my_kpa_linear = KernelPARegressor(kernel="linear", loss="paii", C=1, epsilon=0.001, n_iter=1, fit_intercept=False)
my_kpa_linear.fit(X_train, y_train)
print "alphas", len(my_kpa_linear.alphas_), my_kpa_linear.alphas_
y_preds = my_kpa_linear.predict(X_test)
mae_kpa_linear = mean_absolute_error(y_test, y_preds)
print "My KPA linear MAE = %2.4f" % mae_kpa_linear
my_kpa_rbf = KernelPARegressor(kernel="rbf", loss="paii", gamma=0.001, C=1, epsilon=0.001, n_iter=1, fit_intercept=False)
# search = GridSearchCV(my_kpa_rbf, param_grid_kern,
# scoring='mean_absolute_error', n_jobs=8, iid=True, refit=True, cv=5,
# verbose=1)
# search.fit(X_train, y_train)
my_kpa_rbf.fit(X_train, y_train)
print "alphas", len(my_kpa_rbf.alphas_), my_kpa_rbf.alphas_
print "support", len(my_kpa_rbf.support_)
# print "alphas", len(search.best_estimator_.alphas_) # , my_kpa_rbf.alphas_
# print "support", len(search.best_estimator_.support_)
# print search.best_params_
y_preds = my_kpa_rbf.predict(X_test)
# y_preds = search.predict(X_test)
mae_my_kpa = mean_absolute_error(y_test, y_preds)
print "My Kernel PA MAE = %2.4f" % mae_my_kpa
# print search.best_estimator_
# print np.corrcoef(search.best_estimator_.coef_, coef)
# param_grid = {
# "C": [0.001, 0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 10,
# 100, 1000, 10000],
# "epsilon": [0.0001, 0.001, 0.01, 0.1],
# # "loss": ["epsilon_insensitive", "squared_epsilon_insensitive"]}
# "loss": ["squared_epsilon_insensitive"]}
# search = GridSearchCV(PassiveAggressiveRegressor(fit_intercept=True),
# param_grid, scoring='mean_absolute_error', n_jobs=8, iid=True,
# refit=True, cv=5, verbose=1)
# search.fit(X_train, y_train)
sk_pa = PassiveAggressiveRegressor(loss="squared_epsilon_insensitive", C=1,
epsilon=0.001, n_iter=1,
fit_intercept=False,
warm_start=True)
for i in xrange(X_train.shape[0]):
# for x_i, y_i in zip(X_train, y_train):
x = np.array(X_train[i], ndmin=2)
y = np.array(y_train[i], ndmin=1)
# print x.shape
# print y
sk_pa.partial_fit(x, y)
#.........这里部分代码省略.........