值错误“使用序列设置数组元素”,Group Lasso参数

2024-09-29 17:09:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在UCI自行车共享数据集上运行一些正则化技术。我想实现一个现代ish组套索正则化技术来评估我的模型。然而,我遇到了一个问题。这里有一段代码片段供大家理解。 我的模型分为培训和测试两部分,并且具有相同数量的元素,因为我还没有准备简历

standard_data1.columns
Index(['January', 'February', 'March', 'April', 'May', 'June', 'July',
       'August', 'September', 'October', 'November', 'December', 'Monday',
       'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday',
       'clear', 'light_rain/snow', 'heavy_rain/snow', 'atemp', 'hum',
       'windspeed', 'cnt'],
      dtype='object')
X_train1,X_test1,y_train1,y_test1=train_test_split(standard_data1.drop(['cnt'],axis=1),standard_data1['cnt'],test_size=0.25,random_state=42)
lambda1 = [0.001, 0.01, 0.1, 1, 10]
group_index = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 5, 6])
group_lasso_model = asgl.ASGL(model='lm', penalization='gl',lambda1=lambda1)
group_lasso_model.fit(x=X_train1, y=y_train1, group_index=group_index)

这里是我得到的错误,有没有人有一个简单的解决方案,这个错误似乎很容易被破解,但我不确定

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-128-fd9c54078bdf> in <module>
      2 group_index = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 5, 6])
      3 group_lasso_model = asgl.ASGL(model='lm', penalization='gl',lambda1=lambda1)
----> 4 group_lasso_model.fit(x=X_train1, y=y_train1, group_index=group_index)

~/anaconda3/lib/python3.7/site-packages/asgl/asgl.py in fit(self, x, y, group_index)
    754                 else:
    755                     self.coef_ = getattr(self, self._get_solver_names())(x=x, y=y, param=param,
--> 756                                                                          group_index=group_index)
    757             else:
    758                 self.coef_ = self._parallel_execution(x=x, y=y, param=param, group_index=group_index)

~/anaconda3/lib/python3.7/site-packages/asgl/asgl.py in gl(self, x, y, group_index, param)
    372             group_lasso_penalization += cvxpy.sqrt(group_sizes[i]) * cvxpy.norm(beta_var[i], 2)
    373         if self.model == 'lm':
--> 374             objective_function = (1.0 / n) * cvxpy.sum_squares(y - model_prediction)
    375         else:
    376             objective_function = (1.0 / n) * cvxpy.sum(self._quantile_function(x=(y - model_prediction)))

~/anaconda3/lib/python3.7/site-packages/cvxpy/atoms/sum_squares.py in sum_squares(expr)
     31         An expression representing the sum of squares.
     32     """
---> 33     return quad_over_lin(expr, 1)

~/anaconda3/lib/python3.7/site-packages/cvxpy/atoms/quad_over_lin.py in __init__(self, x, y)
     28 
     29     def __init__(self, x, y):
---> 30         super(quad_over_lin, self).__init__(x, y)
     31 
     32     @Atom.numpy_numeric

~/anaconda3/lib/python3.7/site-packages/cvxpy/atoms/atom.py in __init__(self, *args)
     42             )
     43         # Convert raw values to Constants.
---> 44         self.args = [Atom.cast_to_const(arg) for arg in args]
     45         self.validate_arguments()
     46         self._shape = self.shape_from_args()

~/anaconda3/lib/python3.7/site-packages/cvxpy/atoms/atom.py in <listcomp>(.0)
     42             )
     43         # Convert raw values to Constants.
---> 44         self.args = [Atom.cast_to_const(arg) for arg in args]
     45         self.validate_arguments()
     46         self._shape = self.shape_from_args()

~/anaconda3/lib/python3.7/site-packages/cvxpy/expressions/expression.py in cast_to_const(expr)
    465                         "Combine Expressions using atoms such as bmat, hstack, and vstack."
    466                     )
--> 467         return expr if isinstance(expr, Expression) else cvxtypes.constant()(expr)
    468 
    469     @staticmethod

~/anaconda3/lib/python3.7/site-packages/cvxpy/expressions/constants/constant.py in __init__(self, value)
     43             self._sparse = True
     44         else:
---> 45             self._value = intf.DEFAULT_INTF.const_to_matrix(value)
     46             self._sparse = False
     47         self._imag = None

~/anaconda3/lib/python3.7/site-packages/cvxpy/interface/numpy_interface/ndarray_interface.py in const_to_matrix(self, value, convert_scalars)
     48             return result
     49         else:
---> 50             return result.astype(numpy.float64)
     51 
     52     # Return an identity matrix.

ValueError: setting an array element with a sequence.

以下是我一直关注的github: https://github.com/alvaromc317/asgl/blob/master/user_guide.ipynb


Tags: toinpyselfindexmodellibpackages

热门问题