英文:
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've been trying to write a general function to help me generate the regression results given historical data, and I didn't wanted to use the `.predict` function of statsmodels because it wouldn'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'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 'float'`.
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 = ["1","2"]
b = ["3","4"]
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论