Hướng dẫn parzen-window classifier python - phân loại parzen-window python

Cửa sổ Parzen là một kỹ thuật ước tính mật độ không tham số. Ước tính mật độ trong nhận dạng mẫu có thể đạt được bằng cách sử dụng cách tiếp cận của các cửa sổ Parzen. Kỹ thuật ước tính mật độ cửa sổ Parzen là một loại khái quát hóa kỹ thuật biểu đồ. & NBSP; is a non-parametric density estimation technique. Density estimation in Pattern Recognition can be achieved by using the approach of the Parzen Windows. Parzen window density estimation technique is a kind of generalization of the histogram technique. 

Nó được sử dụng để lấy hàm mật độ, & nbsp;. & Nbsp; được sử dụng để thực hiện phân loại Bayes. Khi chúng ta có một tính năng mẫu mới & nbsp; và khi cần phải tính toán giá trị của mật độ có điều kiện lớp, & nbsp; được sử dụng. & Nbsp; lấy giá trị dữ liệu đầu vào mẫu và trả về ước tính mật độ của mẫu dữ liệu đã cho. & NBSP;


is used to implement a Bayes Classifier. When we have a new sample feature 
and when there is a need to compute the value of the class conditional densities, 
is used. 
takes sample input data value and returns the density estimate of the given data sample. 

Một hypercube n-chiều được coi là được giả định là sở hữu các mẫu dữ liệu k. & Nbsp; chiều dài của cạnh của hypercube được coi là hn. & Nbsp;
The length of the edge of the hypercube is assumed to be hn. 

Do đó khối lượng của hypercube là: vn = hnd & nbsp;

Chúng tôi xác định hàm cửa sổ hypercube, [u] là hàm chỉ báo của hypercube đơn vị tập trung ở gốc .:&nbsφ[u] which is an indicator function of the unit hypercube which is centered at origin.: 
φ[u] = 1 if |ui| [unit_len/2]: return 0 return 1

2.3.2 Định lượng các điểm mẫu bên trong 3D-hypercube #

Sử dụng chức năng cửa sổ mà chúng tôi vừa triển khai ở trên, bây giờ chúng ta hãy định lượng có bao nhiêu điểm thực sự nằm bên trong và bên ngoài HyperCube.

X_all = np.vstack[[X_inside,X_outside]]
assert[X_all.shape == [10,3]]

k_n = 0
for row in X_all:
    k_n += window_function[row.reshape[3,1]]

print['Points inside the hypercube:', k_n]
print['Points outside the hybercube:', len[X_all] - k_n]

Points inside the hypercube: 3
Points outside the hypercube: 7

Dựa trên chức năng cửa sổ mà chúng tôi xác định trong phần trên, giờ đây chúng tôi có thể xây dựng ước tính Parzen-Window với hạt nhân HyperCube như sau:

Và áp dụng điều này vào ví dụ đơn vị-hypercube của chúng tôi ở trên, trong đó 3 trong số 10 mẫu nằm trong hypercube [vào vùng R], chúng tôi có thể tính toán xác suất P [x] mà các mẫu X nằm trong vùng R như sau:x] that x samples fall within region R as follows:

2.5 Giả định quan trọng: Hội tụ #

Một trong những giả định quan trọng nhất tại sao kỹ thuật Parzen-Window hoạt động [cũng như kỹ thuật hàng xóm gần nhất K] là PN [trong đó chỉ số N biểu thị số lượng mẫu của các mẫu Chúng tôi cho rằng một số lượng vô hạn các mẫu đào tạo, được Emanuel Parzen thể hiện độc đáo trong bài báo của mình.x] when we assume an infinite number of training samples, which was nicely shown by Emanuel Parzen in his paper.

2.6 Các thông số quan trọng của kỹ thuật Parzen-Window: chiều rộng cửa sổ và kernel #

Hai tham số quan trọng trong các kỹ thuật cửa sổ parzen là

  • 1] Chiều rộng cửa sổ
  • 2] Hạt nhân

1] Chiều rộng cửa sổ: Hãy để chúng tôi bỏ qua phần này ngay bây giờ và thảo luận và khám phá câu hỏi về việc chọn một chiều rộng cửa sổ thích hợp sau đó bằng cách sử dụng một ví dụ thực hành.
Let us skip this part for now and discuss and explore the question of choosing an appropriate window width later by using an hands-on example.

2] Kernel: Thông thường nhất, một hypercube hoặc hạt nhân Gaussian được sử dụng cho chức năng cửa sổ. Nhưng làm thế nào để chúng ta biết cái nào tốt hơn? Nó thực sự phụ thuộc vào mẫu đào tạo. Trong thực tế, sự lựa chọn thường được thực hiện bằng cách kiểm tra trình phân loại mẫu dẫn xuất để xem phương pháp nào dẫn đến hiệu suất tốt hơn trên tập dữ liệu thử nghiệm. Theo trực giác, sẽ có ý nghĩa khi sử dụng một hạt nhân Gaussian cho một bộ dữ liệu theo phân phối Gaussian. Nhưng hãy nhớ rằng, toàn bộ mục đích của ước tính Parzen-Window là ước tính mật độ của một phân phối không xác định! Vì vậy, trong thực tế, chúng tôi sẽ biết liệu dữ liệu của chúng tôi có bắt nguồn từ phân phối Gaussian hay không [nếu không chúng tôi sẽ cần ước tính, nhưng có thể sử dụng một kỹ thuật tham số như ước tính MLE hoặc Bayes]. Nếu chúng ta quyết định sử dụng hạt nhân Gaussian thay vì hypercube, chúng ta chỉ có thể đổi các điều khoản của hàm cửa sổ, mà chúng ta đã xác định ở trên cho HyperCube, bằng cách:::
Most commonly, either a hypercube or a Gaussian kernel is used for the window function. But how do we know which is better? It really depends on the training sample. In practice, the choice is often made by testing the derived pattern classifier to see which method leads to a better performance on the test data set.
Intuitively, it would make sense to use a Gaussian kernel for a data set that follows a Gaussian distribution. But remember, the whole purpose of the Parzen-window estimation is to estimate densities of a unknown distribution! So, in practice we wouldn’t know whether our data stems from a Gaussian distribution or not [otherwise we wouldn’t need to estimate, but can use a parametric technique like MLE or Bayesian Estimation].
If we would decide to use a Gaussian kernel instead of the hypercube, we can just simply swap the terms of the window function, which we defined above for the hypercube, by:

Ước tính Parzen-Window sau đó sẽ trông như thế này:

Trộn các hạt nhân khác nhau: Trong một số bài báo bạn sẽ thấy, các tác giả trộn hạt nhân HyperCube và Gaussian để ước tính mật độ ở các vùng khác nhau. Trong thực tế, điều này có thể hoạt động rất tốt, tuy nhiên, lưu ý rằng về lý thuyết, điều này sẽ vi phạm các nguyên tắc cơ bản: mật độ tích hợp vào 1.
In some papers you will see, that the authors mixed hypercube and Gaussian kernels to estimate the densities at different regions. In practice, this might work very well, however, but note that in theory this would violate on of the underlying principles: that the density integrates to 1.

  • Một vài yêu cầu khác là ở giới hạn khối lượng chúng tôi chọn cho cửa sổ Parzen trở nên vô hạn nhỏ.

  • Số lượng điểm KN trong khu vực này hội tụ đến:

  • từ đó chúng ta có thể kết luận:

2.7 Thực hiện ước tính Parzen-Window #

Và một lần nữa, chúng ta hãy đi đến phần thú vị hơn và triển khai mã cho kernel HyperCube:

def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
    '''
    Implementation of the Parzen-window estimation for hypercubes.

    Keyword arguments:
        x_samples: A 'n x d'-dimensional numpy array, where each sample
            is stored in a separate row.
        h: The length of the hypercube.
        center: The coordinate center of the hypercube

    Returns the probability density for observing k samples inside the hypercube.

    '''
    dimensions = x_samples.shape[1]

    assert [len[center] == dimensions],  
            'Number of center coordinates have to match sample dimensions'
    k = 0
    for x in x_samples:
        is_inside = 1
        for axis,center_point in zip[x, center]:
            if np.abs[axis-center_point] > [h/2]:
                is_inside = 0
        k += is_inside
    return [k / len[x_samples]] / [h**dimensions]

print['p[x] =', parzen_window_est[X_all, h=1]]

Tại thời điểm này, tôi hoàn toàn có thể hiểu nếu bạn mất tổng quan một chút. Tôi đã tóm tắt ba phần quan trọng [kernel hypercube, chức năng cửa sổ và ước tính parzen-window kết quả] trong phần sau và tôi nghĩ rằng nó đáng để xem xét ngắn gọn về nó, trước khi chúng tôi áp dụng nó vào một tập dữ liệu bên dưới.

3 Áp dụng phương pháp parzen-Window cho bộ dữ liệu Gaussian đa biến ngẫu nhiên #

Chúng ta hãy sử dụng bộ dữ liệu 2 chiều được rút ra từ phân phối Gaussian đa biến để áp dụng kỹ thuật Parzen-Window để ước tính mật độ.

3.1 Tạo 10000 mẫu 2D ngẫu nhiên từ phân phối Gaussian #

Hàm mật độ xác suất Gaussian đa biến chung [PDF] được định nghĩa là:

Và chúng tôi sẽ sử dụng các tham số sau để vẽ các mẫu ngẫu nhiên:

import numpy as np

# Generate 10,000 random 2D-patterns
mu_vec = np.array[[0,0]]
cov_mat = np.array[[[1,0],[0,1]]]
x_2Dgauss = np.random.multivariate_normal[mu_vec, cov_mat, 10000]

print[x_2Dgauss.shape]

#from matplotlib import pyplot as plt

f, ax = plt.subplots[figsize=[7, 7]]
ax.scatter[x_2Dgauss[:,0], x_2Dgauss[:,1],
        marker='o', color='green', s=4, alpha=0.3]
plt.title['10000 samples randomly drawn from a 2D Gaussian distribution']
plt.ylabel['x2']
plt.xlabel['x1']
ftext = 'p[x] ~ N[mu=[0,0]^t, cov=I]'
plt.figtext[.15,.85, ftext, fontsize=11, ha='left']
plt.ylim[[-4,4]]
plt.xlim[[-4,4]]

plt.show[]

3.2 Thực hiện và vẽ hàm mật độ Gaussian đa biến #

3.2.1 Vẽ mật độ Gaussian bivariate #

Đầu tiên, chúng ta hãy vẽ sơ đồ phân phối Gaussian đa biến [ở đây: bivariate] trong một ô 3D để có ý tưởng tốt hơn về phân phối mật độ thực tế.

#import numpy as np
#from matplotlib import pyplot as plt

from matplotlib.mlab import bivariate_normal
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure[figsize=[10, 7]]
ax = fig.gca[projection='3d']
x = np.linspace[-5, 5, 200]
y = x
X,Y = np.meshgrid[x, y]
Z = bivariate_normal[X, Y]
surf = ax.plot_surface[X, Y, Z, rstride=1,
        cstride=1, cmap=plt.cm.coolwarm,
        linewidth=0, antialiased=False
    ]

ax.set_zlim[0, 0.2]
ax.zaxis.set_major_locator[plt.LinearLocator[10]]
ax.zaxis.set_major_formatter[plt.FormatStrFormatter['%.02f']]

ax.set_xlabel['X']
ax.set_ylabel['Y']
ax.set_zlabel['p[x]']

plt.title['Bivariate Gaussian distribution']
fig.colorbar[surf, shrink=0.5, aspect=7, cmap=plt.cm.coolwarm]

plt.show[]

3.2.2 Thực hiện mã để tính mật độ Gaussian đa biến #

Để tính toán xác suất từ ​​hàm mật độ Gaussian đa biến và so sánh kết quả ước tính của chúng tôi, hãy để chúng tôi thực hiện nó bằng phương trình sau:

Thật không may, hiện tại không có thư viện Python cung cấp chức năng này. Tin tốt là,

Points inside the hypercube: 3
Points outside the hypercube: 7
7 sẽ được triển khai trong ứng cử viên phát hành mới là
Points inside the hypercube: 3
Points outside the hypercube: 7
8 [câu 0,14].

#import numpy as np

def pdf_multivariate_gauss[x, mu, cov]:
    '''
    Caculate the multivariate normal density [pdf]

    Keyword arguments:
        x = numpy array of a "d x 1" sample vector
        mu = numpy array of a "d x 1" mean vector
        cov = "numpy array of a d x d" covariance matrix
    '''
    assert[mu.shape[0] > mu.shape[1]],\
        'mu must be a row vector'
    assert[x.shape[0] > x.shape[1]],\
        'x must be a row vector'
    assert[cov.shape[0] == cov.shape[1]],\
        'covariance matrix must be square'
    assert[mu.shape[0] == cov.shape[0]],\
        'cov_mat and mu_vec must have the same dimensions'
    assert[mu.shape[0] == x.shape[0]],\
        'mu and x must have the same dimensions'

    part1 = 1 / [ [[2* np.pi]**[len[mu]/2]] * [np.linalg.det[cov]**[1/2]] ]
    part2 = [-1/2] * [[x-mu].T.dot[np.linalg.inv[cov]]].dot[[x-mu]]
    return float[part1 * np.exp[part2]]

3.2.2.1 Kiểm tra triển khai PDF Gaussian đa biến #

Chúng ta hãy nhanh chóng xác nhận rằng Gaussian đa biến, mà chúng tôi vừa triển khai, hoạt động như mong đợi bằng cách so sánh nó với Gaussian bivariate từ gói matplotlib.mlab.

%matplotlib inline

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
fig = plt.figure[figsize=[7,7]]
ax = fig.gca[projection='3d']
ax.set_aspect["equal"]

# Plot Points

# samples within the cube
X_inside = np.array[[[0,0,0],[0.2,0.2,0.2],[0.1, -0.1, -0.3]]]

X_outside = np.array[[[-1.2,0.3,-0.3],[0.8,-0.82,-0.9],[1, 0.6, -0.7],
                  [0.8,0.7,0.2],[0.7,-0.8,-0.45],[-0.3, 0.6, 0.9],
                  [0.7,-0.6,-0.8]]]

for row in X_inside:
    ax.scatter[row[0], row[1], row[2], color="r", s=50, marker='^']

for row in X_outside:
    ax.scatter[row[0], row[1], row[2], color="k", s=50]

# Plot Cube
h = [-0.5, 0.5]
for s, e in combinations[np.array[list[product[h,h,h]]], 2]:
    if np.sum[np.abs[s-e]] == h[1]-h[0]:
        ax.plot3D[*zip[s,e], color="g"]

ax.set_xlim[-1.5, 1.5]
ax.set_ylim[-1.5, 1.5]
ax.set_zlim[-1.5, 1.5]

plt.show[]
0

%matplotlib inline

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
fig = plt.figure[figsize=[7,7]]
ax = fig.gca[projection='3d']
ax.set_aspect["equal"]

# Plot Points

# samples within the cube
X_inside = np.array[[[0,0,0],[0.2,0.2,0.2],[0.1, -0.1, -0.3]]]

X_outside = np.array[[[-1.2,0.3,-0.3],[0.8,-0.82,-0.9],[1, 0.6, -0.7],
                  [0.8,0.7,0.2],[0.7,-0.8,-0.45],[-0.3, 0.6, 0.9],
                  [0.7,-0.6,-0.8]]]

for row in X_inside:
    ax.scatter[row[0], row[1], row[2], color="r", s=50, marker='^']

for row in X_outside:
    ax.scatter[row[0], row[1], row[2], color="k", s=50]

# Plot Cube
h = [-0.5, 0.5]
for s, e in combinations[np.array[list[product[h,h,h]]], 2]:
    if np.sum[np.abs[s-e]] == h[1]-h[0]:
        ax.plot3D[*zip[s,e], color="g"]

ax.set_xlim[-1.5, 1.5]
ax.set_ylim[-1.5, 1.5]
ax.set_zlim[-1.5, 1.5]

plt.show[]
1

3.2.3 So sánh ước tính cửa sổ Parzen với mật độ thực tế #

Và cuối cùng, chúng ta hãy so sánh các mật độ mà chúng ta ước tính sử dụng việc thực hiện ước tính Parzen-Window với mật độ Gaussian đa biến thực tế.

Nhưng trước khi chúng tôi thực hiện so sánh, chúng tôi phải tự hỏi mình thêm một câu hỏi: chúng tôi nên chọn kích thước cửa sổ nào [tức là, giá trị h bên h của hypercube của chúng tôi] là gì? Chiều rộng cửa sổ là một hàm của số lượng mẫu đào tạo,

Nhưng tại sao √n, không chỉ n?

Điều này là do số lượng điểm KN trong cửa sổ tăng nhỏ hơn nhiều so với số lượng mẫu đào tạo, mặc dù

Chúng tôi có: k [unit_len/2]: return 0 return 1 0

Bây giờ chúng ta có thể sử dụng chức năng

Points inside the hypercube: 3
Points outside the hypercube: 7
9 với hạt nhân HyperCube để tính P [x].x].

def window_function[x_vec, unit_len=1]:
"""
Implementation of the window function. Returns 1 if 3x1-sample vector
lies within a origin-centered hypercube, 0 otherwise.

"""
for row in x_vec:
    if np.abs[row] > [unit_len/2]:
        return 0
return 1
1

Và chúng ta hãy nhanh chóng xác nhận rằng nó hoạt động trên một bộ dữ liệu lớn hơn cho các siêu đơn vị không được tập trung ở nguồn gốc: [Lưu ý rằng tôi chỉ chọn độ dài tùy ý cho H, đây có thể không phải là lựa chọn tốt nhất cho H.]
[Note that I just chose an arbitrary length for h, this is likely not the best choice for h.]

def window_function[x_vec, unit_len=1]:
"""
Implementation of the window function. Returns 1 if 3x1-sample vector
lies within a origin-centered hypercube, 0 otherwise.

"""
for row in x_vec:
    if np.abs[row] > [unit_len/2]:
        return 0
return 1
2

def window_function[x_vec, unit_len=1]:
"""
Implementation of the window function. Returns 1 if 3x1-sample vector
lies within a origin-centered hypercube, 0 otherwise.

"""
for row in x_vec:
    if np.abs[row] > [unit_len/2]:
        return 0
return 1
3

def window_function[x_vec, unit_len=1]:
"""
Implementation of the window function. Returns 1 if 3x1-sample vector
lies within a origin-centered hypercube, 0 otherwise.

"""
for row in x_vec:
    if np.abs[row] > [unit_len/2]:
        return 0
return 1
4

5.2 Sử dụng hạt nhân Gaussian từ
Points inside the hypercube: 3
Points outside the hypercube: 7
6 #

Vì chúng tôi đã trải qua kỹ thuật Parzen-Window từng bước cho hạt nhân HyperCube, chúng tôi hãy nhập lớp

def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
    '''
    Implementation of the Parzen-window estimation for hypercubes.

    Keyword arguments:
        x_samples: A 'n x d'-dimensional numpy array, where each sample
            is stored in a separate row.
        h: The length of the hypercube.
        center: The coordinate center of the hypercube

    Returns the probability density for observing k samples inside the hypercube.

    '''
    dimensions = x_samples.shape[1]

    assert [len[center] == dimensions],  
            'Number of center coordinates have to match sample dimensions'
    k = 0
    for x in x_samples:
        is_inside = 1
        for axis,center_point in zip[x, center]:
            if np.abs[axis-center_point] > [h/2]:
                is_inside = 0
        k += is_inside
    return [k / len[x_samples]] / [h**dimensions]

print['p[x] =', parzen_window_est[X_all, h=1]]
1 từ gói
Points inside the hypercube: 3
Points outside the hypercube: 7
8 để tiếp cận thuận tiện hơn.

Tài liệu hoàn chỉnh có thể được tìm thấy trên Docs.Scipy.org.

Lớp

def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
    '''
    Implementation of the Parzen-window estimation for hypercubes.

    Keyword arguments:
        x_samples: A 'n x d'-dimensional numpy array, where each sample
            is stored in a separate row.
        h: The length of the hypercube.
        center: The coordinate center of the hypercube

    Returns the probability density for observing k samples inside the hypercube.

    '''
    dimensions = x_samples.shape[1]

    assert [len[center] == dimensions],  
            'Number of center coordinates have to match sample dimensions'
    k = 0
    for x in x_samples:
        is_inside = 1
        for axis,center_point in zip[x, center]:
            if np.abs[axis-center_point] > [h/2]:
                is_inside = 0
        k += is_inside
    return [k / len[x_samples]] / [h**dimensions]

print['p[x] =', parzen_window_est[X_all, h=1]]
1 lấy 2 tham số làm đầu vào

  • DataSet:

    def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
        '''
        Implementation of the Parzen-window estimation for hypercubes.
    
        Keyword arguments:
            x_samples: A 'n x d'-dimensional numpy array, where each sample
                is stored in a separate row.
            h: The length of the hypercube.
            center: The coordinate center of the hypercube
    
        Returns the probability density for observing k samples inside the hypercube.
    
        '''
        dimensions = x_samples.shape[1]
    
        assert [len[center] == dimensions],  
                'Number of center coordinates have to match sample dimensions'
        k = 0
        for x in x_samples:
            is_inside = 1
            for axis,center_point in zip[x, center]:
                if np.abs[axis-center_point] > [h/2]:
                    is_inside = 0
            k += is_inside
        return [k / len[x_samples]] / [h**dimensions]
    
    print['p[x] =', parzen_window_est[X_all, h=1]]
    
    4 Datapoints để ước tính từ. Trong trường hợp dữ liệu đơn biến, đây là mảng 1-D, nếu không thì một mảng 2 chiều có hình [ # của DIMS, # của dữ liệu]. :
    def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
        '''
        Implementation of the Parzen-window estimation for hypercubes.
    
        Keyword arguments:
            x_samples: A 'n x d'-dimensional numpy array, where each sample
                is stored in a separate row.
            h: The length of the hypercube.
            center: The coordinate center of the hypercube
    
        Returns the probability density for observing k samples inside the hypercube.
    
        '''
        dimensions = x_samples.shape[1]
    
        assert [len[center] == dimensions],  
                'Number of center coordinates have to match sample dimensions'
        k = 0
        for x in x_samples:
            is_inside = 1
            for axis,center_point in zip[x, center]:
                if np.abs[axis-center_point] > [h/2]:
                    is_inside = 0
            k += is_inside
        return [k / len[x_samples]] / [h**dimensions]
    
    print['p[x] =', parzen_window_est[X_all, h=1]]
    
    4 Datapoints to estimate from. In case of univariate data this is a 1-D array, otherwise a 2-D array with shape [# of dims, # of data].

  • BW_METHOD:

    def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
        '''
        Implementation of the Parzen-window estimation for hypercubes.
    
        Keyword arguments:
            x_samples: A 'n x d'-dimensional numpy array, where each sample
                is stored in a separate row.
            h: The length of the hypercube.
            center: The coordinate center of the hypercube
    
        Returns the probability density for observing k samples inside the hypercube.
    
        '''
        dimensions = x_samples.shape[1]
    
        assert [len[center] == dimensions],  
                'Number of center coordinates have to match sample dimensions'
        k = 0
        for x in x_samples:
            is_inside = 1
            for axis,center_point in zip[x, center]:
                if np.abs[axis-center_point] > [h/2]:
                    is_inside = 0
            k += is_inside
        return [k / len[x_samples]] / [h**dimensions]
    
    print['p[x] =', parzen_window_est[X_all, h=1]]
    
    5 Phương pháp được sử dụng để tính toán băng thông ước tính. Đây có thể là
    def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
        '''
        Implementation of the Parzen-window estimation for hypercubes.
    
        Keyword arguments:
            x_samples: A 'n x d'-dimensional numpy array, where each sample
                is stored in a separate row.
            h: The length of the hypercube.
            center: The coordinate center of the hypercube
    
        Returns the probability density for observing k samples inside the hypercube.
    
        '''
        dimensions = x_samples.shape[1]
    
        assert [len[center] == dimensions],  
                'Number of center coordinates have to match sample dimensions'
        k = 0
        for x in x_samples:
            is_inside = 1
            for axis,center_point in zip[x, center]:
                if np.abs[axis-center_point] > [h/2]:
                    is_inside = 0
            k += is_inside
        return [k / len[x_samples]] / [h**dimensions]
    
    print['p[x] =', parzen_window_est[X_all, h=1]]
    
    6,
    def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
        '''
        Implementation of the Parzen-window estimation for hypercubes.
    
        Keyword arguments:
            x_samples: A 'n x d'-dimensional numpy array, where each sample
                is stored in a separate row.
            h: The length of the hypercube.
            center: The coordinate center of the hypercube
    
        Returns the probability density for observing k samples inside the hypercube.
    
        '''
        dimensions = x_samples.shape[1]
    
        assert [len[center] == dimensions],  
                'Number of center coordinates have to match sample dimensions'
        k = 0
        for x in x_samples:
            is_inside = 1
            for axis,center_point in zip[x, center]:
                if np.abs[axis-center_point] > [h/2]:
                    is_inside = 0
            k += is_inside
        return [k / len[x_samples]] / [h**dimensions]
    
    print['p[x] =', parzen_window_est[X_all, h=1]]
    
    7, hằng số vô hướng hoặc có thể gọi được. Nếu một vô hướng, điều này sẽ được sử dụng trực tiếp làm kde.factor. Nếu có thể gọi được, nó sẽ lấy một thể hiện Gaussian_kde là tham số chỉ và trả về vô hướng. Nếu
    def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
        '''
        Implementation of the Parzen-window estimation for hypercubes.
    
        Keyword arguments:
            x_samples: A 'n x d'-dimensional numpy array, where each sample
                is stored in a separate row.
            h: The length of the hypercube.
            center: The coordinate center of the hypercube
    
        Returns the probability density for observing k samples inside the hypercube.
    
        '''
        dimensions = x_samples.shape[1]
    
        assert [len[center] == dimensions],  
                'Number of center coordinates have to match sample dimensions'
        k = 0
        for x in x_samples:
            is_inside = 1
            for axis,center_point in zip[x, center]:
                if np.abs[axis-center_point] > [h/2]:
                    is_inside = 0
            k += is_inside
        return [k / len[x_samples]] / [h**dimensions]
    
    print['p[x] =', parzen_window_est[X_all, h=1]]
    
    8 [mặc định],
    def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
        '''
        Implementation of the Parzen-window estimation for hypercubes.
    
        Keyword arguments:
            x_samples: A 'n x d'-dimensional numpy array, where each sample
                is stored in a separate row.
            h: The length of the hypercube.
            center: The coordinate center of the hypercube
    
        Returns the probability density for observing k samples inside the hypercube.
    
        '''
        dimensions = x_samples.shape[1]
    
        assert [len[center] == dimensions],  
                'Number of center coordinates have to match sample dimensions'
        k = 0
        for x in x_samples:
            is_inside = 1
            for axis,center_point in zip[x, center]:
                if np.abs[axis-center_point] > [h/2]:
                    is_inside = 0
            k += is_inside
        return [k / len[x_samples]] / [h**dimensions]
    
    print['p[x] =', parzen_window_est[X_all, h=1]]
    
    6 được sử dụng. Xem ghi chú để biết thêm chi tiết.
    :
    def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
        '''
        Implementation of the Parzen-window estimation for hypercubes.
    
        Keyword arguments:
            x_samples: A 'n x d'-dimensional numpy array, where each sample
                is stored in a separate row.
            h: The length of the hypercube.
            center: The coordinate center of the hypercube
    
        Returns the probability density for observing k samples inside the hypercube.
    
        '''
        dimensions = x_samples.shape[1]
    
        assert [len[center] == dimensions],  
                'Number of center coordinates have to match sample dimensions'
        k = 0
        for x in x_samples:
            is_inside = 1
            for axis,center_point in zip[x, center]:
                if np.abs[axis-center_point] > [h/2]:
                    is_inside = 0
            k += is_inside
        return [k / len[x_samples]] / [h**dimensions]
    
    print['p[x] =', parzen_window_est[X_all, h=1]]
    
    5 The method used to calculate the estimator bandwidth. This can be
    def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
        '''
        Implementation of the Parzen-window estimation for hypercubes.
    
        Keyword arguments:
            x_samples: A 'n x d'-dimensional numpy array, where each sample
                is stored in a separate row.
            h: The length of the hypercube.
            center: The coordinate center of the hypercube
    
        Returns the probability density for observing k samples inside the hypercube.
    
        '''
        dimensions = x_samples.shape[1]
    
        assert [len[center] == dimensions],  
                'Number of center coordinates have to match sample dimensions'
        k = 0
        for x in x_samples:
            is_inside = 1
            for axis,center_point in zip[x, center]:
                if np.abs[axis-center_point] > [h/2]:
                    is_inside = 0
            k += is_inside
        return [k / len[x_samples]] / [h**dimensions]
    
    print['p[x] =', parzen_window_est[X_all, h=1]]
    
    6,
    def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
        '''
        Implementation of the Parzen-window estimation for hypercubes.
    
        Keyword arguments:
            x_samples: A 'n x d'-dimensional numpy array, where each sample
                is stored in a separate row.
            h: The length of the hypercube.
            center: The coordinate center of the hypercube
    
        Returns the probability density for observing k samples inside the hypercube.
    
        '''
        dimensions = x_samples.shape[1]
    
        assert [len[center] == dimensions],  
                'Number of center coordinates have to match sample dimensions'
        k = 0
        for x in x_samples:
            is_inside = 1
            for axis,center_point in zip[x, center]:
                if np.abs[axis-center_point] > [h/2]:
                    is_inside = 0
            k += is_inside
        return [k / len[x_samples]] / [h**dimensions]
    
    print['p[x] =', parzen_window_est[X_all, h=1]]
    
    7, a scalar constant or a callable. If a scalar, this will be used directly as kde.factor. If a callable, it should take a gaussian_kde instance as only parameter and return a scalar. If
    def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
        '''
        Implementation of the Parzen-window estimation for hypercubes.
    
        Keyword arguments:
            x_samples: A 'n x d'-dimensional numpy array, where each sample
                is stored in a separate row.
            h: The length of the hypercube.
            center: The coordinate center of the hypercube
    
        Returns the probability density for observing k samples inside the hypercube.
    
        '''
        dimensions = x_samples.shape[1]
    
        assert [len[center] == dimensions],  
                'Number of center coordinates have to match sample dimensions'
        k = 0
        for x in x_samples:
            is_inside = 1
            for axis,center_point in zip[x, center]:
                if np.abs[axis-center_point] > [h/2]:
                    is_inside = 0
            k += is_inside
        return [k / len[x_samples]] / [h**dimensions]
    
    print['p[x] =', parzen_window_est[X_all, h=1]]
    
    8 [default],
    def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
        '''
        Implementation of the Parzen-window estimation for hypercubes.
    
        Keyword arguments:
            x_samples: A 'n x d'-dimensional numpy array, where each sample
                is stored in a separate row.
            h: The length of the hypercube.
            center: The coordinate center of the hypercube
    
        Returns the probability density for observing k samples inside the hypercube.
    
        '''
        dimensions = x_samples.shape[1]
    
        assert [len[center] == dimensions],  
                'Number of center coordinates have to match sample dimensions'
        k = 0
        for x in x_samples:
            is_inside = 1
            for axis,center_point in zip[x, center]:
                if np.abs[axis-center_point] > [h/2]:
                    is_inside = 0
            k += is_inside
        return [k / len[x_samples]] / [h**dimensions]
    
    print['p[x] =', parzen_window_est[X_all, h=1]]
    
    6 is used. See Notes for more details.

Lưu ý Khởi tạo một ví dụ

def parzen_window_est[x_samples, h=1, center=[0,0,0]]:
    '''
    Implementation of the Parzen-window estimation for hypercubes.

    Keyword arguments:
        x_samples: A 'n x d'-dimensional numpy array, where each sample
            is stored in a separate row.
        h: The length of the hypercube.
        center: The coordinate center of the hypercube

    Returns the probability density for observing k samples inside the hypercube.

    '''
    dimensions = x_samples.shape[1]

    assert [len[center] == dimensions],  
            'Number of center coordinates have to match sample dimensions'
    k = 0
    for x in x_samples:
        is_inside = 1
        for axis,center_point in zip[x, center]:
            if np.abs[axis-center_point] > [h/2]:
                is_inside = 0
        k += is_inside
    return [k / len[x_samples]] / [h**dimensions]

print['p[x] =', parzen_window_est[X_all, h=1]]
1 yêu cầu một mảng numpy trong đó các mẫu khác nhau được đặt hàng theo các cột và trong đó các hàng phản ánh kích thước của bộ dữ liệu - đây là lý do tại sao chúng ta phải truyền dữ liệu đào tạo được tạo trước đó của chúng tôi ở dạng được chuyển đổi.

Trước tiên, hãy xem nhanh cách phương thức Gaussian_kde [] hoạt động bằng cách sử dụng vô hướng đơn giản làm chiều rộng cửa sổ, giống như chúng ta đã làm với kernel HyperCube trước đó và ước tính mật độ ở trung tâm.

def window_function[x_vec, unit_len=1]:
"""
Implementation of the window function. Returns 1 if 3x1-sample vector
lies within a origin-centered hypercube, 0 otherwise.

"""
for row in x_vec:
    if np.abs[row] > [unit_len/2]:
        return 0
return 1
5

5.3 So sánh hạt nhân Gaussian và HyperCube cho chiều rộng cửa sổ tùy ý # #

Bây giờ chúng ta hãy so sánh hạt nhân Gaussian với HyperCube cho cùng một chiều rộng cửa sổ được chọn tùy ý:

def window_function[x_vec, unit_len=1]:
"""
Implementation of the window function. Returns 1 if 3x1-sample vector
lies within a origin-centered hypercube, 0 otherwise.

"""
for row in x_vec:
    if np.abs[row] > [unit_len/2]:
        return 0
return 1
6

def window_function[x_vec, unit_len=1]:
"""
Implementation of the window function. Returns 1 if 3x1-sample vector
lies within a origin-centered hypercube, 0 otherwise.

"""
for row in x_vec:
    if np.abs[row] > [unit_len/2]:
        return 0
return 1
7

5.4 So sánh các tính toán ước tính băng thông khác nhau cho hạt nhân Gaussian #

Lớp

import numpy as np

# Generate 10,000 random 2D-patterns
mu_vec = np.array[[0,0]]
cov_mat = np.array[[[1,0],[0,1]]]
x_2Dgauss = np.random.multivariate_normal[mu_vec, cov_mat, 10000]

print[x_2Dgauss.shape]
1 đi kèm với 2 tính toán ước tính của nhóm nhạc khác nhau, chúng ta hãy khám phá nếu nó tạo ra sự khác biệt khi chọn cái này trên bộ dữ liệu của chúng ta:

def window_function[x_vec, unit_len=1]:
"""
Implementation of the window function. Returns 1 if 3x1-sample vector
lies within a origin-centered hypercube, 0 otherwise.

"""
for row in x_vec:
    if np.abs[row] > [unit_len/2]:
        return 0
return 1
8

def window_function[x_vec, unit_len=1]:
"""
Implementation of the window function. Returns 1 if 3x1-sample vector
lies within a origin-centered hypercube, 0 otherwise.

"""
for row in x_vec:
    if np.abs[row] > [unit_len/2]:
        return 0
return 1
9

6 vẽ các ước tính từ các hạt khác nhau #

Và cuối cùng, chúng ta hãy vẽ các kết quả cho các phương pháp ước tính băng thông kernel Gaussian khác nhau cho phân phối ước tính.

X_all = np.vstack[[X_inside,X_outside]]
assert[X_all.shape == [10,3]]

k_n = 0
for row in X_all:
    k_n += window_function[row.reshape[3,1]]

print['Points inside the hypercube:', k_n]
print['Points outside the hybercube:', len[X_all] - k_n]
0

6.1 Hiệu ứng chiều rộng cửa sổ: Đỉnh địa phương và tính trung bình #

Như chúng ta đã thảo luận trong một phần trước đó, việc chọn một chiều rộng cửa sổ thích hợp có thể là một nhiệm vụ đầy thách thức, nếu chúng ta chọn một chiều rộng cửa sổ quá nhỏ, chúng ta sẽ kết thúc với các đột biến cục bộ trong mật độ và nếu chúng ta chọn chiều rộng cửa sổ mà là quá lớn, chúng tôi sẽ tính trung bình trên toàn bộ phân phối:

X_all = np.vstack[[X_inside,X_outside]]
assert[X_all.shape == [10,3]]

k_n = 0
for row in X_all:
    k_n += window_function[row.reshape[3,1]]

print['Points inside the hypercube:', k_n]
print['Points outside the hybercube:', len[X_all] - k_n]
1

X_all = np.vstack[[X_inside,X_outside]]
assert[X_all.shape == [10,3]]

k_n = 0
for row in X_all:
    k_n += window_function[row.reshape[3,1]]

print['Points inside the hypercube:', k_n]
print['Points outside the hybercube:', len[X_all] - k_n]
2

7 Sử dụng ước tính mật độ hạt nhân cho nhiệm vụ phân loại mẫu #

Trong phần giới thiệu, tôi đã đề cập rằng một ứng dụng phổ biến của kỹ thuật Parzen-Window là ước tính mật độ điều kiện của lớp [hoặc cũng thường được gọi là 'khả năng'] P [x* |]* trong một vấn đề phân loại mẫu được giám sát từ bộ dữ liệu đào tạo [trong đó X là một mẫu đa chiều thuộc lớp cụ thể ωi]. Bây giờ, chúng ta hãy sử dụng ước tính mật độ kernel để thiết kế một trình phân loại Bayes đơn giản.x* | ω]* in a supervised pattern classification problem from the training dataset [where x is a multi-dimensional sample that belongs to particular class ωi].
Now, let us use the kernel density estimation for designing a simple Bayes-classifier.

Quy tắc Bayes

Để thiết kế trình phân loại lỗi tối thiểu, chúng tôi sẽ sử dụng quy tắc Bayes,:

Quy tắc quyết định

Trong đó xác suất sau được sử dụng để xác định quy tắc quyết định của chúng tôi. Ví dụ: đối với một vấn đề 2 lớp đơn giản với hai nhãn lớp 1 và 2:

Quyết định 1 nếu P [1 | *x]> P [ω2 | x], nếu không, quyết định *2*x ] > P[ω2 | x ], else decide *ω2

Hàm mục tiêu

Đối với ví dụ này, chúng ta hãy đơn giản hóa vấn đề một chút. Chúng tôi sẽ giả sử rằng chúng tôi có xác suất trước bằng nhau [xác suất gặp phải một lớp cụ thể là bằng nhau]: P [1 | * x] * = P [ω2 | * x] * = 1/n
P[ω1 | *x ]* = P[ω2 | *x ]* = … = P[ω1 | *x ]* = 1/n

Và vì p [x] chỉ là một yếu tố tỷ lệ bằng nhau cho tất cả các xác suất sau, chúng ta có thể thả nó từ phương trình.x] is just a scale factor that is equal for all posterior probabilities, we can drop it from the equation.

Bây giờ, chúng ta có thể đơn giản hóa quy tắc quyết định, để nó chỉ phụ thuộc vào các linh mục. Đối với một vấn đề 2 lớp, điều này sẽ là

Quyết định 1 nếu P [1 | *x]> P [ω2 | x], nếu không, quyết định *2*x ] > P[ω2 | x ], else decide *ω2

Hàm mục tiêu

Đối với ví dụ này, chúng ta hãy đơn giản hóa vấn đề một chút. Chúng tôi sẽ giả sử rằng chúng tôi có xác suất trước bằng nhau [xác suất gặp phải một lớp cụ thể là bằng nhau]: P [1 | * x] * = P [ω2 | * x] * = 1/n
ωj -> max[P[ω1 | *x ]] for *j = 1, 2, …, c

Và vì p [x] chỉ là một yếu tố tỷ lệ bằng nhau cho tất cả các xác suất sau, chúng ta có thể thả nó từ phương trình.

Bây giờ, chúng ta có thể đơn giản hóa quy tắc quyết định, để nó chỉ phụ thuộc vào các linh mục. Đối với một vấn đề 2 lớp, điều này sẽ là

Bayes Classifier #

X_all = np.vstack[[X_inside,X_outside]]
assert[X_all.shape == [10,3]]

k_n = 0
for row in X_all:
    k_n += window_function[row.reshape[3,1]]

print['Points inside the hypercube:', k_n]
print['Points outside the hybercube:', len[X_all] - k_n]
3

X_all = np.vstack[[X_inside,X_outside]]
assert[X_all.shape == [10,3]]

k_n = 0
for row in X_all:
    k_n += window_function[row.reshape[3,1]]

print['Points inside the hypercube:', k_n]
print['Points outside the hybercube:', len[X_all] - k_n]
4

X_all = np.vstack[[X_inside,X_outside]]
assert[X_all.shape == [10,3]]

k_n = 0
for row in X_all:
    k_n += window_function[row.reshape[3,1]]

print['Points inside the hypercube:', k_n]
print['Points outside the hybercube:', len[X_all] - k_n]
5

X_all = np.vstack[[X_inside,X_outside]]
assert[X_all.shape == [10,3]]

k_n = 0
for row in X_all:
    k_n += window_function[row.reshape[3,1]]

print['Points inside the hypercube:', k_n]
print['Points outside the hybercube:', len[X_all] - k_n]
6

X_all = np.vstack[[X_inside,X_outside]]
assert[X_all.shape == [10,3]]

k_n = 0
for row in X_all:
    k_n += window_function[row.reshape[3,1]]

print['Points inside the hypercube:', k_n]
print['Points outside the hybercube:', len[X_all] - k_n]
7

X_all = np.vstack[[X_inside,X_outside]]
assert[X_all.shape == [10,3]]

k_n = 0
for row in X_all:
    k_n += window_function[row.reshape[3,1]]

print['Points inside the hypercube:', k_n]
print['Points outside the hybercube:', len[X_all] - k_n]
8

Và nói chung hơn [đối với nhiều lớp], trình phân loại của chúng tôi trở thành ωj -> max [p [1 | *x]] cho *j = 1, 2, triệt, c, c

7.1 Tạo bộ dữ liệu đào tạo và kiểm tra mẫu #

Chúng ta hãy tạo dữ liệu 2 chiều ngẫu nhiên cho 3 lớp từ phân phối Gaussian đa biến với các tham số sau:

Bây giờ, chúng tôi sẽ tạo 120 mẫu ngẫu nhiên cho mỗi trong số 3 lớp và chia nó thành một tập dữ liệu thử nghiệm và đào tạo có kích thước bằng nhau, để mỗi bộ sẽ chứa 30 mẫu cho mỗi lớp. 7.2 Thực hiện trình phân loại bằng quy tắc quyết định của Bayes, # ]] for *j = 1, 2, …, c

X_all = np.vstack[[X_inside,X_outside]]
assert[X_all.shape == [10,3]]

k_n = 0
for row in X_all:
    k_n += window_function[row.reshape[3,1]]

print['Points inside the hypercube:', k_n]
print['Points outside the hybercube:', len[X_all] - k_n]
9

Bây giờ, chúng ta hãy thực hiện phân loại. Để tóm tắt lại:

Chúng ta có thể loại bỏ các xác suất trước đó [các linh mục bằng nhau] và hệ số tỷ lệ:

ωj -> Max [*P [ω1

x]] cho *j = 1, 2,…, c

7.3 Ước tính mật độ thông qua kỹ thuật Parzen-Window với hạt nhân Gaussian #

Points inside the hypercube: 3
Points outside the hypercube: 7
1

Points inside the hypercube: 3
Points outside the hypercube: 7
2

Points inside the hypercube: 3
Points outside the hypercube: 7
3

Hoặc sự thuận tiện của chúng tôi, chúng tôi hãy sử dụng lớp KDE thư viện
Points inside the hypercube: 3
Points outside the hypercube: 7
6 để ước tính mật độ hạt nhân:

Points inside the hypercube: 3
Points outside the hypercube: 7
0

7.4 Phân loại dữ liệu kiểm tra và tính toán tỷ lệ lỗi #

Points inside the hypercube: 3
Points outside the hypercube: 7
4

Points inside the hypercube: 3
Points outside the hypercube: 7
5


Bây giờ, đã đến lúc phân loại dữ liệu kiểm tra và tính toán lỗi thực nghiệm.

Bài Viết Liên Quan

Toplist mới

Bài mới nhất

Chủ Đề