- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
使用以下小型数据集:
bill = [34,108,64,88,99,51]
tip = [5,17,11,8,14,5]
我计算了一条最佳拟合回归线(手动)。
yi = 0.1462*x - 0.8188 #yi = slope(x) + intercept
我使用 Matplotlib 绘制了我的原始数据,如下所示:
plt.scatter(bill,tip, color="black")
plt.xlim(20,120) #set ranges
plt.ylim(4,18)
#plot centroid point (mean of each variable (74,10))
line1 = plt.plot([74, 74],[0,10], ':', c="red")
line2 = plt.plot([0,74],[10,10],':', c="red")
plt.scatter(74,10, c="red")
#annotate the centroid point
plt.annotate('centroid (74,10)', xy=(74.1,10), xytext=(81,9),
arrowprops=dict(facecolor="black", shrink=0.01),
)
#label axes
plt.xlabel("Bill amount ($)")
plt.ylabel("Tip amount ($)")
#display plot
plt.show()
我不确定如何将回归线放到图本身上。我知道有很多内置的东西可以快速拟合和显示最佳拟合线,但我这样做是为了练习。我知道我可以在点“0,0.8188”(截距)开始画线,但我不知道如何使用斜率值来完成线(设置线终点)。
鉴于 x 轴每增加一次,斜率应增加“0.1462”;对于线坐标,我尝试将 (0,0.8188) 作为起点,将 (100,14.62) 作为终点。但是这条线不通过我的质心点。它只是错过了它。
干杯,乔恩
最佳答案
问题中的推理部分正确。有了函数 f(x) = a*x +b
,您可以将 y 轴 (x=0) 的截距作为第一个点作为 (0, b)
(或本例中的 (0,-0.8188)
)。
该线上的任何其他点由 (x, f(x))
或 (x, a*x+b)
给出。所以查看 x=100 处的点会得到 (100, f(100))
,插入:(100, 0.1462*100-0.8188)
= (100,13.8012)
。在您在问题中描述的情况下,您只是忘记了考虑 b
。
下面显示了如何使用该函数在 matplotlib 中绘制线条:
import matplotlib.pyplot as plt
import numpy as np
bill = [34,108,64,88,99,51]
tip = [5,17,11,8,14,5]
plt.scatter(bill, tip)
#fit function
f = lambda x: 0.1462*x - 0.8188
# x values of line to plot
x = np.array([0,100])
# plot fit
plt.plot(x,f(x),lw=2.5, c="k",label="fit line between 0 and 100")
#better take min and max of x values
x = np.array([min(bill),max(bill)])
plt.plot(x,f(x), c="orange", label="fit line between min and max")
plt.legend()
plt.show()
当然拟合也可以自动完成。您可以通过调用 numpy.polyfit
获取斜率和截距:
#fit function
a, b = np.polyfit(np.array(bill), np.array(tip), deg=1)
f = lambda x: a*x + b
图中的其余部分将保持不变。
关于 python /Matplotlib : adding regression line to a plot given its intercept and slope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43152529/
我有一个注入(inject)了 Ninject 3.0 的服务类。我已经设置了它,因此它的代理是类代理而不是接口(interface)代理。该服务有 2 个方法,第一个方法返回广泛结果,第二个方法调用
当工具提示即将打开时,我想在状态栏中显示 wpf 应用程序中任何控件的工具提示文本。 当然,我可以尝试递归遍历主窗口的所有子控件,然后 设置他们的 ToolTipOpening事件总是相同的方法。但是
给定一个打开到 Web 服务器的套接字连接的 Flash 应用程序,是否可以读取与 Java 应用程序交换的数据包,而无需重定向所有 Flash 流量(即无需编写套接字代理)? 最佳答案 您想要做的事
我有一个文本框,我想对其进行一些验证。目前我有这段代码: function updateChanger() { // Validate input var likeMessage =
Linux 中的一些命令,例如。 ssh-agent $BASH 确保当前控制台中运行的命令的某些操作被另一个工具“拦截”。这一般是如何运作的?为了理解这一点,我应该研究哪些主题?假设我想制作一个工具
拦截过滤器模式(Intercepting Filter Pattern)用于对应用程序的请求或响应做一些预处理/后处理 定义过滤器,并在把请求传给实际目标应用程序之前应用在请求上 过滤器可以做认证
我一直在尝试实现应用程序范围的拦截器,但是,我一直看到以下错误: TypeError: this.interceptor.intercept is not a function 经过多次调试,我意识到
众所周知,我们可以像这样配置拦截器: 我的问题,如何配置
我正在开发一款游戏,里面有寻的导弹。目前,它们只是转向目标,这会产生看起来相当愚蠢的结果,所有导弹都跟随目标周围。 我想创造一种更致命的导弹,它将瞄准目标“将”到达的地方,但我对如何做到这一点感到有点
从 iTunes 中捕获事件有什么好的解决方案吗? 我想构建一个位于 iTunes 内部的插件,并响应特定歌曲的评级变化。对于 Windows,他们有一个我还没有深入研究的 SDK,但我也想为 Mac
现在我有一个名为 chartModelService 的 Angular 服务,上面有这个方法: this.fetchChartModel = function() { return $htt
我的 Controller 有以下方法: @RequestMapping(method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED
如果您安装了 Facebook 应用程序,并使用应用程序 openURL 方法打开 Facebook 页面,您会注意到 Facebook 应用程序接管并处理此 URL,而不是 Safari 执行此操作
我有这样一个类: class A { public static void makeCall() { URL url = "www.google.com"; I
我希望我的应用拦截手机发现的所有标签。这样我想检查标签的 uid 并在之后传递 Intent (显示应用程序选择器等)。 这就是我目前在 list 中的内容,以及用于 TAG_DISCOVERE
我正在用 Playwright's API testing 测试服务器. 我有一个简单的 GET 路由,它总是返回 405 not allowed。它还记录调用了 API 路由。为了这个 StackO
有什么方法可以拦截 Android WebView 中的重定向请求? shouldInterceptRequest(WebView,WebResourceRequest) 似乎没有被调用! 我想通过以
This question already has answers here: Element MyElement is not clickable at point (x, y)… Other el
我有一个拦截器,捕获404错误 拦截(要求:HttpRequest,下一个:HttpHandler):可观察> return next.handle(req).pipe(retry(1),
有没有办法让 Cypress 捕获任何未使用 cy.intercept 处理和 stub 的请求。我希望 Cypress 返回一个有用的错误,以突出显示发出未 stub 请求的实例。目前它只是让这些请
我是一名优秀的程序员,十分优秀!