代码之家  ›  专栏  ›  技术社区  ›  akalanka

statmodels OLS在python中给出了一个TypeError

  •  1
  • akalanka  · 技术社区  · 6 年前

    我正在尝试将一组特性应用到statsmodel的OLS线性回归模型中。

    我一次添加几个功能。第一个功能很好。但是,当我不断添加新功能时,它给了我一个错误。

    Traceback (most recent call last):
      File "read_xml.py", line 337, in <module>
        model = sm.OLS(Y, X).fit()
    ...
      File "D:\pythonprojects\testproj\test_env\lib\site-packages\statsmodels\base\data.py", line 132, in _handle_constant
        if not np.isfinite(ptp_).all():
    TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
    

    所以我用

    X = X.astype(float)
    

    然后出现了另一个错误。

    Traceback (most recent call last):
      File "read_xml.py", line 339, in <module>
        print(model.summary())
    ...
    File "D:\pythonprojects\testproj\test_env\lib\site-packages\scipy\stats\_distn_infrastructure.py", line 1824, in sf
        place(output, (1-cond0)+np.isnan(x), self.badvalue)
    TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
    

    new_df0 = pd.concat([df_lex[0], summary_df[0]], axis = 0, join = 'inner')
    new_df1 = pd.concat([df_lex[1], summary_df[1]], axis = 0, join = 'inner')
    data = pd.concat([new_df0, new_df1], axis = 1)
    print(data.shape)
    X = data.values[0:6,:]
    Y = data.values[6,:]
    Y = Y.reshape(1,88)
    X = X.T
    Y = Y.T
    X = X.astype(float)
    model = sm.OLS(Y, X).fit()
    predictions = model.predict(X)
    print(model.summary())
    

    在中触发的第一个错误 model = sm.OLS(Y,X).fit() 第二个错误触发 model.summary()

    但是在其他一些功能上,没有错误。

    new_df0 = pd.concat([df_len[0], summary_df[0]], axis = 0, join = 'inner')
    new_df1 = pd.concat([df_len[1], summary_df[1]], axis = 0, join = 'inner')
    
    data = pd.concat([new_df0, new_df1], axis = 1)
    print(data.shape)
    X = data.values[0:2,:]
    Y = data.values[2,:]
    Y = Y.reshape(1,88)
    X = X.T
    Y = Y.T
    X = X.astype(float)
    print(X.shape)
    print(Y.shape)
    
    model = sm.OLS(Y, X).fit()
    predictions = model.predict(X)
    print(model.summary())
    

    2 回复  |  直到 6 年前
        1
  •  2
  •   akalanka    6 年前
    Y.astype(float)
    

        2
  •  1
  •   stealthyninja michkra    5 年前

    检查类型 X_opt y . 可能是float64,因为计算精度。所以,试试:

    X_opt = X_opt.astype(np.float64)
    y = y.astype(np.float64)
    

    我也犯了同样的错误,用这种方法修正了它。

        3
  •  -1
  •   sukhbinder    6 年前

    请使用

    model=sm.OLS(df.Y,df.X, missing='drop').fit()
    

    可以 是原因。

    推荐文章