gpt4 book ai didi

python - 如何绘制回归线?

转载 作者:行者123 更新时间:2023-12-01 06:51:06 27 4
gpt4 key购买 nike

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com