2015-10-27 1 views
1

J'essaie de faire une régression Ridge pondérée avec sklearn. Toutefois, le code se casse lorsque j'appelle la méthode d'ajustement. L'exception que je reçois est:sklearn RidgeCV avec sample_weight

Exception: Data must be 1-dimensional 

Mais je suis sûr (en vérifiant par impression-déclarations) que les données que je suis passe a les bonnes formes.

print temp1.shape  #(781, 21) 
print temp2.shape  #(781,) 
print weights.shape  #(781,) 

result=RidgeCV(normalize=True).fit(temp1,temp2,sample_weight=weights) 

Qu'est-ce qui pourrait mal tourner ??

est ici toute la production:

--------------------------------------------------------------------------- 
Exception         Traceback (most recent call last) 
<ipython-input-65-a5b1eba5d9cf> in <module>() 
    22 
    23 
---> 24  result=RidgeCV(normalize=True).fit(temp2,temp1, sample_weight=weights) 
    25 
    26 

/usr/local/lib/python2.7/dist-packages/sklearn/linear_model/ridge.pyc in fit(self, X, y, sample_weight) 
    868         gcv_mode=self.gcv_mode, 
    869         store_cv_values=self.store_cv_values) 
--> 870    estimator.fit(X, y, sample_weight=sample_weight) 
    871    self.alpha_ = estimator.alpha_ 
    872    if self.store_cv_values: 

/usr/local/lib/python2.7/dist-packages/sklearn/linear_model/ridge.pyc in fit(self, X, y, sample_weight) 
    793        else alpha) 
    794    if error: 
--> 795     out, c = _errors(weighted_alpha, y, v, Q, QT_y) 
    796    else: 
    797     out, c = _values(weighted_alpha, y, v, Q, QT_y) 

/usr/local/lib/python2.7/dist-packages/sklearn/linear_model/ridge.pyc in _errors(self, alpha, y, v, Q, QT_y) 
    685   w = 1.0/(v + alpha) 
    686   c = np.dot(Q, self._diag_dot(w, QT_y)) 
--> 687   G_diag = self._decomp_diag(w, Q) 
    688   # handle case where y is 2-d 
    689   if len(y.shape) != 1: 

/usr/local/lib/python2.7/dist-packages/sklearn/linear_model/ridge.pyc in _decomp_diag(self, v_prime, Q) 
    672  def _decomp_diag(self, v_prime, Q): 
    673   # compute diagonal of the matrix: dot(Q, dot(diag(v_prime), Q^T)) 
--> 674   return (v_prime * Q ** 2).sum(axis=-1) 
    675 
    676  def _diag_dot(self, D, B): 

/usr/local/lib/python2.7/dist-packages/pandas/core/ops.pyc in wrapper(left, right, name) 
    531    return left._constructor(wrap_results(na_op(lvalues, rvalues)), 
    532          index=left.index, name=left.name, 
--> 533          dtype=dtype) 
    534  return wrapper 
    535 

/usr/local/lib/python2.7/dist-packages/pandas/core/series.pyc in __init__(self, data, index, dtype, name, copy, fastpath) 
    209    else: 
    210     data = _sanitize_array(data, index, dtype, copy, 
--> 211          raise_cast_failure=True) 
    212 
    213     data = SingleBlockManager(data, index, fastpath=True) 

/usr/local/lib/python2.7/dist-packages/pandas/core/series.pyc in _sanitize_array(data, index, dtype, copy, raise_cast_failure) 
    2683  elif subarr.ndim > 1: 
    2684   if isinstance(data, np.ndarray): 
-> 2685    raise Exception('Data must be 1-dimensional') 
    2686   else: 
    2687    subarr = _asarray_tuplesafe(data, dtype=dtype) 

Exception: Data must be 1-dimensional 

Répondre

4

L'erreur semble être due à sample_weights étant une série Pandas plutôt qu'un tableau numpy:

from sklearn.linear_model import RidgeCV 

temp1 = pd.DataFrame(np.random.rand(781, 21)) 
temp2 = pd.Series(temp1.sum(1)) 
weights = pd.Series(1 + 0.1 * np.random.rand(781)) 

result = RidgeCV(normalize=True).fit(temp1, temp2, 
            sample_weight=weights) 
# Exception: Data must be 1-dimensional 

Si vous utilisez un tableau numpy à la place, la l'erreur disparaît:

result = RidgeCV(normalize=True).fit(temp1, temp2, 
            sample_weight=weights.values) 

Cela semble être un bug; J'ai ouvert un scikit-learn issue pour le signaler.