如何绘制回归线?

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()

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

如何绘制回归线?

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

如何绘制回归线?

jojosjjsjj 回答:如何绘制回归线?

使用plt.scatter(),即

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

代替

plt.plot(x,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,1)

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

Scatter with regression

本文链接:https://www.f2er.com/3048457.html

大家都在问