【发布时间】:2015-02-13 20:42:49
【问题描述】:
我有一个Xs 的列表及其输出值Ys。并使用以下代码,我可以训练以下回归器:
- 线性回归器
- 等渗回归器
- 贝叶斯岭回归器
- 梯度提升回归器
代码:
import numpy as np
from sklearn.linear_model import LinearRegression, BayesianRidge
from sklearn.isotonic import IsotonicRegression
from sklearn import ensemble
from sklearn.svm import SVR
from sklearn.gaussian_process import GaussianProcess
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
def get_meteor_scores(infile):
with io.open(infile, 'r') as fin:
meteor_scores = [float(i.strip().split()[-1]) for
i in re.findall(r'Segment [0-9].* score\:.*\n',
fin.read())]
return meteor_scores
def get_sts_scores(infile):
with io.open(infile, 'r') as fin:
sts_scores = [float(i) for i in fin]
return sts_scores
Xs = 'meteor.output.train'
Ys = 'score.train'
# Gets scores from https://raw.githubusercontent.com/alvations/USAAR-SemEval-2015/master/task02-USAAR-SHEFFIELD/x.meteor.train
meteor_scores = np.array(get_meteor_scores(Xs))
# Gets scores from https://raw.githubusercontent.com/alvations/USAAR-SemEval-2015/master/task02-USAAR-SHEFFIELD/score.train
sts_scores = np.array(get_sts_scores(Ys))
x = meteor_scores
y = sts_scores
n = len(sts_scores)
# Linear Regression
lr = LinearRegression()
lr.fit(x[:, np.newaxis], y)
# Baysian Ridge Regression
br = BayesianRidge(compute_score=True)
br.fit(x[:, np.newaxis], y)
# Isotonic Regression
ir = IsotonicRegression()
y_ = ir.fit_transform(x, y)
# Gradient Boosting Regression
params = {'n_estimators': 500, 'max_depth': 4, 'min_samples_split': 1,
'learning_rate': 0.01, 'loss': 'ls'}
gbr = ensemble.GradientBoostingRegressor(**params)
gbr.fit(x[:, np.newaxis], y)
但是如何为Support Vector Regression、Gaussian Process 和Decision Tree Regressor 训练回归器?
当我尝试以下方法训练 Support Vector Regressors 时,出现错误:
from sklearn.svm import SVR
# Support Vector Regressions
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
svr_lin = SVR(kernel='linear', C=1e3)
svr_poly = SVR(kernel='poly', C=1e3, degree=2)
y_rbf = svr_rbf.fit(x, y)
y_lin = svr_lin.fit(x, y)
y_poly = svr_poly.fit(x, y)
[出]:
Traceback (most recent call last):
File "/home/alvas/git/USAAR-SemEval-2015/task02-somethingLiddat/carolling.py", line 47, in <module>
y_rbf = svr_rbf.fit(x, y)
File "/home/alvas/.local/lib/python2.7/site-packages/sklearn/svm/base.py", line 149, in fit
(X.shape[0], y.shape[0]))
ValueError: X and y have incompatible shapes.
X has 1 samples, but y has 10597.
当我尝试Gaussian Process时发生同样的情况:
from sklearn.gaussian_process import GaussianProcess
# Gaussian Process
gp = GaussianProcess(corr='squared_exponential', theta0=1e-1,
thetaL=1e-3, thetaU=1,
random_start=100)
gp.fit(x, y)
[出]:
Traceback (most recent call last):
File "/home/alvas/git/USAAR-SemEval-2015/task02-somethingLiddat/carolling.py", line 57, in <module>
gp.fit(x, y)
File "/home/alvas/.local/lib/python2.7/site-packages/sklearn/gaussian_process/gaussian_process.py", line 271, in fit
X, y = check_arrays(X, y)
File "/home/alvas/.local/lib/python2.7/site-packages/sklearn/utils/validation.py", line 254, in check_arrays
% (size, n_samples))
ValueError: Found array with dim 10597. Expected 1
运行gp.fit(x[:,np.newaxis], y) 时出现此错误:
Traceback (most recent call last):
File "/home/alvas/git/USAAR-SemEval-2015/task02-somethingLiddat/carolling.py", line 95, in <module>
gp.fit(x[:,np.newaxis], y)
File "/home/alvas/.local/lib/python2.7/site-packages/sklearn/gaussian_process/gaussian_process.py", line 301, in fit
raise Exception("Multiple input features cannot have the same"
Exception: Multiple input features cannot have the same target value.
当我尝试Decision Tree Regressor:
from sklearn.tree import DecisionTreeRegressor
# Decision Tree Regression
dtr2 = DecisionTreeRegressor(max_depth=2)
dtr5 = DecisionTreeRegressor(max_depth=2)
dtr2.fit(x,y)
dtr5.fit(x,y)
[出]:
Traceback (most recent call last):
File "/home/alvas/git/USAAR-SemEval-2015/task02-somethingLiddat/carolling.py", line 47, in <module>
dtr2.fit(x,y)
File "/home/alvas/.local/lib/python2.7/site-packages/sklearn/tree/tree.py", line 140, in fit
n_samples, self.n_features_ = X.shape
ValueError: need more than 1 value to unpack
【问题讨论】:
标签: python machine-learning nlp scikit-learn regression