写入typeerror时:--'float'和'str'不支持的操作数类型

时间:2019-04-15 08:27:38

标签: python

我是python的新手。当我想建立一个关于excel文件的线性回归模型时,代码存在一些问题,无法运行。我应该怎么解决?

def linear_regression_by_gd(X, Y, gamma=0.000001, eps=0.0001, max_iter=100): 
    pre_w = np.array(np.ones((X.shape[1], 1)))
    cur_w = np.array(np.zeros((X.shape[1], 1)))
    count = 1
    while (cur_w - pre_w).T.dot(cur_w - pre_w) > eps and count < max_iter:
        pre_w = cur_w
        cur_w = cur_w - np.array(gamma / np.sqrt(count) * X.T.dot(X.dot(cur_w) - Y))
        count += 1
    return cur_w

w = linear_regression_by_gd(x_train, y_train)

这是完整的错误信息:

   Traceback (most recent call last):
      File "D:\JetBrains\untitled\a\venv\lib\site-packages\pandas\core\ops.py", line 2001, in na_op
        result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs)
      File "D:\JetBrains\untitled\a\venv\lib\site-packages\pandas\core\computation\expressions.py", line 208, in evaluate
        return _evaluate(op, op_str, a, b, **eval_kwargs)
      File "D:\JetBrains\untitled\a\venv\lib\site-packages\pandas\core\computation\expressions.py", line 68, in _evaluate_standard
        return op(a, b)
    TypeError: unsupported operand type(s) for -: 'float' and 'str'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "D:/JetBrains/untitled/a/b.py", line 60, in <module>
        w = linear_regression_by_gd(x_train, y_train)
      File "D:/JetBrains/untitled/a/b.py", line 56, in linear_regression_by_gd
        cur_w = cur_w -np.array(gamma / np.sqrt(count) * X.T.dot(( X.dot(cur_w)) - (Y)))
      File "D:\JetBrains\untitled\a\venv\lib\site-packages\pandas\core\ops.py", line 2023, in f
        return self._combine_frame(other, pass_op, fill_value, level)
      File "D:\JetBrains\untitled\a\venv\lib\site-packages\pandas\core\frame.py", line 5090, in _combine_frame
        result = _arith_op(this.values, other.values)
      File "D:\JetBrains\untitled\a\venv\lib\site-packages\pandas\core\frame.py", line 5084, in _arith_op
        return func(left, right)
      File "D:\JetBrains\untitled\a\venv\lib\site-packages\pandas\core\ops.py", line 2003, in na_op
        result = masked_arith_op(x, y, op)
      File "D:\JetBrains\untitled\a\venv\lib\site-packages\pandas\core\ops.py", line 1009, in masked_arith_op
        com.values_from_object(yrav[mask]))
    TypeError: unsupported operand type(s) for -: 'float' and 'str'

0 个答案:

没有答案