TypeError float when fitting linear curve using Statsmodel but already converted the type

huangapple go评论76阅读模式
英文:

TypeError float when fitting linear curve using Statsmodel but already converted the type

问题

I've translated the code-related portion of your text as requested:

所以我一直在尝试编写一个通用函数帮助我根据历史数据生成回归结果我不想使用`statsmodels``.predict`函数因为它不会让我有机会生成任何最佳拟合线当给定任何因子和任何依赖变量来拟合时
但问题是当我尝试使用OLS时它一直给我报错

TypeError: 'float' object is not iterable


但这令人困惑,因为我尝试将我的依赖变量的类型转换为`list`和`nd.array`,但它总是变成`float`类型。我不知道如何解决这个问题。

为了澄清,我将在这里放置我的代码:

```python
#############回归###################
import csv
from scipy import stats
import statsmodels.api as sm
from scipy.optimize import curve_fit
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.stattools import adfuller
from sklearn.model_selection import train_test_split
from pmdarima.arima.utils import ndiffs
# 导入库
import pmdarima as pm
from pmdarima import auto_arima
# 在训练集上拟合SARIMAX(0, 1, 1)x(2, 1, 1, 12)
from statsmodels.tsa.statespace.sarimax import SARIMAX
from statsmodels.tsa.arima_model import ARIMA
from statsmodels.tsa.seasonal import seasonal_decompose

#首先,使用GDP作为三个股票因素
GDP = list(df1['GDP'])
GDP = GDP[:len(stockdf)]
GDP.reverse()
print(GDP)

def param(factor, stock):
    print(type(sm.add_constant(factor)))
    model = sm.OLS(list(stock), sm.add_constant(factor))
    
    # model = sm.OLS(factor, sm.add_constant(stock))
    p = model.fit().params.tolist()
    return p[1]*x + p[0]

#def findRegression(factor,stock):
    #return list(map(param,factor,stock))
#model = findRegression(GDP, stockdf['上证指数']
print(type(stockdf['SSE']))
stock = list(stockdf['SSE'])
print(type(stock))
model = list(map(param, GDP, stock))
#print(len(stockdf['上证指数']))
#print(stockdf['上证指数'])

此外,末尾的print(type())会给出以下结果:

<class 'pandas.core.series.Series'>
<class 'list'>
<class 'numpy.ndarray'>

但当我尝试在param(factor, stock)函数中添加type(stock)时,它告诉我它是class 'float'

任何帮助将不胜感激!


Please note that code formatting and syntax may require additional adjustments depending on your specific Python environment and the libraries being used.

<details>
<summary>英文:</summary>

So I&#39;ve been trying to write a general function to help me generate the regression results given historical data, and I didn&#39;t wanted to use the `.predict` function of statsmodels because it wouldn&#39;t give me the leeway of generating any best-fit linera line when given any factor and any dependent-variable to fit.
But the thing is, when I try to use OLS, it keeps giving me this error message:

TypeError: 'float' object is not iterable

But this is confusing because I tried to convert the type of my dependent variable to `list`list, and to `nd.array` but somehow it keeps changing into `float` type. And I don&#39;t know how to solve this.

For clarification, I am going to put my code here:

#############REGRESSION###################
import csv
from scipy import stats
import statsmodels.api as sm
from scipy.optimize import curve_fit
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.stattools import adfuller
from sklearn.model_selection import train_test_split
from pmdarima.arima.utils import ndiffs

Import the library

import pmdarima as pm
from pmdarima import auto_arima

Fit a SARIMAX(0, 1, 1)x(2, 1, 1, 12) on the training set

from statsmodels.tsa.statespace.sarimax import SARIMAX
from statsmodels.tsa.arima_model import ARIMA
from statsmodels.tsa.seasonal import seasonal_decompose

#First, three stock factors with GDP
GDP=list(df1['GDP'])
GDP=GDP[:len(stockdf)]
GDP.reverse()
print(GDP)

def param(factor,stock):
print(type(sm.add_constant(factor)))
model = sm.OLS(list(stock), sm.add_constant(factor))

#model=sm.OLS(factor,sm.add_constant(stock))
p=model.fit().params.tolist()
return p[1]*x+p[0]

#def findRegression(factor,stock):
#return list(map(param,factor,stock))
#model=findRegression(GDP,stockdf['上证指数']
print(type(stockdf['SSE']))
stock=list(stockdf['SSE'])
print(type(stock))
model=list(map(param,GDP,stock))
#print(len(stockdf['上证指数']))
#print(stockdf['上证指数'])

also, the `print(type())` at the end gives me: 

<class 'pandas.core.series.Series'>
<class 'list'>
<class 'numpy.ndarray'>

but when I try to add `type(stock)` in my `param(factor, stock)` function, it tells me it is `class &#39;float&#39;`.

Any help would be very much appreciated!

</details>


# 答案1
**得分**: 0

model=list(map(param,GDP,stock))

当你在一个函数上使用map并传入一个列表时,(我鼓励你阅读map的文档),map会为你的列表的每个元素“映射”它。

因此,当你执行map(param,GDP,stock)时,你并没有将stock列表作为参数传递给你的函数“param”,而是传递了stock[0],stock[1]... stock[len(stock)-1]。

示例:

```python
def testing(val1, val2):
    print(val1)
    print(val2)

a = ["1","2"]
b = ["3","4"]

model=list(map(testing,a,b))

打印结果:

1 3
2 4

因为函数testing会针对每个数组的每个元素被调用,因此出现了浮点数类型的问题,当你执行list(stock)时,实际上相当于执行list(2.5)(这是一个示例),这是不可能的。

希望这清楚了。

英文:
model=list(map(param,GDP,stock))

When you use map on a function with a list, (I encourage you to read the map documentation), map is gonna "map" it for every element of your list

Ence, when you are doing map(param,GDP,stock), you are not passing the stock list as a parameter to your function "param" but instead you are passing stock[0], stock[1]... stock[len(stock)-1]

Example :

def testing(val1, val2):
    print(val1)
    print(val2)



a = [&quot;1&quot;,&quot;2&quot;]
b = [&quot;3&quot;,&quot;4&quot;]

model=list(map(testing,a,b))

prints

1 3
2 4 

Because the function testing gets called with each element from each array, ence your float type problem, when you do list(stock), you are doing list(2.5) (that's an example) which is not possible

hope this is clear.

huangapple
  • 本文由 发表于 2023年6月5日 16:06:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定