python - 如何绘制回归线?
问题描述
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()
我想绘制此图,其中包含数据点的数据点以及数据集的线性和多项式回归线。
但我不知道如何从下图中选择/删除线条,以便获得所需的结果
解决方案
使用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()
推荐阅读
- html - Bootstrap 在桌子上冻结头
- ios - 无法在 iOS 中的数组上声明附加命令
- html - 抓取文件中的持久非 utf-8 字符
- javascript - .on('scroll') 在使用 replaceWith() 方法后停止触发
- c++ - C++ - 时间执行 0 与 Chrono 库
- swift - 时间和日期格式化程序 SwiftUI JSON 数据
- angular - 将 [(ngModel)] 绑定到未定义的值
- azure - 如何在 Azure 函数中配置 MS.Extensions.Logging 日志级别?
- regex - 通过正则表达式在加载页面上的 DOM 中分离部分脚本标记
- java - 处理死信队列的配置