2014-12-15 3 views
2

У меня есть список Xs и их выходное значение Ys. И, используя следующий код, я могу обучить следующие регрессоров:Обучение различных регрессоров с sklearn

  • Linear REGRESSOR
  • Изотонический REGRESSOR
  • Baysian Ридж REGRESSOR
  • Gradient Повышая REGRESSOR

Код:

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 

ответ

2

Все эти регрессии требуют многомерного x-массива, но ваш x-array - массив 1D. Так что только требование состоит в том, чтобы преобразовать массив в массив 2D для работы этих регрессоров. Это может быть достигнуто с помощью x[:, np.newaxis]

Демо:

>>> 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) 
>>> x=np.arange(10) 
>>> y=np.arange(10) 
>>> y_rbf = svr_rbf.fit(x[:,np.newaxis], y) 
>>> y_lin = svr_lin.fit(x[:,np.newaxis], y) 
>>> svr_poly = svr_poly.fit(x[:,np.newaxis], y) 
>>> 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[:, np.newaxis], y) 
GaussianProcess(beta0=None, 
     corr=<function squared_exponential at 0x7f46f3ebcf50>, 
     normalize=True, nugget=array(2.220446049250313e-15), 
     optimizer='fmin_cobyla', random_start=100, 
     random_state=<mtrand.RandomState object at 0x7f4702d97150>, 
     regr=<function constant at 0x7f46f3ebc8c0>, storage_mode='full', 
     theta0=array([[ 0.1]]), thetaL=array([[ 0.001]]), 
     thetaU=array([[1]]), verbose=False) 
>>> from sklearn.tree import DecisionTreeRegressor 
>>> # Decision Tree Regression 
... dtr2 = DecisionTreeRegressor(max_depth=2) 
>>> dtr5 = DecisionTreeRegressor(max_depth=2) 
>>> dtr2.fit(x[:,np.newaxis],y) 
DecisionTreeRegressor(compute_importances=None, criterion='mse', max_depth=2, 
      max_features=None, min_density=None, min_samples_leaf=1, 
      min_samples_split=2, random_state=None, splitter='best') 
>>> dtr5.fit(x[:,np.newaxis],y) 
DecisionTreeRegressor(compute_importances=None, criterion='mse', max_depth=2, 
      max_features=None, min_density=None, min_samples_leaf=1, 
      min_samples_split=2, random_state=None, splitter='best') 

Препроцессирование для GaussianProcess:

xu = np.unique(x) # get unique x values 
idx = [np.where(x==x1)[0][0] for x1 in xu] # get corresponding indices for unique x values 
gp.fit(xu[:,np.newaxis], y[idx]) # y[idx] selects y values corresponding to unique x values 
+0

Я получаю сообщение об ошибке с 'gp.fit (x [:, np.newaxis], y)', т. Е.Исключение: несколько входных функций не могут иметь одно и то же целевое значение. – alvas

+0

'y [np.searchsorted (x, np.unique (x))]' получает: 'IndexError: индекс 10597 выходит за пределы для оси 1 с размером 10597' – alvas

+0

Извините за описанный выше метод. Это неверно. Я загрузил правильный метод в свой ответ. Проверьте это. –

1

Multiple input features cannot have the same target value.

Это означает, что одна точка данных повторяется в ваших входных данных, и гауссовский процесс не позволяет дважды указывать одну точку данных. К сожалению, ваш набор данных больше недоступен, поэтому я не могу проверить это, но это то, что я думаю, должно быть так.

Смежные вопросы