如何使用 matplotlib 将线性回归绘制为一条线段 [英] How to plot a linear regression to a segment of a line with matplotlib

查看:100
本文介绍了如何使用 matplotlib 将线性回归绘制为一条线段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用matplotlib绘制了一些数据,这些数据长250点.我可以拟合整个数据集.但是,我希望使用最小二乘拟合并在最后50个数据点上绘制一条回归线.最好的方法是什么?(下面是我的情节代码.)

I have some data plotted as lines with matplotlib that are 250 points long. I can fit the whole data sets. However, I wish to fit and plot a regression line to just the last 50 data points using least squares. What is the best approach? (My plot code below.)

j = 0
for line, rank in sortedSymbols:
    series = getattr(self, line)["CLOSE"]
    dates = pd.to_datetime(getattr(self, line)["DATE"]).dt.date
    ax.plot(dates.iloc[-250:], series.iloc[-250:]/series.iloc[-250] * (40+j), label = line)
    j += 10

推荐答案

有很多不同的方法可以做到这一点.但是,如果没有有关您的数据结构以及您究竟要寻找什么的更多信息,这是可以完成的一种方法.np.polyfit()(文档 此处) 对列表或数组中的顺序数据使用 OLS 回归.

There are a bunch of different ways to do this. But without more information on your data structure and what exactly your looking for, this is one way it could be done. np.polyfit() (documentation here) uses OLS regression on your sequential data in lists or arrays.

import numpy as np

j = 0
for line, rank in sortedSymbols:
    series = getattr(self, line)["CLOSE"]
    dates = pd.to_datetime(getattr(self, line)["DATE"]).dt.date

    #Calculate the slope and intercept of fitted curve based on last 50 datapoint using the values 
    #you plotted before with 1 specified for a linear best fit line
    slope, intercept = np.polyfit(dates.iloc[-50:].index,series.iloc[-50:]/series.iloc[-250] * (40+j),1)

    #plot the trend line
    ax.plot(dates.iloc[-50:],slope*dates.iloc[-50:].index+intercept,label=line)


    j += 10

这篇关于如何使用 matplotlib 将线性回归绘制为一条线段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆