Bạn có thể sử dụng cú pháp cơ bản sau đây để vẽ một dòng phù hợp nhất với Python:
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y]
#add line of best fit to plot
plt.plot[x, a*x+b]
Ví dụ sau đây cho thấy cách sử dụng cú pháp này trong thực tế.
Ví dụ 1: Lô đường cơ bản phù hợp nhất với Python
Mã sau đây cho thấy cách vẽ một dòng cơ bản phù hợp nhất với Python:
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y]
#add line of best fit to plot
plt.plot[x, a*x+b]
Ví dụ 2: Lô đường tùy chỉnh phù hợp nhất với Python
Mã sau đây cho thấy cách tạo cùng một dòng phù hợp nhất như ví dụ trước ngoại trừ với các bổ sung sau:
- Màu sắc tùy chỉnh cho các điểm và dòng phù hợp nhất
- Kiểu và chiều rộng tùy chỉnh cho dòng phù hợp nhất
- Phương trình của đường hồi quy được trang bị được hiển thị trên lô
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y, color='purple']
#add line of best fit to plot
plt.plot[x, a*x+b, color='steelblue', linestyle='--', linewidth=2]
#add fitted regression equation to plot
plt.text[1, 17, 'y = ' + '{:.2f}'.format[b] + ' + {:.2f}'.format[a] + 'x', size=14]
Hãy thoải mái đặt phương trình hồi quy được trang bị trong bất cứ điều gì [x, y] bạn muốn trên cốt truyện.[x, y] coordinates you would like on the plot.
Đối với ví dụ cụ thể này, chúng tôi đã chọn [x, y] = [1, 17].
Tài nguyên bổ sung
Các hướng dẫn sau đây giải thích cách phù hợp với các mô hình hồi quy khác nhau trong Python:
Hướng dẫn đầy đủ về hồi quy tuyến tính trong Python Cách thực hiện hồi quy đa thức trong Python Cách thực hiện hồi quy lượng tử trong Python
How to Perform Polynomial Regression in Python
How to Perform Quantile Regression in Python
Hồi quy - Cách lập trình dòng phù hợp nhất
Chào mừng bạn đến với phần thứ 9 của hướng dẫn hồi quy học máy của chúng tôi trong học tập máy của chúng tôi với loạt Hướng dẫn Python. Chúng tôi đã làm việc để tính toán hồi quy, hoặc phù hợp nhất, tốt nhất cho một bộ dữ liệu nhất định trong Python. Trước đây, chúng tôi đã viết một chức năng sẽ thu thập độ dốc và bây giờ chúng tôi cần tính toán hệ thống y. Mã của chúng tôi cho đến thời điểm này:machine learning regression tutorial within our Machine Learning with Python tutorial series. We've been working on calculating the regression, or best-fit, line for a given dataset in Python. Previously, we wrote a function that will gather the slope, and now we need to calculate the y-intercept. Our code up to this point:
from statistics import mean import numpy as np xs = np.array[[1,2,3,4,5], dtype=np.float64] ys = np.array[[5,4,6,5,6], dtype=np.float64] def best_fit_slope[xs,ys]: m = [[[mean[xs]*mean[ys]] - mean[xs*ys]] / [[mean[xs]*mean[xs]] - mean[xs*xs]]] return m m = best_fit_slope[xs,ys] print[m]
Một lời nhắc nhở, tính toán cho hệ thống Y-Intercept của dòng phù hợp nhất là:
Điều này sẽ dễ dàng hơn một chút so với độ dốc. Chúng ta có thể lưu một vài dòng bằng cách kết hợp điều này vào chức năng khác của chúng ta. Chúng tôi sẽ đổi tên nó thành
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y]
#add line of best fit to plot
plt.plot[x, a*x+b]
3.Tiếp theo, chúng ta có thể điền vào:
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y]
#add line of best fit to plot
plt.plot[x, a*x+b]
4 và trả lại M và B:def best_fit_slope_and_intercept[xs,ys]: m = [[[mean[xs]*mean[ys]] - mean[xs*ys]] / [[mean[xs]*mean[xs]] - mean[xs*xs]]] b = mean[ys] - m*mean[xs] return m, b
Bây giờ chúng ta có thể kêu gọi nó với:
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y]
#add line of best fit to plot
plt.plot[x, a*x+b]
5Mã đầy đủ của chúng tôi cho đến thời điểm này:
from statistics import mean import numpy as np xs = np.array[[1,2,3,4,5], dtype=np.float64] ys = np.array[[5,4,6,5,6], dtype=np.float64] def best_fit_slope_and_intercept[xs,ys]: m = [[[mean[xs]*mean[ys]] - mean[xs*ys]] / [[mean[xs]*mean[xs]] - mean[xs*xs]]] b = mean[ys] - m*mean[xs] return m, b m, b = best_fit_slope_and_intercept[xs,ys] print[m,b]
Đầu ra phải là:
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y]
#add line of best fit to plot
plt.plot[x, a*x+b]
6Bây giờ chúng ta chỉ cần tạo một dòng cho dữ liệu:
Hãy nhớ lại rằng
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y]
#add line of best fit to plot
plt.plot[x, a*x+b]
7. Chúng ta có thể tạo ra một chức năng cho việc này ... hoặc chỉ loại bỏ nó trong một vòng 1 cho vòng lặp:regression_line = [[m*x]+b for x in xs]
Vòng lặp 1 cho vòng lặp trên giống như làm:
regression_line = [] for x in xs: regression_line.append[[m*x]+b]
Tuyệt vời, cuối cùng chúng ta hãy gặt hái thành quả lao động của chúng ta! Thêm các nhập khẩu sau:
import matplotlib.pyplot as plt from matplotlib import style style.use['ggplot']
Điều này sẽ cho phép chúng tôi tạo ra đồ thị, và làm cho chúng không quá xấu xí. Bây giờ ở cuối:
plt.scatter[xs,ys,color='#003F72'] plt.plot[xs, regression_line] plt.show[]
Đầu tiên, chúng tôi vẽ một biểu đồ phân tán của dữ liệu hiện có, sau đó chúng tôi vẽ đồ thị dòng hồi quy của chúng tôi, sau đó cuối cùng hiển thị nó. Nếu bạn không quen thuộc, bạn có thể kiểm tra trực quan hóa dữ liệu với loạt hướng dẫn Python và Matplotlib., you can check out the Data Visualization with Python and Matplotlib tutorial series.
Output:
Xin chúc mừng vì đã làm cho nó đến nay! Vì vậy, làm thế nào bạn có thể thực sự đưa ra một dự đoán dựa trên mô hình này bạn vừa thực hiện? Đủ đơn giản, phải không? Bạn có mô hình của bạn, bạn chỉ cần điền vào
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y]
#add line of best fit to plot
plt.plot[x, a*x+b]
8. Ví dụ: chúng ta hãy dự đoán một vài điểm:import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y]
#add line of best fit to plot
plt.plot[x, a*x+b]
0Chúng tôi có dữ liệu đầu vào của chúng tôi, "tính năng" của chúng tôi để nói. Nhãn là gì?
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y]
#add line of best fit to plot
plt.plot[x, a*x+b]
1Đầu ra:
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y]
#add line of best fit to plot
plt.plot[x, a*x+b]
9Chúng tôi thậm chí có thể vẽ đồ thị nó:
import numpy as np
import matplotlib.pyplot as plt
#define data
x = np.array[[1, 2, 3, 4, 5, 6, 7, 8]]
y = np.array[[2, 5, 6, 7, 9, 12, 16, 19]]
#find line of best fit
a, b = np.polyfit[x, y, 1]
#add points to plot
plt.scatter[x, y]
#add line of best fit to plot
plt.plot[x, a*x+b]
2Output:
Bây giờ chúng ta biết làm thế nào để tạo ra các mô hình của riêng mình, điều này thật tuyệt, nhưng chúng ta vẫn đang thiếu một cái gì đó không thể thiếu: mô hình của chúng ta chính xác như thế nào? Đây là chủ đề để thảo luận trong hướng dẫn tiếp theo!
Hướng dẫn tiếp theo: