首页 > 解决方案 > 我想制作收入与教育的散点图,但显示错误

问题描述

我正在尝试为我的统计模型类创建收入与教育的散点图,但它显示“标识符中的字符无效”但是当我检查 txt 文件时,字符“收入”和“教育”都存在。请问你能帮帮我吗?

mod = smf.ols(formula=’education~earnings, data=mydata)
res = mod.fit()
res.summary()
beta=res.params 
matplotlib.pyplot.scatter(mydata["education"],mydata["earnings"],color="black") 
matplotlib.pyplot.plot(mydata["education"], res.fittedvalues, "r") 
matplotlib.pyplot.ylabel("earnings")
matplotlib.pyplot.xlabel("education") 
matplotlib.pyplot.title("Scatterplot earnings versus education") 
matplotlib.pyplot.show()

标签: pythonmatplotlib

解决方案


我认为问题在于这一行后面的引号=

mod = smf.ols(formula=’education~earnings, data=mydata)

这让 Python 很困惑,因为它不是一个有效的变量名。公式应作为字符串传递,并带有单/双​​引号。

mod = smf.ols(formula='education~earnings', data=mydata)

也许在复制粘贴时有些东西混淆了?


推荐阅读