类型错误:float() 参数必须是字符串或数字,而不是“方法"-多变量回归 [英] TypeError: float() argument must be a string or a number, not 'method' - Multiple variable regression

查看:130
本文介绍了类型错误:float() 参数必须是字符串或数字,而不是“方法"-多变量回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误消息:TypeError: float() 参数必须是字符串或数字,而不是方法".下面是我的代码片段.

I've been getting the error: TypeError: float() argument must be a string or a number, not 'method'. Below is my snippet of code.

我已经检查过类似这样的其他帖子:TypeError: float() argument must be a string or a number, not 'function' – Python/Sklearn 但似乎无法找到错误的根本原因.python 是否说我的变量(y、x1、x2 等)是方法",这就是我收到错误的原因?

I've checked other posts like this one: TypeError: float() argument must be a string or a number, not 'function' – Python/Sklearn but can't seem to get to the root cause of the error. Is python saying that my variables (y, x1, x2 etc.) are 'methods' which is why I'm receiving the error?

如果是这样,有谁知道我该如何解决这个问题?在此先感谢任何可以帮助我的人!

If so, does anyone know how I can resolve this? Thanks in advance to anyone that can help me!

# Replace inf and NaNs

df_raw['daily pct return']= df_raw['daily pct return'].replace([np.inf, -np.inf],np.nan).dropna
df_raw = pd.DataFrame(data=df_raw)
df_raw.to_csv('Raw_final.csv', header=True)


y = (df_raw['daily pct return'].shift(periods=1)).astype(float)
x1 = (df_raw['Excess daily return']).astype(float)
x2 = (df_raw['Excess weekly return']).astype(float)
x3 = (df_raw['Excess monthly return']).astype(float)
x4 = (df_raw['Trading vol / mkt cap']).astype(float)
x5 = (df_raw['Std dev']).astype(float)
x6 = (df_raw['Residual risk']).astype(float)

result = smf.OLS(exog=y, endog=[x1, x2, x3, x4, x5, x6]).fit()
print(result.params)
print(result.summary())

推荐答案

错别字:

df_raw['daily pct return'] = df_raw['daily pct return'].replace(...).dropna()

在代码的第一条语句中,您将数据帧的 dropna 方法分配给 'daily pct return' 列.您可能希望它成为方法的输出.要实现这一点,只需在语句的末尾添加一对括号,这样解释器就可以理解您正在调用该方法以使用其输出,而不是将方法本身作为对象引用.

In the very first statement of your code, you are assigning to 'daily pct return' column the dropna method of the dataframe. You probably want it to be the output of the method instead. To achieve this, simply add a pair of parenthesis to the end of the statement, so the interpreter understands you are calling the method to use its output, and not referring to the method itself as to an object.

这篇关于类型错误:float() 参数必须是字符串或数字,而不是“方法"-多变量回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆