【问题标题】:Regression Model in Python not meaningfulPython中的回归模型没有意义
【发布时间】:2021-05-17 13:15:28
【问题描述】:

我想在 python 中创建一个介于能量等级与能量等级之间的回归模型。房价和查看能源等级是否会影响房价

Dataset 看起来像:

这是使用线性回归实现的模型。

import statsmodels.formula.api as smf

# Initialise and fit linear regression model using `statsmodels`
model = smf.ols('price ~ energyrating', data=df)

model = model.fit()
model.params

#price=2.004943e+06 + (-.913381e+05)*energyrating

Intercept       2.004943e+06
energyrating   -3.913381e+05
dtype: float64
# Predict values
pred = model.predict()

# Plot regression against actual data
plt.figure(figsize=(12, 6))
plt.plot(df['energyrating'], df['price'], 'o')           # scatter plot showing actual data
plt.plot(df['energyrating'], pred, 'r', linewidth=2)   # regression line
plt.xlabel('Energy ratings')
plt.ylabel('Price')
plt.title('Energy ratings Vs. Price')

plt.show()

这个模型没有提供任何有意义的知识,对于正确的图表,我对 energyrating 进行了平方,因为与 price 相比,这些值很小,但仍然不正确。我错过了任何重要的点吗?

如何创建一个模型,在 energy ratingprice 之间提供有意义的关系?

欢迎其他型号的建议提前致谢

【问题讨论】:

  • 我通过分析您提供的数据集得出的结论是,priceenergyrating 之间几乎总是没有联系(这基本上是对接近于零的斜率的解释)。请注意,无论evergy评级如何,您都有很多价格相对较低的积分。你得到的回归线是正确的结果。
  • 非常感谢您的回复。只有一个问题,有什么其他模型或算法可以帮助我找到关系。既然你说“没有联系”,我想确认是否有任何模型或图表可以帮助我找到关系。?
  • 如果没有线性关系,可能存在其他类型的关系。这在这里看起来有点高斯!但这更像是一个数学/统计问题,而不是编程。可以在这里问:stats.stackexchange.com
  • 你想要一个找到关系的模型或算法,但我说没有关系,至少我没有看到任何关系。选择其他方法应该会产生相同的结果。您可能想尝试多项式回归或神经网络,但结果不会像您在问题中所说的那样“有意义”。你问我怎样才能创建一个提供有意义关系的模型,但实际上没有任何关系时你无法得到。您可能想了解pandas-profiling 模块,该模块报告相关性并提供有关数据帧的其他有趣信息。
  • 回答高斯问题。是的,但前提是您拥有最高价格点的百分比。再说一遍 - 请注意,您有很多价格相对较低的房子。

标签: python machine-learning regression


【解决方案1】:

你错过了这段代码,所以你应该把它放在“model = smf.ols('price ~ energyrating', data=df)”和“model = model.fit()”之间。 也许……

#Divide test data and training data into 7 and 3
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

model = model.fit(x_train, y_train)

【讨论】:

  • 你好。谢谢你。我收到此错误ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
  • 对于此代码,import statsmodels.formula.api as smf from sklearn.model_selection import train_test_split # Initialise and fit linear regression model using `statsmodels` model = smf.ols('price ~ energyrating', data=df) x=df['price'] y-df['energyrating'] x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0) model = model.fit(x_train, y_train)
猜你喜欢
  • 1970-01-01
  • 2019-09-18
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 2010-10-27
  • 1970-01-01
  • 2018-12-25
  • 2017-04-06
相关资源
最近更新 更多