Bài viết này giới thiệu về ước tính mật độ hạt nhân bằng thư viện máy học của Python
x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
1Ước tính mật độ hạt nhân [KDE] là một phương pháp phi tham số để ước tính hàm mật độ xác suất của một biến ngẫu nhiên nhất định. Nó còn được gọi bằng cái tên truyền thống, phương pháp Cửa sổ Parzen-Rosenblatt, theo tên những người phát hiện ra nó.
Đưa ra một mẫu độc lập, phân phối giống hệt nhau [i. i. d] quan sát \[[x_1,x_2,\ldots,x_n]\] của một biến ngẫu nhiên từ phân phối nguồn không xác định, ước tính mật độ hạt nhân, được đưa ra bởi
$$
p[x] = \frac{1}{nh} \Sigma_{j=1}^{n}K[\frac{x-x_j}{
$$
trong đó \[K[a]\] là hàm hạt nhân và \[h\] là tham số làm mịn, còn được gọi là băng thông. Các hạt nhân khác nhau sẽ được thảo luận sau trong bài viết này, nhưng để hiểu toán học, chúng ta hãy xem một ví dụ đơn giản
Tính toán ví dụ
Giả sử chúng ta có các điểm mẫu [-2,-1,0,1,2], với nhân tuyến tính được cho bởi. \[K[a]= 1-\frac{. a. }{h}\] và \[h=10\]
xj=[−2−1012]. 0−xj. =[21012]. 0−xjh. =[0. 20. 100. 10. 2]K[. 0−xjh. ]=[0. 80. 910. 90. số 8]Thay phần trên vào công thức cho \[p[x]\]
$$
p[0] = \frac{1}{[5][10]} [ 0. 8+0. 9+1+0. 9+0. 8 ] = 0. 088
$$
Ước tính mật độ hạt nhân bằng Python
Mặc dù có một số cách tính toán ước tính mật độ hạt nhân trong Python, nhưng chúng tôi sẽ sử dụng thư viện máy học phổ biến
x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
1 cho mục đích này. Nhập các thư viện sau vào mã của bạnimport numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KernelDensity
from sklearn.model_selection import GridSearchCV
Dữ liệu tổng hợp
Để chứng minh ước tính mật độ hạt nhân, dữ liệu tổng hợp được tạo từ hai loại phân phối khác nhau. Một là phân phối chuẩn logarit bất đối xứng và một là phân phối Gaussian. Hàm sau trả về 2000 điểm dữ liệu
Mã bên dưới lưu trữ các điểm trong
x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
3. Chúng ta có thể tạo biểu đồ phân tán của các điểm này dọc theo trục y hoặc chúng ta có thể tạo biểu đồ của các điểm nàyx_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
Sử dụng KernelDensity của Scikit-Learn
Để tìm dạng của hàm mật độ ước tính, chúng ta có thể tạo một tập hợp các điểm cách đều nhau và ước tính mật độ hạt nhân tại mỗi điểm. Các điểm kiểm tra được đưa ra bởi
x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
Bây giờ chúng ta sẽ tạo một đối tượng
x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
4 và sử dụng phương thức x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
5 để tìm điểm của từng mẫu như trong đoạn mã dưới đây. Phương thức x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
6 sử dụng hai tham số mặc định, i. e. x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
7 và x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
8x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
1Hình dạng của phân phối có thể được xem bằng cách vẽ điểm mật độ cho từng điểm, như được đưa ra dưới đây
x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
2Hiểu thông số băng thông
Ví dụ trước không phải là một ước tính ấn tượng lắm về hàm mật độ, chủ yếu do các tham số mặc định. Hãy thử nghiệm với các giá trị băng thông khác nhau để xem nó ảnh hưởng như thế nào đến ước tính mật độ
x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
3Hãy xem hướng dẫn thực hành, thực tế của chúng tôi để học Git, với các phương pháp hay nhất, tiêu chuẩn được ngành chấp nhận và bao gồm bảng gian lận. Dừng các lệnh Git trên Google và thực sự tìm hiểu nó
Chúng ta có thể thấy rõ rằng việc tăng băng thông dẫn đến ước tính mượt mà hơn. Các giá trị băng thông rất nhỏ dẫn đến các đường cong nhọn và rung, trong khi các giá trị rất cao dẫn đến một đường cong mượt mà rất tổng quát, bỏ sót các chi tiết quan trọng. Điều quan trọng là chọn một giá trị cân bằng cho tham số này
Điều chỉnh thông số băng thông
Thư viện
x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
1 cho phép điều chỉnh tham số x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
0 thông qua xác thực chéo và trả về giá trị tham số giúp tối đa hóa khả năng ghi nhật ký của dữ liệu. Hàm chúng ta có thể sử dụng để đạt được điều này là x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
1, hàm này yêu cầu các giá trị khác nhau của tham số x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
0x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
8Mô hình tốt nhất có thể được truy xuất bằng cách sử dụng trường
x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
3 của đối tượng x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
4Hãy xem ước tính mật độ hạt nhân tối ưu bằng cách sử dụng hạt nhân Gaussian và cũng in giá trị của băng thông
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KernelDensity
from sklearn.model_selection import GridSearchCV
1import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KernelDensity
from sklearn.model_selection import GridSearchCV
2Bây giờ, ước tính mật độ này dường như mô hình hóa dữ liệu rất tốt. Nửa đầu của biểu đồ phù hợp với phân phối log-chuẩn và nửa sau của biểu đồ mô hình phân phối chuẩn khá tốt
Các hạt nhân khác nhau để ước tính mật độ
x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
1 cho phép ước tính mật độ hạt nhân bằng các hàm hạt nhân khác nhau
6. \[K[a;h] \propto \cos [\frac{\pi a}{2h}] \text { if }. a. < h \]x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
7. \[K[a;h] \propto 1 - \frac{a^2}{h^2}\]x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
8. \[K[a;h] \propto \exp [-\frac{. a. {h}]\]x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
9. \[K[a;h] \propto \exp[-\frac{a^2}{2h^2}]\]x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
10. \[K[a;h] \propto 1 - \frac{. a. }{h} \text {nếu }. a. < h \]x_train = generate_data[][:, np.newaxis] fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]] plt.subplot[121] plt.scatter[np.arange[len[x_train]], x_train, c='red'] plt.xlabel['Sample no.'] plt.ylabel['Value'] plt.title['Scatter plot'] plt.subplot[122] plt.hist[x_train, bins=50] plt.title['Histogram'] fig.subplots_adjust[wspace=.3] plt.show[]
11. \[K[a;h] \propto 1 \text { if }. a. < h \]x_train = generate_data[][:, np.newaxis] fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]] plt.subplot[121] plt.scatter[np.arange[len[x_train]], x_train, c='red'] plt.xlabel['Sample no.'] plt.ylabel['Value'] plt.title['Scatter plot'] plt.subplot[122] plt.hist[x_train, bins=50] plt.title['Histogram'] fig.subplots_adjust[wspace=.3] plt.show[]
Một cách đơn giản để hiểu cách thức hoạt động của các hạt nhân này là vẽ chúng. Điều này có nghĩa là xây dựng một mô hình bằng cách sử dụng một mẫu chỉ có một giá trị, ví dụ: 0. Tiếp theo, ước tính mật độ của tất cả các điểm quanh 0 và vẽ mật độ dọc theo trục y. Đoạn mã dưới đây cho thấy toàn bộ quá trình
x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
0Thử nghiệm với các hạt nhân khác nhau
Hãy thử nghiệm với các hạt nhân khác nhau và xem cách chúng ước tính hàm mật độ xác suất cho dữ liệu tổng hợp của chúng ta
Chúng ta có thể sử dụng
x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
1, như trước đây, để tìm giá trị x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
0 tối ưu. Tuy nhiên, đối với hạt nhân x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
14, x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
15 và x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
16, x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
1 có thể đưa ra cảnh báo thời gian chạy do một số điểm dẫn đến giá trị x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
18. Một cách khả thi để giải quyết vấn đề này là viết một chức năng chấm điểm tùy chỉnh cho x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
1Trong mã bên dưới, điểm số của
x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
18 cho điểm kiểm tra được bỏ qua trong hàm tính điểm tùy chỉnh của x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
21 và giá trị trung bình được trả về. Đây không nhất thiết là sơ đồ tốt nhất để xử lý các giá trị điểm số x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
18 và một số chiến lược khác có thể được áp dụng, tùy thuộc vào dữ liệu được đề cậpMô hình tối ưu hóa cuối cùng
Ví dụ trên cho thấy cách các hạt nhân khác nhau ước tính mật độ theo những cách khác nhau. Một bước cuối cùng là thiết lập
x_test = np.linspace[-1, 7, 2000][:, np.newaxis]
1 để nó không chỉ phát hiện ra băng thông tối ưu mà còn cả hạt nhân tối ưu cho dữ liệu mẫu của chúng ta. Đây là mã cuối cùng cũng vẽ ước tính mật độ cuối cùng và các tham số được điều chỉnh của nó trong tiêu đề ôx_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
0Tiến xa hơn - Cầm tay chỉ việc End-to-End Project
Bản chất tò mò của bạn khiến bạn muốn tiến xa hơn? . "Dự đoán giá nhà thực tế - Học máy bằng Python"
Trong dự án có hướng dẫn này - bạn sẽ học cách xây dựng các mô hình học máy truyền thống mạnh mẽ cũng như các mô hình học sâu, sử dụng Học tập đồng bộ và đào tạo những người học meta để dự đoán giá nhà từ một loạt các mô hình Scikit-Learn và Keras
Sử dụng Keras, API học sâu được xây dựng dựa trên Tensorflow, chúng tôi sẽ thử nghiệm các kiến trúc, xây dựng một tập hợp các mô hình xếp chồng lên nhau và huấn luyện mạng thần kinh siêu học viên [mô hình cấp 1] để tìm ra giá của một ngôi nhà
Học sâu thật tuyệt vời - nhưng trước khi dùng đến nó, bạn cũng nên cố gắng giải quyết vấn đề bằng các kỹ thuật đơn giản hơn, chẳng hạn như với các thuật toán học nông. Hiệu suất cơ bản của chúng tôi sẽ dựa trên thuật toán Hồi quy rừng ngẫu nhiên. Ngoài ra - chúng ta sẽ khám phá việc tạo các nhóm mô hình thông qua Scikit-Learn thông qua các kỹ thuật như đóng gói và bỏ phiếu
Đây là một dự án đầu cuối và giống như tất cả các dự án Máy học, chúng tôi sẽ bắt đầu với - với Phân tích dữ liệu khám phá, tiếp theo là Tiền xử lý dữ liệu và cuối cùng là Xây dựng các mô hình học nông và học sâu để phù hợp với dữ liệu mà chúng tôi đã khám phá và
Sự kết luận
Ước tính mật độ hạt nhân bằng cách sử dụng thư viện của
x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
1 x_train = generate_data[][:, np.newaxis]
fig, ax = plt.subplots[nrows=1, ncols=2, figsize=[10, 5]]
plt.subplot[121]
plt.scatter[np.arange[len[x_train]], x_train, c='red']
plt.xlabel['Sample no.']
plt.ylabel['Value']
plt.title['Scatter plot']
plt.subplot[122]
plt.hist[x_train, bins=50]
plt.title['Histogram']
fig.subplots_adjust[wspace=.3]
plt.show[]
25 đã được thảo luận trong bài viết này. Các ví dụ được đưa ra cho dữ liệu đơn biến, tuy nhiên nó cũng có thể được áp dụng cho dữ liệu có nhiều chiềuMặc dù là một cách trực quan và đơn giản để ước tính mật độ cho các bản phân phối nguồn không xác định, một nhà khoa học dữ liệu nên sử dụng nó một cách thận trọng vì lời nguyền của chiều có thể làm chậm đáng kể