python - 如何绘制回归线?

标签 python matplotlib linear-regression non-linear-regression

import numpy as np 
import matplotlib.pyplot as plt 
import pandas as pd
import math
import csv
import seaborn as sns
import numpy.polynomial.polynomial as poly

headers = ['time', 'freq','sig_str']
df = pd.read_csv(r"Lavendershark.csv", delimiter = ',', names = headers)
sns.set()
df.pivot_table('sig_str', index='time', columns='freq').plot()
plt.ylabel("Signal Strength(MHz)")
plt.xlabel("Time(ms)")
# plt.show()

freqs = df.freq.unique()
print(freqs)

for fq in freqs:
    dffq = df[df['freq']==fq]
    print(dffq)
    X = dffq['time'].values
    Y = dffq['sig_str'].values # mean of our inputs and outputs
    x_mean = np.mean(X)
    y_mean = np.mean(Y) #total number of values
    n = len(X)  # using the formula to calculate the b1 and b0
    numerator = 0
    denominator = 0
    for i in range(n):
        numerator += (X[i] - x_mean) * (Y[i] - y_mean)
        denominator += (X[i] - x_mean) ** 2

    b1 = numerator / denominator
    b0 = y_mean - (b1 * x_mean) #printing the coefficient
    print(b1, b0)

    #plotting values 
    x_max = np.max(X) 
    x_min = np.min(X) #calculating line values of x and y
    x = np.linspace(x_min, x_max, 1000)
    y = b0 + b1 * x #plotting line 
    plt.plot(x, y, color='#00ff00') #plot the data point
    plt.legend()

    coefs = np.polyfit(X, Y, 3)
    x_new = np.linspace(X[0], X[-1], num=len(X)*10)
    ffit = np.poly1d(coefs)
    plt.plot(x_new, ffit(x_new),color='#f2411b')
    plt.legend()


plt.show()

我想绘制这个图,其中包含数据集的数据点以及线性和多项式回归线。

enter image description here

但我不知道如何从下面的图中选择/删除线条,以便获得所需的结果

enter image description here

最佳答案

使用plt.scatter() ,即

plt.scatter(x, y, color='#00ff00')

而不是

plt.plot(x, y, color='#00ff00')

用于数据(不适用于拟合)。拟合散点图示例:

import numpy as np
from numpy.polynomial.polynomial import polyfit
import matplotlib.pyplot as plt

n=50
x = np.linspace(0, 10, n)
y = 5 * x + 10 + (np.random.random(n) - 0.5) * 5
b, m = polyfit(x, y, 1)

plt.scatter(x, y,  marker='.')
plt.plot(x, b + m*x, linestyle='-')
plt.show()

Scatter with regression

关于python - 如何绘制回归线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59000919/

相关文章:

python - Keras 模型未能减少损失

python - pip install matplotlib 在使用 Homebrew 软件的特立独行者上失败

java - 对于日期值数组,趋势值会通过 LinearRegression.java 返回地雷

python - 需要输入: Linear Regression prediction of difficulty of routes quite bad

python - 如何检查连续变量和分类变量之间的相关性?

python - 结合 Pandas 中的两个时间序列

Python:重新格式化一组文本文件的简洁/优雅的方式?

python - 在一个图中叠加三个直方图

Python 绘图字典

python - Z3Py:获取每个运算符对应的函数,例如 '*' , '==' , '-'