在数组 (n,1) 和矩阵 (n,2) 之间运行线性回归 PYTHON

问题描述 投票:0回答:0

我正在尝试在有效联邦基金利率列和两列失业率、通货膨胀率之间进行线性回归,但我一直收到此错误

TypeError: first argument must be an iterable of pandas objects, you passed an object of type "Series"

这是我使用的代码:

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Load the two CSV files into dataframes
df1 = pd.read_csv('index.csv', sep=',')
X=df1["Effective Federal Funds Rate"]
inflation_rate=df1["Inflation Rate"]
Unemployment_Rate=df1["Unemployment Rate"]
y=pd.concat(Unemployment_Rate,inflation_rate)
X.dropna(axis=0)
X.dropna(axis=0)
# Create a linear regression model and fit it to the data
model = LinearRegression()
model.fit(X, y)

# Print the coefficients of the linear regression model
print('Coefficient: ', model.coef_)
print('Intercept: ', model.intercept_)
r_squared = model.score(X, y)
print('Coefficient of determination (R^2): ', r_squared)
# Plot the data points and the linear regression line
plt.scatter(X, y)
plt.plot(X, model.predict(X), color='red')
plt.xlabel('Interest rates')
plt.ylabel('Natural Gas Opening Price ($)')
plt.show()

我是在做逻辑错误还是语法错误? 最终结果应该是一个图表来分析两个输入元素之间的关系。

python pandas dataframe matplotlib linear-regression
© www.soinside.com 2019 - 2024. All rights reserved.