Sử dụng python nội suy numpy

Hàm numpy.interp() trả về phép nội suy tuyến tính từng phần một chiều cho một hàm với các điểm dữ liệu rời rạc đã cho (xp, fp), được đánh giá tại x

cú pháp. cục mịch. interp(x, xp, fp, left = Không, phải = Không, dấu chấm = Không)

Thông số
x. [array_like] Tọa độ x để đánh giá các giá trị được nội suy
xp. [Chuỗi số float 1-D] Tọa độ x của các điểm dữ liệu, phải tăng nếu khoảng thời gian đối số không được chỉ định. Mặt khác, xp được sắp xếp bên trong sau khi chuẩn hóa các ranh giới định kỳ với chu kỳ xp = xp %
fp. [Chuỗi 1-D của float hoặc phức] Tọa độ y của các điểm dữ liệu, cùng độ dài với xp
bên trái. [tùy chọn float hoặc phức tương ứng với fp] Giá trị trả về cho x < xp[0], mặc định là fp[0]
đúng. [tùy chọn float hoặc phức tương ứng với fp] Giá trị trả về cho x > xp[-1], mặc định là fp[-1]
giai đoạn = Stage. [Không hoặc thả nổi, tùy chọn] Khoảng thời gian cho tọa độ x. Tham số này cho phép nội suy chính xác các tọa độ x góc. Các tham số left và right bị bỏ qua nếu khoảng thời gian được chỉ định

Trở lại. [float or complex or ndarray] Các giá trị được nội suy, có cùng hình dạng với x

Mã số 1




# Python program explaining

# numpy.interp() function

      

# importing numpy as geek 

import numpy as geek 

  

[1.   1.   1.5  1.72 2.14]
0
[1.   1.   1.5  1.72 2.14]
1
[1.   1.   1.5  1.72 2.14]
5# numpy.interp() function8
[1.   1.   1.5  1.72 2.14]
7numpy.interp()5
[1.   1.   1.5  1.72 2.14]
7      2
[1.   1.   1.5  1.72 2.14]
7      4
[1.   1.   1.5  1.72 2.14]
7      6numpy.interp()1

scipy (đọc là /ˈsaɪpaɪ'/ "Sigh Pie") là phần mềm nguồn mở cho toán học, khoa học và kỹ thuật. Thư viện SciPy được xây dựng dựa trên thư viện NumPy, cung cấp thao tác mảng N thuận tiện và nhanh chóng. SciPy bao gồm các gói con (mô-đun con) cho tính toán tuyến tính, tối ưu hóa, tích hợp và thống kê

NumPy và SciPy rất dễ sử dụng, mạnh mẽ và được nhiều nhà khoa học, kỹ sư hàng đầu thế giới lựa chọn. Nếu bạn cần thao tác số trên máy tính và hiển thị kết quả hoặc công bố, hãy thử SciPy

Vì sao nên sử dụng scipy?

  • SciPy chứa nhiều loại gói phụ giúp giải quyết vấn đề phổ biến nhất liên quan đến tính toán khoa học
  • SciPy là thư viện Khoa học được sử dụng nhiều nhất chỉ sau Thư viện Khoa học GNU cho C/C++ hoặc Matlab
  • Ứng dụng nhẹ nhàng và hiểu biết cũng như sức mạnh tính toán nhanh
  • Nó có thể hoạt động trên mảng (mảng) của thư viện NumPy

Cài đặt thư viện

  • Thông qua các phân phối. Anaconda, Miniconda, WinPython, Pyzo
  • Sử dụng pip. python-m pip install --user scipy
    Bạn có thể cài đặt cùng lúc nhiều thư viện với pip. python-m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nos

Lưu ý. Numpy phải được cài đặt trước. Bạn cũng nên cài đặt Matplotlib khi sử dụng Scipy. Một số cách cài đặt Scipy khác chi tiết tại mặt đất

Khai báo thư viện

Để đơn giản và thuận tiện, các gói con của thư viện Scipy và các thư viện Numpy, Matplotlib thường được khai báo dưới dạng báo cáo

from scipy import special
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

special is a package con of scipy. Các gói con khác được khai báo tương tự

Các hàm cơ bản

scipy được xây dựng trên Numpy nên bạn có thể sử dụng các hàm của Numpy để thực hiện tất cả các thao tác mảng cơ bản. Chi tiết về các hàm này bạn có thể tìm thấy trong NumPy Reference Guide hoặc sử dụng các lệnh ,  và. Bên dưới là ví dụ về một số thao tác mảng cơ bản

Ví dụ 1. Ví dụ này được viết trong Python Shell

________số 8_______

Tổng quan về các gói con của Scipy

Việc sử dụng thư viện SciPy yêu cầu (hoặc tùy chọn phụ thuộc vào) một số thư viện khác để hoạt động, phụ thuộc chính là Python và NumPy. Nó yêu cầu một bộ sưu tập lớn hơn các thư viện và công cụ để xây dựng thư viện hoặc xây dựng tài liệu. Công cụ và thư viện luôn thay đổi. SciPy Mục đích tương thích với một số bản phát hành của thư viện và công cụ phụ thuộc. Việc buộc cơ sở người dùng sử dụng các thành phần khác nhau để nâng cấp cho mỗi bản phát hành sẽ làm giảm đáng kể giá trị của SciPy. Tuy nhiên, khả năng duy trì khả năng tương thích ngược với các công cụ / thư viện cũ đặt ra những hạn chế trong đó chức năng và khả năng mới hơn có thể được kết hợp. SciPy có một cách tiếp cận có phần bảo thủ là duy trì khả năng tương thích với một số bản phát hành chính của Python và NumPy trên nền tảng chính. Do đó người dùng cần chú ý đến sự tương thích giữa phiên bản của các thư viện khác có liên quan đến phiên bản của Scipy. Những thông tin này được tổng hợp trong mục Lộ trình chuỗi công cụ của tài liệu hướng dẫn scipy.  

scipy có nhiều gói con và liên tục được phát triển, bổ sung tính năng mới. Bảng tổng hợp các gói con của Scipy theo thứ tự bảng chữ cái

gói conMiêu tảclusterThuật toán phân cụm (Clustering Algorithms)
>>> # khai báo numpy
>>> import numpy as np
>>> # Hàm mrid()
>>> np.mgrid[0:5,0:4]
array([[[0, 0, 0, 0],
        [1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3],
        [4, 4, 4, 4]],

       [[0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3]]])
>>> # Hàm mrid() số ảo
>>> np.mgrid[0:5:4j,0:4:3j]
array([[[0.        , 0.        , 0.        ],
        [1.66666667, 1.66666667, 1.66666667],
        [3.33333333, 3.33333333, 3.33333333],
        [5.        , 5.        , 5.        ]],

       [[0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ]]])
>>> # Hàm đa thức
>>> from numpy import poly1d
>>> p = poly1d([2,6])
>>> print(p)
 
2 x + 6
>>> # Bình phương hàm p
>>> print(p*p)
   2
4 x + 24 x + 36
>>> # Đạo hàm của p
>>> print(p.deriv())
 
2
>>> # Giá trị p tại x = 1, 2, 10
>>> p([1,2,10])
array([ 8, 10, 26])
>>> # Hàm select() để lấy ra array theo điều kiện
>>> x = np.arange(15)
>>> dieu_kien = [x< 10, x>=5]
>>> chon_ra = [x, x**3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8, 27, 64,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
>>> dieu_kien = [x>12, x<3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 13, 14])
>>> 
0 Hằng số toán học và vật lý
>>> # khai báo numpy
>>> import numpy as np
>>> # Hàm mrid()
>>> np.mgrid[0:5,0:4]
array([[[0, 0, 0, 0],
        [1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3],
        [4, 4, 4, 4]],

       [[0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3]]])
>>> # Hàm mrid() số ảo
>>> np.mgrid[0:5:4j,0:4:3j]
array([[[0.        , 0.        , 0.        ],
        [1.66666667, 1.66666667, 1.66666667],
        [3.33333333, 3.33333333, 3.33333333],
        [5.        , 5.        , 5.        ]],

       [[0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ]]])
>>> # Hàm đa thức
>>> from numpy import poly1d
>>> p = poly1d([2,6])
>>> print(p)
 
2 x + 6
>>> # Bình phương hàm p
>>> print(p*p)
   2
4 x + 24 x + 36
>>> # Đạo hàm của p
>>> print(p.deriv())
 
2
>>> # Giá trị p tại x = 1, 2, 10
>>> p([1,2,10])
array([ 8, 10, 26])
>>> # Hàm select() để lấy ra array theo điều kiện
>>> x = np.arange(15)
>>> dieu_kien = [x< 10, x>=5]
>>> chon_ra = [x, x**3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8, 27, 64,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
>>> dieu_kien = [x>12, x<3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 13, 14])
>>> 
1Hàm biến đổi Fourier nhanh (Fast Fourier Transform)
>>> # khai báo numpy
>>> import numpy as np
>>> # Hàm mrid()
>>> np.mgrid[0:5,0:4]
array([[[0, 0, 0, 0],
        [1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3],
        [4, 4, 4, 4]],

       [[0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3]]])
>>> # Hàm mrid() số ảo
>>> np.mgrid[0:5:4j,0:4:3j]
array([[[0.        , 0.        , 0.        ],
        [1.66666667, 1.66666667, 1.66666667],
        [3.33333333, 3.33333333, 3.33333333],
        [5.        , 5.        , 5.        ]],

       [[0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ]]])
>>> # Hàm đa thức
>>> from numpy import poly1d
>>> p = poly1d([2,6])
>>> print(p)
 
2 x + 6
>>> # Bình phương hàm p
>>> print(p*p)
   2
4 x + 24 x + 36
>>> # Đạo hàm của p
>>> print(p.deriv())
 
2
>>> # Giá trị p tại x = 1, 2, 10
>>> p([1,2,10])
array([ 8, 10, 26])
>>> # Hàm select() để lấy ra array theo điều kiện
>>> x = np.arange(15)
>>> dieu_kien = [x< 10, x>=5]
>>> chon_ra = [x, x**3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8, 27, 64,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
>>> dieu_kien = [x>12, x<3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 13, 14])
>>> 
2Giải phương trình vi phân và phân tích
>>> # khai báo numpy
>>> import numpy as np
>>> # Hàm mrid()
>>> np.mgrid[0:5,0:4]
array([[[0, 0, 0, 0],
        [1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3],
        [4, 4, 4, 4]],

       [[0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3]]])
>>> # Hàm mrid() số ảo
>>> np.mgrid[0:5:4j,0:4:3j]
array([[[0.        , 0.        , 0.        ],
        [1.66666667, 1.66666667, 1.66666667],
        [3.33333333, 3.33333333, 3.33333333],
        [5.        , 5.        , 5.        ]],

       [[0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ]]])
>>> # Hàm đa thức
>>> from numpy import poly1d
>>> p = poly1d([2,6])
>>> print(p)
 
2 x + 6
>>> # Bình phương hàm p
>>> print(p*p)
   2
4 x + 24 x + 36
>>> # Đạo hàm của p
>>> print(p.deriv())
 
2
>>> # Giá trị p tại x = 1, 2, 10
>>> p([1,2,10])
array([ 8, 10, 26])
>>> # Hàm select() để lấy ra array theo điều kiện
>>> x = np.arange(15)
>>> dieu_kien = [x< 10, x>=5]
>>> chon_ra = [x, x**3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8, 27, 64,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
>>> dieu_kien = [x>12, x<3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 13, 14])
>>> 
3Nội suy và làm lệch spline
>>> # khai báo numpy
>>> import numpy as np
>>> # Hàm mrid()
>>> np.mgrid[0:5,0:4]
array([[[0, 0, 0, 0],
        [1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3],
        [4, 4, 4, 4]],

       [[0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3]]])
>>> # Hàm mrid() số ảo
>>> np.mgrid[0:5:4j,0:4:3j]
array([[[0.        , 0.        , 0.        ],
        [1.66666667, 1.66666667, 1.66666667],
        [3.33333333, 3.33333333, 3.33333333],
        [5.        , 5.        , 5.        ]],

       [[0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ]]])
>>> # Hàm đa thức
>>> from numpy import poly1d
>>> p = poly1d([2,6])
>>> print(p)
 
2 x + 6
>>> # Bình phương hàm p
>>> print(p*p)
   2
4 x + 24 x + 36
>>> # Đạo hàm của p
>>> print(p.deriv())
 
2
>>> # Giá trị p tại x = 1, 2, 10
>>> p([1,2,10])
array([ 8, 10, 26])
>>> # Hàm select() để lấy ra array theo điều kiện
>>> x = np.arange(15)
>>> dieu_kien = [x< 10, x>=5]
>>> chon_ra = [x, x**3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8, 27, 64,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
>>> dieu_kien = [x>12, x<3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 13, 14])
>>> 
4Đầu vào và đầu ra
>>> # khai báo numpy
>>> import numpy as np
>>> # Hàm mrid()
>>> np.mgrid[0:5,0:4]
array([[[0, 0, 0, 0],
        [1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3],
        [4, 4, 4, 4]],

       [[0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3]]])
>>> # Hàm mrid() số ảo
>>> np.mgrid[0:5:4j,0:4:3j]
array([[[0.        , 0.        , 0.        ],
        [1.66666667, 1.66666667, 1.66666667],
        [3.33333333, 3.33333333, 3.33333333],
        [5.        , 5.        , 5.        ]],

       [[0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ]]])
>>> # Hàm đa thức
>>> from numpy import poly1d
>>> p = poly1d([2,6])
>>> print(p)
 
2 x + 6
>>> # Bình phương hàm p
>>> print(p*p)
   2
4 x + 24 x + 36
>>> # Đạo hàm của p
>>> print(p.deriv())
 
2
>>> # Giá trị p tại x = 1, 2, 10
>>> p([1,2,10])
array([ 8, 10, 26])
>>> # Hàm select() để lấy ra array theo điều kiện
>>> x = np.arange(15)
>>> dieu_kien = [x< 10, x>=5]
>>> chon_ra = [x, x**3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8, 27, 64,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
>>> dieu_kien = [x>12, x<3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 13, 14])
>>> 
5Đại số tuyến tính_______8_______6Xử lý ảnh N

Scipy sắp tới sẽ phát triển hỗ trợ BLAS và LAPACK, thêm mảng thưa thớt bên cạnh ma trận thưa thớt, cải tiến biến đổi Fourier, hỗ trợ mảng phân tán và mảng GPU, cải thiện các mã nguồn (bản dựng nguồn) trên Windows và . Mỗi gói con đều được đội đóng góp (Contribute) xây dựng lộ trình phát triển riêng. Chi tiết tại đây. Tiếp theo chúng ta cùng tìm hiểu từng gói con

Chi tiết từng gói con

1. Các gói phân cụm (
>>> # hàm whiten()- Chuẩn hóa một nhóm quan sát trên cơ sở từng tính năng.
>>> import numpy as np
>>> from scipy.cluster.vq import whiten
>>> features  = np.array([[2.9, 3.3, 4.7], [7.5, 6.5, 7.2],[0.8, 0.1, 0.7,]])
>>> whiten(features)
array([[1.03644899, 1.26301815, 1.75565531],
       [2.68047153, 2.48776302, 2.68951452],
       [0.28591696, 0.03827328, 0.26148058]])
>>> # hàm vq - Gán mã từ một cuốn sách mã cho các quan sát.
>>> from scipy.cluster.vq import vq
>>> code_book = np.array([[1.,1.,1.,],[2.,1.,1.]])
>>> vq(features, code_book)
(array([1, 1, 0]), array([4.44859528, 9.9468588 , 0.96953597]))
>>> # hàm kmeans - Thực hiện k-mean trên một tập các vectơ quan sát tạo thành các cụm k
>>> from scipy.cluster.vq import vq, kmeans, whiten
>>> import matplotlib.pyplot as plt
>>> features = np.array([[],])
>>> from numpy import random
>>> features = np.array([[ 1.3,2.2],
                     [ 2.1,2.4],
                     [ 0.4,0.6],
                     [ 0.3,2.8],
                     [ 0.1,0.2],
                     [ 0.2,1.4],
                     [ 2.0,0.1],
                     [ 4.3,3.9],
                     [ 3.0,4.0]])
>>> whitened = whiten(features)
>>> book = np.array((whitened[0],whitened[1]))
>>> kmeans(whitened,book)
(array([[0.52090714, 0.86997969],
       [2.27745447, 2.45501118]]), 0.8168475927077372)
>>> 
>>> random.seed((500,1500))
>>> codes = 4
>>> kmeans(whitened,codes)
(array([[0.89644484, 1.76379444],
       [1.45369434, 0.07150518],
       [2.65299217, 2.82445461],
       [0.16959767, 0.52437132]]), 0.39112017893552065)
>>> # Tạo 60 điểm dữ liệu trong 2 cụm a và b
>>> pts = 60
>>> a = np.random.multivariate_normal([0, 0], [[4, 1], [1, 4]], size=pts)
>>> b = np.random.multivariate_normal([20, 10], [[14, 2], [2, 1]], size=pts)
>>> features = np.concatenate((a, b))
>>> # Whiten dữ liệu
>>> whitened = whiten(features)
>>> # Tìm 2 cụm dữ liệu
>>> codebook, distortion = kmeans(whitened, 2)
>>> # Vẽ Whiten dữ liệu và tâm cụm (màu đỏ)
>>> plt.scatter(whitened[:, 0], whitened[:, 1])

>>> plt.scatter(codebook[:, 0], codebook[:, 1], c='r')

>>> plt.show()
>>># hàm k-mean2
>>> from scipy.cluster.vq import kmeans2
>>> # Tạo mảng z có hình dạng (100, 2) chứa hỗn hợp các mẫu từ ba phân phối bình thường đa biến.
>>> np.random.seed(12345678)
>>> a = np.random.multivariate_normal([0, 6], [[2, 1], [1, 2.5]], size=45)
>>> b = np.random.multivariate_normal([1, 0], [[1, -1], [-1, 3]], size=35)
>>> c = np.random.multivariate_normal([6, 5], [[4, 0], [0, 1.]], size=25)
>>> z = np.concatenate((a, b, c))
>>> np.random.shuffle(z)
>>> # Tính toán 3 cụm
>>> centroid, label = kmeans2(z, 3, minit='points')
>>> centroid
array([[ 6.17155178,  4.85018346],
       [ 0.89380981, -0.14430193],
       [ 0.03293515,  6.16629408]])
>>> # Số điểm trong mỗi cụm
>>> counts = np.bincount(label)
>>> counts
array([24, 35, 46], dtype=int64)
>>> # Vẽ các cụm
>>> w0 = z[label == 0]
>>> w1 = z[label == 1]
>>> w2 = z[label == 2]
>>> plt.plot(w0[:, 0], w0[:, 1], 'o', alpha=0.5, label='cụm 1')
[]
>>> plt.plot(w1[:, 0], w1[:, 1], 's', alpha=0.5, label='cụm 2')
[]
>>> plt.plot(w2[:, 0], w2[:, 1], 'd', alpha=0.5, label='cụm 3')
[]
>>> plt.plot(centroid[:, 0], centroid[:, 1], 'ro', label='tâm')
[]
>>> plt.axis('equal')
(-3.2328087766702582, 8.417272880962955, -3.9724381733346927, 10.306803368482795)
>>> plt.legend(shadow=True)

>>> plt.show()
4)

Các thuật toán phân cụm rất hữu ích trong lý thuyết thông tin, phát hiện mục tiêu, truyền thông, nén và các lĩnh vực khác.

>>> # hàm whiten()- Chuẩn hóa một nhóm quan sát trên cơ sở từng tính năng.
>>> import numpy as np
>>> from scipy.cluster.vq import whiten
>>> features  = np.array([[2.9, 3.3, 4.7], [7.5, 6.5, 7.2],[0.8, 0.1, 0.7,]])
>>> whiten(features)
array([[1.03644899, 1.26301815, 1.75565531],
       [2.68047153, 2.48776302, 2.68951452],
       [0.28591696, 0.03827328, 0.26148058]])
>>> # hàm vq - Gán mã từ một cuốn sách mã cho các quan sát.
>>> from scipy.cluster.vq import vq
>>> code_book = np.array([[1.,1.,1.,],[2.,1.,1.]])
>>> vq(features, code_book)
(array([1, 1, 0]), array([4.44859528, 9.9468588 , 0.96953597]))
>>> # hàm kmeans - Thực hiện k-mean trên một tập các vectơ quan sát tạo thành các cụm k
>>> from scipy.cluster.vq import vq, kmeans, whiten
>>> import matplotlib.pyplot as plt
>>> features = np.array([[],])
>>> from numpy import random
>>> features = np.array([[ 1.3,2.2],
                     [ 2.1,2.4],
                     [ 0.4,0.6],
                     [ 0.3,2.8],
                     [ 0.1,0.2],
                     [ 0.2,1.4],
                     [ 2.0,0.1],
                     [ 4.3,3.9],
                     [ 3.0,4.0]])
>>> whitened = whiten(features)
>>> book = np.array((whitened[0],whitened[1]))
>>> kmeans(whitened,book)
(array([[0.52090714, 0.86997969],
       [2.27745447, 2.45501118]]), 0.8168475927077372)
>>> 
>>> random.seed((500,1500))
>>> codes = 4
>>> kmeans(whitened,codes)
(array([[0.89644484, 1.76379444],
       [1.45369434, 0.07150518],
       [2.65299217, 2.82445461],
       [0.16959767, 0.52437132]]), 0.39112017893552065)
>>> # Tạo 60 điểm dữ liệu trong 2 cụm a và b
>>> pts = 60
>>> a = np.random.multivariate_normal([0, 0], [[4, 1], [1, 4]], size=pts)
>>> b = np.random.multivariate_normal([20, 10], [[14, 2], [2, 1]], size=pts)
>>> features = np.concatenate((a, b))
>>> # Whiten dữ liệu
>>> whitened = whiten(features)
>>> # Tìm 2 cụm dữ liệu
>>> codebook, distortion = kmeans(whitened, 2)
>>> # Vẽ Whiten dữ liệu và tâm cụm (màu đỏ)
>>> plt.scatter(whitened[:, 0], whitened[:, 1])

>>> plt.scatter(codebook[:, 0], codebook[:, 1], c='r')

>>> plt.show()
>>># hàm k-mean2
>>> from scipy.cluster.vq import kmeans2
>>> # Tạo mảng z có hình dạng (100, 2) chứa hỗn hợp các mẫu từ ba phân phối bình thường đa biến.
>>> np.random.seed(12345678)
>>> a = np.random.multivariate_normal([0, 6], [[2, 1], [1, 2.5]], size=45)
>>> b = np.random.multivariate_normal([1, 0], [[1, -1], [-1, 3]], size=35)
>>> c = np.random.multivariate_normal([6, 5], [[4, 0], [0, 1.]], size=25)
>>> z = np.concatenate((a, b, c))
>>> np.random.shuffle(z)
>>> # Tính toán 3 cụm
>>> centroid, label = kmeans2(z, 3, minit='points')
>>> centroid
array([[ 6.17155178,  4.85018346],
       [ 0.89380981, -0.14430193],
       [ 0.03293515,  6.16629408]])
>>> # Số điểm trong mỗi cụm
>>> counts = np.bincount(label)
>>> counts
array([24, 35, 46], dtype=int64)
>>> # Vẽ các cụm
>>> w0 = z[label == 0]
>>> w1 = z[label == 1]
>>> w2 = z[label == 2]
>>> plt.plot(w0[:, 0], w0[:, 1], 'o', alpha=0.5, label='cụm 1')
[]
>>> plt.plot(w1[:, 0], w1[:, 1], 's', alpha=0.5, label='cụm 2')
[]
>>> plt.plot(w2[:, 0], w2[:, 1], 'd', alpha=0.5, label='cụm 3')
[]
>>> plt.plot(centroid[:, 0], centroid[:, 1], 'ro', label='tâm')
[]
>>> plt.axis('equal')
(-3.2328087766702582, 8.417272880962955, -3.9724381733346927, 10.306803368482795)
>>> plt.legend(shadow=True)

>>> plt.show()
4 bao gồm
>>> # hàm whiten()- Chuẩn hóa một nhóm quan sát trên cơ sở từng tính năng.
>>> import numpy as np
>>> from scipy.cluster.vq import whiten
>>> features  = np.array([[2.9, 3.3, 4.7], [7.5, 6.5, 7.2],[0.8, 0.1, 0.7,]])
>>> whiten(features)
array([[1.03644899, 1.26301815, 1.75565531],
       [2.68047153, 2.48776302, 2.68951452],
       [0.28591696, 0.03827328, 0.26148058]])
>>> # hàm vq - Gán mã từ một cuốn sách mã cho các quan sát.
>>> from scipy.cluster.vq import vq
>>> code_book = np.array([[1.,1.,1.,],[2.,1.,1.]])
>>> vq(features, code_book)
(array([1, 1, 0]), array([4.44859528, 9.9468588 , 0.96953597]))
>>> # hàm kmeans - Thực hiện k-mean trên một tập các vectơ quan sát tạo thành các cụm k
>>> from scipy.cluster.vq import vq, kmeans, whiten
>>> import matplotlib.pyplot as plt
>>> features = np.array([[],])
>>> from numpy import random
>>> features = np.array([[ 1.3,2.2],
                     [ 2.1,2.4],
                     [ 0.4,0.6],
                     [ 0.3,2.8],
                     [ 0.1,0.2],
                     [ 0.2,1.4],
                     [ 2.0,0.1],
                     [ 4.3,3.9],
                     [ 3.0,4.0]])
>>> whitened = whiten(features)
>>> book = np.array((whitened[0],whitened[1]))
>>> kmeans(whitened,book)
(array([[0.52090714, 0.86997969],
       [2.27745447, 2.45501118]]), 0.8168475927077372)
>>> 
>>> random.seed((500,1500))
>>> codes = 4
>>> kmeans(whitened,codes)
(array([[0.89644484, 1.76379444],
       [1.45369434, 0.07150518],
       [2.65299217, 2.82445461],
       [0.16959767, 0.52437132]]), 0.39112017893552065)
>>> # Tạo 60 điểm dữ liệu trong 2 cụm a và b
>>> pts = 60
>>> a = np.random.multivariate_normal([0, 0], [[4, 1], [1, 4]], size=pts)
>>> b = np.random.multivariate_normal([20, 10], [[14, 2], [2, 1]], size=pts)
>>> features = np.concatenate((a, b))
>>> # Whiten dữ liệu
>>> whitened = whiten(features)
>>> # Tìm 2 cụm dữ liệu
>>> codebook, distortion = kmeans(whitened, 2)
>>> # Vẽ Whiten dữ liệu và tâm cụm (màu đỏ)
>>> plt.scatter(whitened[:, 0], whitened[:, 1])

>>> plt.scatter(codebook[:, 0], codebook[:, 1], c='r')

>>> plt.show()
>>># hàm k-mean2
>>> from scipy.cluster.vq import kmeans2
>>> # Tạo mảng z có hình dạng (100, 2) chứa hỗn hợp các mẫu từ ba phân phối bình thường đa biến.
>>> np.random.seed(12345678)
>>> a = np.random.multivariate_normal([0, 6], [[2, 1], [1, 2.5]], size=45)
>>> b = np.random.multivariate_normal([1, 0], [[1, -1], [-1, 3]], size=35)
>>> c = np.random.multivariate_normal([6, 5], [[4, 0], [0, 1.]], size=25)
>>> z = np.concatenate((a, b, c))
>>> np.random.shuffle(z)
>>> # Tính toán 3 cụm
>>> centroid, label = kmeans2(z, 3, minit='points')
>>> centroid
array([[ 6.17155178,  4.85018346],
       [ 0.89380981, -0.14430193],
       [ 0.03293515,  6.16629408]])
>>> # Số điểm trong mỗi cụm
>>> counts = np.bincount(label)
>>> counts
array([24, 35, 46], dtype=int64)
>>> # Vẽ các cụm
>>> w0 = z[label == 0]
>>> w1 = z[label == 1]
>>> w2 = z[label == 2]
>>> plt.plot(w0[:, 0], w0[:, 1], 'o', alpha=0.5, label='cụm 1')
[]
>>> plt.plot(w1[:, 0], w1[:, 1], 's', alpha=0.5, label='cụm 2')
[]
>>> plt.plot(w2[:, 0], w2[:, 1], 'd', alpha=0.5, label='cụm 3')
[]
>>> plt.plot(centroid[:, 0], centroid[:, 1], 'ro', label='tâm')
[]
>>> plt.axis('equal')
(-3.2328087766702582, 8.417272880962955, -3.9724381733346927, 10.306803368482795)
>>> plt.legend(shadow=True)

>>> plt.show()
6 và 
>>> # hàm whiten()- Chuẩn hóa một nhóm quan sát trên cơ sở từng tính năng.
>>> import numpy as np
>>> from scipy.cluster.vq import whiten
>>> features  = np.array([[2.9, 3.3, 4.7], [7.5, 6.5, 7.2],[0.8, 0.1, 0.7,]])
>>> whiten(features)
array([[1.03644899, 1.26301815, 1.75565531],
       [2.68047153, 2.48776302, 2.68951452],
       [0.28591696, 0.03827328, 0.26148058]])
>>> # hàm vq - Gán mã từ một cuốn sách mã cho các quan sát.
>>> from scipy.cluster.vq import vq
>>> code_book = np.array([[1.,1.,1.,],[2.,1.,1.]])
>>> vq(features, code_book)
(array([1, 1, 0]), array([4.44859528, 9.9468588 , 0.96953597]))
>>> # hàm kmeans - Thực hiện k-mean trên một tập các vectơ quan sát tạo thành các cụm k
>>> from scipy.cluster.vq import vq, kmeans, whiten
>>> import matplotlib.pyplot as plt
>>> features = np.array([[],])
>>> from numpy import random
>>> features = np.array([[ 1.3,2.2],
                     [ 2.1,2.4],
                     [ 0.4,0.6],
                     [ 0.3,2.8],
                     [ 0.1,0.2],
                     [ 0.2,1.4],
                     [ 2.0,0.1],
                     [ 4.3,3.9],
                     [ 3.0,4.0]])
>>> whitened = whiten(features)
>>> book = np.array((whitened[0],whitened[1]))
>>> kmeans(whitened,book)
(array([[0.52090714, 0.86997969],
       [2.27745447, 2.45501118]]), 0.8168475927077372)
>>> 
>>> random.seed((500,1500))
>>> codes = 4
>>> kmeans(whitened,codes)
(array([[0.89644484, 1.76379444],
       [1.45369434, 0.07150518],
       [2.65299217, 2.82445461],
       [0.16959767, 0.52437132]]), 0.39112017893552065)
>>> # Tạo 60 điểm dữ liệu trong 2 cụm a và b
>>> pts = 60
>>> a = np.random.multivariate_normal([0, 0], [[4, 1], [1, 4]], size=pts)
>>> b = np.random.multivariate_normal([20, 10], [[14, 2], [2, 1]], size=pts)
>>> features = np.concatenate((a, b))
>>> # Whiten dữ liệu
>>> whitened = whiten(features)
>>> # Tìm 2 cụm dữ liệu
>>> codebook, distortion = kmeans(whitened, 2)
>>> # Vẽ Whiten dữ liệu và tâm cụm (màu đỏ)
>>> plt.scatter(whitened[:, 0], whitened[:, 1])

>>> plt.scatter(codebook[:, 0], codebook[:, 1], c='r')

>>> plt.show()
>>># hàm k-mean2
>>> from scipy.cluster.vq import kmeans2
>>> # Tạo mảng z có hình dạng (100, 2) chứa hỗn hợp các mẫu từ ba phân phối bình thường đa biến.
>>> np.random.seed(12345678)
>>> a = np.random.multivariate_normal([0, 6], [[2, 1], [1, 2.5]], size=45)
>>> b = np.random.multivariate_normal([1, 0], [[1, -1], [-1, 3]], size=35)
>>> c = np.random.multivariate_normal([6, 5], [[4, 0], [0, 1.]], size=25)
>>> z = np.concatenate((a, b, c))
>>> np.random.shuffle(z)
>>> # Tính toán 3 cụm
>>> centroid, label = kmeans2(z, 3, minit='points')
>>> centroid
array([[ 6.17155178,  4.85018346],
       [ 0.89380981, -0.14430193],
       [ 0.03293515,  6.16629408]])
>>> # Số điểm trong mỗi cụm
>>> counts = np.bincount(label)
>>> counts
array([24, 35, 46], dtype=int64)
>>> # Vẽ các cụm
>>> w0 = z[label == 0]
>>> w1 = z[label == 1]
>>> w2 = z[label == 2]
>>> plt.plot(w0[:, 0], w0[:, 1], 'o', alpha=0.5, label='cụm 1')
[]
>>> plt.plot(w1[:, 0], w1[:, 1], 's', alpha=0.5, label='cụm 2')
[]
>>> plt.plot(w2[:, 0], w2[:, 1], 'd', alpha=0.5, label='cụm 3')
[]
>>> plt.plot(centroid[:, 0], centroid[:, 1], 'ro', label='tâm')
[]
>>> plt.axis('equal')
(-3.2328087766702582, 8.417272880962955, -3.9724381733346927, 10.306803368482795)
>>> plt.legend(shadow=True)

>>> plt.show()
7

1. 1. Phân cụm K-mean và lượng tử hóa vestor (

>>> # hàm whiten()- Chuẩn hóa một nhóm quan sát trên cơ sở từng tính năng.
>>> import numpy as np
>>> from scipy.cluster.vq import whiten
>>> features  = np.array([[2.9, 3.3, 4.7], [7.5, 6.5, 7.2],[0.8, 0.1, 0.7,]])
>>> whiten(features)
array([[1.03644899, 1.26301815, 1.75565531],
       [2.68047153, 2.48776302, 2.68951452],
       [0.28591696, 0.03827328, 0.26148058]])
>>> # hàm vq - Gán mã từ một cuốn sách mã cho các quan sát.
>>> from scipy.cluster.vq import vq
>>> code_book = np.array([[1.,1.,1.,],[2.,1.,1.]])
>>> vq(features, code_book)
(array([1, 1, 0]), array([4.44859528, 9.9468588 , 0.96953597]))
>>> # hàm kmeans - Thực hiện k-mean trên một tập các vectơ quan sát tạo thành các cụm k
>>> from scipy.cluster.vq import vq, kmeans, whiten
>>> import matplotlib.pyplot as plt
>>> features = np.array([[],])
>>> from numpy import random
>>> features = np.array([[ 1.3,2.2],
                     [ 2.1,2.4],
                     [ 0.4,0.6],
                     [ 0.3,2.8],
                     [ 0.1,0.2],
                     [ 0.2,1.4],
                     [ 2.0,0.1],
                     [ 4.3,3.9],
                     [ 3.0,4.0]])
>>> whitened = whiten(features)
>>> book = np.array((whitened[0],whitened[1]))
>>> kmeans(whitened,book)
(array([[0.52090714, 0.86997969],
       [2.27745447, 2.45501118]]), 0.8168475927077372)
>>> 
>>> random.seed((500,1500))
>>> codes = 4
>>> kmeans(whitened,codes)
(array([[0.89644484, 1.76379444],
       [1.45369434, 0.07150518],
       [2.65299217, 2.82445461],
       [0.16959767, 0.52437132]]), 0.39112017893552065)
>>> # Tạo 60 điểm dữ liệu trong 2 cụm a và b
>>> pts = 60
>>> a = np.random.multivariate_normal([0, 0], [[4, 1], [1, 4]], size=pts)
>>> b = np.random.multivariate_normal([20, 10], [[14, 2], [2, 1]], size=pts)
>>> features = np.concatenate((a, b))
>>> # Whiten dữ liệu
>>> whitened = whiten(features)
>>> # Tìm 2 cụm dữ liệu
>>> codebook, distortion = kmeans(whitened, 2)
>>> # Vẽ Whiten dữ liệu và tâm cụm (màu đỏ)
>>> plt.scatter(whitened[:, 0], whitened[:, 1])

>>> plt.scatter(codebook[:, 0], codebook[:, 1], c='r')

>>> plt.show()
>>># hàm k-mean2
>>> from scipy.cluster.vq import kmeans2
>>> # Tạo mảng z có hình dạng (100, 2) chứa hỗn hợp các mẫu từ ba phân phối bình thường đa biến.
>>> np.random.seed(12345678)
>>> a = np.random.multivariate_normal([0, 6], [[2, 1], [1, 2.5]], size=45)
>>> b = np.random.multivariate_normal([1, 0], [[1, -1], [-1, 3]], size=35)
>>> c = np.random.multivariate_normal([6, 5], [[4, 0], [0, 1.]], size=25)
>>> z = np.concatenate((a, b, c))
>>> np.random.shuffle(z)
>>> # Tính toán 3 cụm
>>> centroid, label = kmeans2(z, 3, minit='points')
>>> centroid
array([[ 6.17155178,  4.85018346],
       [ 0.89380981, -0.14430193],
       [ 0.03293515,  6.16629408]])
>>> # Số điểm trong mỗi cụm
>>> counts = np.bincount(label)
>>> counts
array([24, 35, 46], dtype=int64)
>>> # Vẽ các cụm
>>> w0 = z[label == 0]
>>> w1 = z[label == 1]
>>> w2 = z[label == 2]
>>> plt.plot(w0[:, 0], w0[:, 1], 'o', alpha=0.5, label='cụm 1')
[]
>>> plt.plot(w1[:, 0], w1[:, 1], 's', alpha=0.5, label='cụm 2')
[]
>>> plt.plot(w2[:, 0], w2[:, 1], 'd', alpha=0.5, label='cụm 3')
[]
>>> plt.plot(centroid[:, 0], centroid[:, 1], 'ro', label='tâm')
[]
>>> plt.axis('equal')
(-3.2328087766702582, 8.417272880962955, -3.9724381733346927, 10.306803368482795)
>>> plt.legend(shadow=True)

>>> plt.show()
6)

Thuật toán k-mean lấy đầu vào số lượng cụm để tạo k và một tập hợp các chức năng quan sát thành cụm. Nó trả về một tập hợp các trọng tâm (centroid), một trọng tâm cho mỗi cụm k. Một quan sát được phân loại với số cụm từ hoặc chỉ số quan trọng của tâm gần nó nhất.  

Một v chú ngữ thuộc cụm từ i nếu nó gần với trọng tâm i hơn bất kỳ trọng tâm nào khác. Nếu v thuộc về tôi, chúng tôi nói trọng tâm i là trọng tâm hệ thống của v. Thuật toán k-mean cố gắng giảm thiểu các dạng biến thể, được định nghĩa là tổng khoảng cách bình phương giữa mỗi màn quan sát và trọng tâm hệ thống của nó. Việc giảm thiểu đạt được bằng cách phân loại lại các quan sát thành các cụm từ và tính toán lại các trọng tâm cho đến khi đạt được một cấu trúc trong đó các trọng tâm được ổn định. Người ta cũng có thể định nghĩa một số lần lặp tối đa

Các hàm hiện tại trong

>>> # hàm whiten()- Chuẩn hóa một nhóm quan sát trên cơ sở từng tính năng.
>>> import numpy as np
>>> from scipy.cluster.vq import whiten
>>> features  = np.array([[2.9, 3.3, 4.7], [7.5, 6.5, 7.2],[0.8, 0.1, 0.7,]])
>>> whiten(features)
array([[1.03644899, 1.26301815, 1.75565531],
       [2.68047153, 2.48776302, 2.68951452],
       [0.28591696, 0.03827328, 0.26148058]])
>>> # hàm vq - Gán mã từ một cuốn sách mã cho các quan sát.
>>> from scipy.cluster.vq import vq
>>> code_book = np.array([[1.,1.,1.,],[2.,1.,1.]])
>>> vq(features, code_book)
(array([1, 1, 0]), array([4.44859528, 9.9468588 , 0.96953597]))
>>> # hàm kmeans - Thực hiện k-mean trên một tập các vectơ quan sát tạo thành các cụm k
>>> from scipy.cluster.vq import vq, kmeans, whiten
>>> import matplotlib.pyplot as plt
>>> features = np.array([[],])
>>> from numpy import random
>>> features = np.array([[ 1.3,2.2],
                     [ 2.1,2.4],
                     [ 0.4,0.6],
                     [ 0.3,2.8],
                     [ 0.1,0.2],
                     [ 0.2,1.4],
                     [ 2.0,0.1],
                     [ 4.3,3.9],
                     [ 3.0,4.0]])
>>> whitened = whiten(features)
>>> book = np.array((whitened[0],whitened[1]))
>>> kmeans(whitened,book)
(array([[0.52090714, 0.86997969],
       [2.27745447, 2.45501118]]), 0.8168475927077372)
>>> 
>>> random.seed((500,1500))
>>> codes = 4
>>> kmeans(whitened,codes)
(array([[0.89644484, 1.76379444],
       [1.45369434, 0.07150518],
       [2.65299217, 2.82445461],
       [0.16959767, 0.52437132]]), 0.39112017893552065)
>>> # Tạo 60 điểm dữ liệu trong 2 cụm a và b
>>> pts = 60
>>> a = np.random.multivariate_normal([0, 0], [[4, 1], [1, 4]], size=pts)
>>> b = np.random.multivariate_normal([20, 10], [[14, 2], [2, 1]], size=pts)
>>> features = np.concatenate((a, b))
>>> # Whiten dữ liệu
>>> whitened = whiten(features)
>>> # Tìm 2 cụm dữ liệu
>>> codebook, distortion = kmeans(whitened, 2)
>>> # Vẽ Whiten dữ liệu và tâm cụm (màu đỏ)
>>> plt.scatter(whitened[:, 0], whitened[:, 1])

>>> plt.scatter(codebook[:, 0], codebook[:, 1], c='r')

>>> plt.show()
>>># hàm k-mean2
>>> from scipy.cluster.vq import kmeans2
>>> # Tạo mảng z có hình dạng (100, 2) chứa hỗn hợp các mẫu từ ba phân phối bình thường đa biến.
>>> np.random.seed(12345678)
>>> a = np.random.multivariate_normal([0, 6], [[2, 1], [1, 2.5]], size=45)
>>> b = np.random.multivariate_normal([1, 0], [[1, -1], [-1, 3]], size=35)
>>> c = np.random.multivariate_normal([6, 5], [[4, 0], [0, 1.]], size=25)
>>> z = np.concatenate((a, b, c))
>>> np.random.shuffle(z)
>>> # Tính toán 3 cụm
>>> centroid, label = kmeans2(z, 3, minit='points')
>>> centroid
array([[ 6.17155178,  4.85018346],
       [ 0.89380981, -0.14430193],
       [ 0.03293515,  6.16629408]])
>>> # Số điểm trong mỗi cụm
>>> counts = np.bincount(label)
>>> counts
array([24, 35, 46], dtype=int64)
>>> # Vẽ các cụm
>>> w0 = z[label == 0]
>>> w1 = z[label == 1]
>>> w2 = z[label == 2]
>>> plt.plot(w0[:, 0], w0[:, 1], 'o', alpha=0.5, label='cụm 1')
[]
>>> plt.plot(w1[:, 0], w1[:, 1], 's', alpha=0.5, label='cụm 2')
[]
>>> plt.plot(w2[:, 0], w2[:, 1], 'd', alpha=0.5, label='cụm 3')
[]
>>> plt.plot(centroid[:, 0], centroid[:, 1], 'ro', label='tâm')
[]
>>> plt.axis('equal')
(-3.2328087766702582, 8.417272880962955, -3.9724381733346927, 10.306803368482795)
>>> plt.legend(shadow=True)

>>> plt.show()
6 là 
>>># hàm fcluster: Hình thành các cụm phẳng từ cụm phân cấp được xác định bởi ma trận liên kết đã cho.
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [0, 3], [1, 4],
         [4, 0], [3, 0], [4, 1],
         [4, 4], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.9, criterion='distance')
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)
>>>
>>># hàm cophenet(Z[, Y]): Tính khoảng cách cophenetic giữa mỗi lần quan sát trong cụm phân cấp được xác định bởi liên kết Z.
>>> from scipy.cluster.hierarchy import single, cophenet
>>> from scipy.spatial.distance import pdist, squareform
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [1, 1], [1, 4],
         [4, 1], [2, 0], [4, 2],
         [4, 4], [3, 3], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = single(pdist(X))
>>> cophenet(Z)
array([1.        , 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 1.        , 1.        ,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 1.        , 1.        , 1.        , 1.        ,
       1.        ])
>>># Dùng hàm squareform() để output dưới dạng ma trận thông thường
>>> squareform(cophenet(Z))
array([[0.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 0.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 0.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 0.        , 2.23606798,
        1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 1.        , 2.23606798, 0.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 1.        , 2.23606798,
        0.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 0.        , 2.23606798, 1.        , 1.        ,
        1.        , 1.        ],
       [1.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 0.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 0.        , 1.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 0.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        0.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        1.        , 0.        ]])
>>># hàm fcluster - gán các điểm dữ liệu gốc cho các cụm đơn. Việc gán này chủ yếu phụ thuộc vào ngưỡng khoảng cách t - khoảng cách giữa các cụm tối đa được phép
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 1], [0, 1], [1, 0],
         [1, 1], [0, 3], [1, 1],
         [2, 3], [3, 0], [4, 1],
         [2, 2], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.6, criterion='distance')
array([ 1,  1,  3,  2, 10,  2,  8,  4,  5,  9,  6,  7], dtype=int32)
>>> 
0, 
>>># hàm fcluster: Hình thành các cụm phẳng từ cụm phân cấp được xác định bởi ma trận liên kết đã cho.
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [0, 3], [1, 4],
         [4, 0], [3, 0], [4, 1],
         [4, 4], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.9, criterion='distance')
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)
>>>
>>># hàm cophenet(Z[, Y]): Tính khoảng cách cophenetic giữa mỗi lần quan sát trong cụm phân cấp được xác định bởi liên kết Z.
>>> from scipy.cluster.hierarchy import single, cophenet
>>> from scipy.spatial.distance import pdist, squareform
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [1, 1], [1, 4],
         [4, 1], [2, 0], [4, 2],
         [4, 4], [3, 3], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = single(pdist(X))
>>> cophenet(Z)
array([1.        , 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 1.        , 1.        ,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 1.        , 1.        , 1.        , 1.        ,
       1.        ])
>>># Dùng hàm squareform() để output dưới dạng ma trận thông thường
>>> squareform(cophenet(Z))
array([[0.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 0.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 0.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 0.        , 2.23606798,
        1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 1.        , 2.23606798, 0.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 1.        , 2.23606798,
        0.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 0.        , 2.23606798, 1.        , 1.        ,
        1.        , 1.        ],
       [1.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 0.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 0.        , 1.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 0.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        0.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        1.        , 0.        ]])
>>># hàm fcluster - gán các điểm dữ liệu gốc cho các cụm đơn. Việc gán này chủ yếu phụ thuộc vào ngưỡng khoảng cách t - khoảng cách giữa các cụm tối đa được phép
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 1], [0, 1], [1, 0],
         [1, 1], [0, 3], [1, 1],
         [2, 3], [3, 0], [4, 1],
         [2, 2], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.6, criterion='distance')
array([ 1,  1,  3,  2, 10,  2,  8,  4,  5,  9,  6,  7], dtype=int32)
>>> 
1(), 
>>># hàm fcluster: Hình thành các cụm phẳng từ cụm phân cấp được xác định bởi ma trận liên kết đã cho.
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [0, 3], [1, 4],
         [4, 0], [3, 0], [4, 1],
         [4, 4], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.9, criterion='distance')
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)
>>>
>>># hàm cophenet(Z[, Y]): Tính khoảng cách cophenetic giữa mỗi lần quan sát trong cụm phân cấp được xác định bởi liên kết Z.
>>> from scipy.cluster.hierarchy import single, cophenet
>>> from scipy.spatial.distance import pdist, squareform
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [1, 1], [1, 4],
         [4, 1], [2, 0], [4, 2],
         [4, 4], [3, 3], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = single(pdist(X))
>>> cophenet(Z)
array([1.        , 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 1.        , 1.        ,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 1.        , 1.        , 1.        , 1.        ,
       1.        ])
>>># Dùng hàm squareform() để output dưới dạng ma trận thông thường
>>> squareform(cophenet(Z))
array([[0.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 0.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 0.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 0.        , 2.23606798,
        1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 1.        , 2.23606798, 0.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 1.        , 2.23606798,
        0.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 0.        , 2.23606798, 1.        , 1.        ,
        1.        , 1.        ],
       [1.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 0.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 0.        , 1.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 0.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        0.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        1.        , 0.        ]])
>>># hàm fcluster - gán các điểm dữ liệu gốc cho các cụm đơn. Việc gán này chủ yếu phụ thuộc vào ngưỡng khoảng cách t - khoảng cách giữa các cụm tối đa được phép
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 1], [0, 1], [1, 0],
         [1, 1], [0, 3], [1, 1],
         [2, 3], [3, 0], [4, 1],
         [2, 2], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.6, criterion='distance')
array([ 1,  1,  3,  2, 10,  2,  8,  4,  5,  9,  6,  7], dtype=int32)
>>> 
2() và 
>>># hàm fcluster: Hình thành các cụm phẳng từ cụm phân cấp được xác định bởi ma trận liên kết đã cho.
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [0, 3], [1, 4],
         [4, 0], [3, 0], [4, 1],
         [4, 4], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.9, criterion='distance')
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)
>>>
>>># hàm cophenet(Z[, Y]): Tính khoảng cách cophenetic giữa mỗi lần quan sát trong cụm phân cấp được xác định bởi liên kết Z.
>>> from scipy.cluster.hierarchy import single, cophenet
>>> from scipy.spatial.distance import pdist, squareform
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [1, 1], [1, 4],
         [4, 1], [2, 0], [4, 2],
         [4, 4], [3, 3], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = single(pdist(X))
>>> cophenet(Z)
array([1.        , 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 1.        , 1.        ,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 1.        , 1.        , 1.        , 1.        ,
       1.        ])
>>># Dùng hàm squareform() để output dưới dạng ma trận thông thường
>>> squareform(cophenet(Z))
array([[0.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 0.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 0.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 0.        , 2.23606798,
        1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 1.        , 2.23606798, 0.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 1.        , 2.23606798,
        0.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 0.        , 2.23606798, 1.        , 1.        ,
        1.        , 1.        ],
       [1.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 0.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 0.        , 1.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 0.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        0.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        1.        , 0.        ]])
>>># hàm fcluster - gán các điểm dữ liệu gốc cho các cụm đơn. Việc gán này chủ yếu phụ thuộc vào ngưỡng khoảng cách t - khoảng cách giữa các cụm tối đa được phép
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 1], [0, 1], [1, 0],
         [1, 1], [0, 3], [1, 1],
         [2, 3], [3, 0], [4, 1],
         [2, 2], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.6, criterion='distance')
array([ 1,  1,  3,  2, 10,  2,  8,  4,  5,  9,  6,  7], dtype=int32)
>>> 
3(). Bên dưới là một số ví dụ cho mô-đun này

Ví dụ. Các hàm trong 

>>> # hàm whiten()- Chuẩn hóa một nhóm quan sát trên cơ sở từng tính năng.
>>> import numpy as np
>>> from scipy.cluster.vq import whiten
>>> features  = np.array([[2.9, 3.3, 4.7], [7.5, 6.5, 7.2],[0.8, 0.1, 0.7,]])
>>> whiten(features)
array([[1.03644899, 1.26301815, 1.75565531],
       [2.68047153, 2.48776302, 2.68951452],
       [0.28591696, 0.03827328, 0.26148058]])
>>> # hàm vq - Gán mã từ một cuốn sách mã cho các quan sát.
>>> from scipy.cluster.vq import vq
>>> code_book = np.array([[1.,1.,1.,],[2.,1.,1.]])
>>> vq(features, code_book)
(array([1, 1, 0]), array([4.44859528, 9.9468588 , 0.96953597]))
>>> # hàm kmeans - Thực hiện k-mean trên một tập các vectơ quan sát tạo thành các cụm k
>>> from scipy.cluster.vq import vq, kmeans, whiten
>>> import matplotlib.pyplot as plt
>>> features = np.array([[],])
>>> from numpy import random
>>> features = np.array([[ 1.3,2.2],
                     [ 2.1,2.4],
                     [ 0.4,0.6],
                     [ 0.3,2.8],
                     [ 0.1,0.2],
                     [ 0.2,1.4],
                     [ 2.0,0.1],
                     [ 4.3,3.9],
                     [ 3.0,4.0]])
>>> whitened = whiten(features)
>>> book = np.array((whitened[0],whitened[1]))
>>> kmeans(whitened,book)
(array([[0.52090714, 0.86997969],
       [2.27745447, 2.45501118]]), 0.8168475927077372)
>>> 
>>> random.seed((500,1500))
>>> codes = 4
>>> kmeans(whitened,codes)
(array([[0.89644484, 1.76379444],
       [1.45369434, 0.07150518],
       [2.65299217, 2.82445461],
       [0.16959767, 0.52437132]]), 0.39112017893552065)
>>> # Tạo 60 điểm dữ liệu trong 2 cụm a và b
>>> pts = 60
>>> a = np.random.multivariate_normal([0, 0], [[4, 1], [1, 4]], size=pts)
>>> b = np.random.multivariate_normal([20, 10], [[14, 2], [2, 1]], size=pts)
>>> features = np.concatenate((a, b))
>>> # Whiten dữ liệu
>>> whitened = whiten(features)
>>> # Tìm 2 cụm dữ liệu
>>> codebook, distortion = kmeans(whitened, 2)
>>> # Vẽ Whiten dữ liệu và tâm cụm (màu đỏ)
>>> plt.scatter(whitened[:, 0], whitened[:, 1])

>>> plt.scatter(codebook[:, 0], codebook[:, 1], c='r')

>>> plt.show()
>>># hàm k-mean2
>>> from scipy.cluster.vq import kmeans2
>>> # Tạo mảng z có hình dạng (100, 2) chứa hỗn hợp các mẫu từ ba phân phối bình thường đa biến.
>>> np.random.seed(12345678)
>>> a = np.random.multivariate_normal([0, 6], [[2, 1], [1, 2.5]], size=45)
>>> b = np.random.multivariate_normal([1, 0], [[1, -1], [-1, 3]], size=35)
>>> c = np.random.multivariate_normal([6, 5], [[4, 0], [0, 1.]], size=25)
>>> z = np.concatenate((a, b, c))
>>> np.random.shuffle(z)
>>> # Tính toán 3 cụm
>>> centroid, label = kmeans2(z, 3, minit='points')
>>> centroid
array([[ 6.17155178,  4.85018346],
       [ 0.89380981, -0.14430193],
       [ 0.03293515,  6.16629408]])
>>> # Số điểm trong mỗi cụm
>>> counts = np.bincount(label)
>>> counts
array([24, 35, 46], dtype=int64)
>>> # Vẽ các cụm
>>> w0 = z[label == 0]
>>> w1 = z[label == 1]
>>> w2 = z[label == 2]
>>> plt.plot(w0[:, 0], w0[:, 1], 'o', alpha=0.5, label='cụm 1')
[]
>>> plt.plot(w1[:, 0], w1[:, 1], 's', alpha=0.5, label='cụm 2')
[]
>>> plt.plot(w2[:, 0], w2[:, 1], 'd', alpha=0.5, label='cụm 3')
[]
>>> plt.plot(centroid[:, 0], centroid[:, 1], 'ro', label='tâm')
[]
>>> plt.axis('equal')
(-3.2328087766702582, 8.417272880962955, -3.9724381733346927, 10.306803368482795)
>>> plt.legend(shadow=True)

>>> plt.show()
6

>>> # hàm whiten()- Chuẩn hóa một nhóm quan sát trên cơ sở từng tính năng.
>>> import numpy as np
>>> from scipy.cluster.vq import whiten
>>> features  = np.array([[2.9, 3.3, 4.7], [7.5, 6.5, 7.2],[0.8, 0.1, 0.7,]])
>>> whiten(features)
array([[1.03644899, 1.26301815, 1.75565531],
       [2.68047153, 2.48776302, 2.68951452],
       [0.28591696, 0.03827328, 0.26148058]])
>>> # hàm vq - Gán mã từ một cuốn sách mã cho các quan sát.
>>> from scipy.cluster.vq import vq
>>> code_book = np.array([[1.,1.,1.,],[2.,1.,1.]])
>>> vq(features, code_book)
(array([1, 1, 0]), array([4.44859528, 9.9468588 , 0.96953597]))
>>> # hàm kmeans - Thực hiện k-mean trên một tập các vectơ quan sát tạo thành các cụm k
>>> from scipy.cluster.vq import vq, kmeans, whiten
>>> import matplotlib.pyplot as plt
>>> features = np.array([[],])
>>> from numpy import random
>>> features = np.array([[ 1.3,2.2],
                     [ 2.1,2.4],
                     [ 0.4,0.6],
                     [ 0.3,2.8],
                     [ 0.1,0.2],
                     [ 0.2,1.4],
                     [ 2.0,0.1],
                     [ 4.3,3.9],
                     [ 3.0,4.0]])
>>> whitened = whiten(features)
>>> book = np.array((whitened[0],whitened[1]))
>>> kmeans(whitened,book)
(array([[0.52090714, 0.86997969],
       [2.27745447, 2.45501118]]), 0.8168475927077372)
>>> 
>>> random.seed((500,1500))
>>> codes = 4
>>> kmeans(whitened,codes)
(array([[0.89644484, 1.76379444],
       [1.45369434, 0.07150518],
       [2.65299217, 2.82445461],
       [0.16959767, 0.52437132]]), 0.39112017893552065)
>>> # Tạo 60 điểm dữ liệu trong 2 cụm a và b
>>> pts = 60
>>> a = np.random.multivariate_normal([0, 0], [[4, 1], [1, 4]], size=pts)
>>> b = np.random.multivariate_normal([20, 10], [[14, 2], [2, 1]], size=pts)
>>> features = np.concatenate((a, b))
>>> # Whiten dữ liệu
>>> whitened = whiten(features)
>>> # Tìm 2 cụm dữ liệu
>>> codebook, distortion = kmeans(whitened, 2)
>>> # Vẽ Whiten dữ liệu và tâm cụm (màu đỏ)
>>> plt.scatter(whitened[:, 0], whitened[:, 1])

>>> plt.scatter(codebook[:, 0], codebook[:, 1], c='r')

>>> plt.show()
>>># hàm k-mean2
>>> from scipy.cluster.vq import kmeans2
>>> # Tạo mảng z có hình dạng (100, 2) chứa hỗn hợp các mẫu từ ba phân phối bình thường đa biến.
>>> np.random.seed(12345678)
>>> a = np.random.multivariate_normal([0, 6], [[2, 1], [1, 2.5]], size=45)
>>> b = np.random.multivariate_normal([1, 0], [[1, -1], [-1, 3]], size=35)
>>> c = np.random.multivariate_normal([6, 5], [[4, 0], [0, 1.]], size=25)
>>> z = np.concatenate((a, b, c))
>>> np.random.shuffle(z)
>>> # Tính toán 3 cụm
>>> centroid, label = kmeans2(z, 3, minit='points')
>>> centroid
array([[ 6.17155178,  4.85018346],
       [ 0.89380981, -0.14430193],
       [ 0.03293515,  6.16629408]])
>>> # Số điểm trong mỗi cụm
>>> counts = np.bincount(label)
>>> counts
array([24, 35, 46], dtype=int64)
>>> # Vẽ các cụm
>>> w0 = z[label == 0]
>>> w1 = z[label == 1]
>>> w2 = z[label == 2]
>>> plt.plot(w0[:, 0], w0[:, 1], 'o', alpha=0.5, label='cụm 1')
[]
>>> plt.plot(w1[:, 0], w1[:, 1], 's', alpha=0.5, label='cụm 2')
[]
>>> plt.plot(w2[:, 0], w2[:, 1], 'd', alpha=0.5, label='cụm 3')
[]
>>> plt.plot(centroid[:, 0], centroid[:, 1], 'ro', label='tâm')
[]
>>> plt.axis('equal')
(-3.2328087766702582, 8.417272880962955, -3.9724381733346927, 10.306803368482795)
>>> plt.legend(shadow=True)

>>> plt.show()


Sử dụng python nội suy numpy

Ảnh
>>># hàm fcluster: Hình thành các cụm phẳng từ cụm phân cấp được xác định bởi ma trận liên kết đã cho.
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [0, 3], [1, 4],
         [4, 0], [3, 0], [4, 1],
         [4, 4], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.9, criterion='distance')
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)
>>>
>>># hàm cophenet(Z[, Y]): Tính khoảng cách cophenetic giữa mỗi lần quan sát trong cụm phân cấp được xác định bởi liên kết Z.
>>> from scipy.cluster.hierarchy import single, cophenet
>>> from scipy.spatial.distance import pdist, squareform
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [1, 1], [1, 4],
         [4, 1], [2, 0], [4, 2],
         [4, 4], [3, 3], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = single(pdist(X))
>>> cophenet(Z)
array([1.        , 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 1.        , 1.        ,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 1.        , 1.        , 1.        , 1.        ,
       1.        ])
>>># Dùng hàm squareform() để output dưới dạng ma trận thông thường
>>> squareform(cophenet(Z))
array([[0.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 0.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 0.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 0.        , 2.23606798,
        1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 1.        , 2.23606798, 0.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 1.        , 2.23606798,
        0.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 0.        , 2.23606798, 1.        , 1.        ,
        1.        , 1.        ],
       [1.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 0.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 0.        , 1.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 0.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        0.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        1.        , 0.        ]])
>>># hàm fcluster - gán các điểm dữ liệu gốc cho các cụm đơn. Việc gán này chủ yếu phụ thuộc vào ngưỡng khoảng cách t - khoảng cách giữa các cụm tối đa được phép
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 1], [0, 1], [1, 0],
         [1, 1], [0, 3], [1, 1],
         [2, 3], [3, 0], [4, 1],
         [2, 2], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.6, criterion='distance')
array([ 1,  1,  3,  2, 10,  2,  8,  4,  5,  9,  6,  7], dtype=int32)
>>> 
5
Sử dụng python nội suy numpy

Ảnh
>>># hàm fcluster: Hình thành các cụm phẳng từ cụm phân cấp được xác định bởi ma trận liên kết đã cho.
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [0, 3], [1, 4],
         [4, 0], [3, 0], [4, 1],
         [4, 4], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.9, criterion='distance')
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)
>>>
>>># hàm cophenet(Z[, Y]): Tính khoảng cách cophenetic giữa mỗi lần quan sát trong cụm phân cấp được xác định bởi liên kết Z.
>>> from scipy.cluster.hierarchy import single, cophenet
>>> from scipy.spatial.distance import pdist, squareform
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [1, 1], [1, 4],
         [4, 1], [2, 0], [4, 2],
         [4, 4], [3, 3], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = single(pdist(X))
>>> cophenet(Z)
array([1.        , 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 1.        , 1.        ,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 1.        , 1.        , 1.        , 1.        ,
       1.        ])
>>># Dùng hàm squareform() để output dưới dạng ma trận thông thường
>>> squareform(cophenet(Z))
array([[0.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 0.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 0.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 0.        , 2.23606798,
        1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 1.        , 2.23606798, 0.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 1.        , 2.23606798,
        0.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 0.        , 2.23606798, 1.        , 1.        ,
        1.        , 1.        ],
       [1.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 0.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 0.        , 1.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 0.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        0.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        1.        , 0.        ]])
>>># hàm fcluster - gán các điểm dữ liệu gốc cho các cụm đơn. Việc gán này chủ yếu phụ thuộc vào ngưỡng khoảng cách t - khoảng cách giữa các cụm tối đa được phép
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 1], [0, 1], [1, 0],
         [1, 1], [0, 3], [1, 1],
         [2, 3], [3, 0], [4, 1],
         [2, 2], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.6, criterion='distance')
array([ 1,  1,  3,  2, 10,  2,  8,  4,  5,  9,  6,  7], dtype=int32)
>>> 
6

Các bạn tham khảo thông tin chi tiết cho module-module này tại đây
1. 2. Mô-đun 

>>> # hàm whiten()- Chuẩn hóa một nhóm quan sát trên cơ sở từng tính năng.
>>> import numpy as np
>>> from scipy.cluster.vq import whiten
>>> features  = np.array([[2.9, 3.3, 4.7], [7.5, 6.5, 7.2],[0.8, 0.1, 0.7,]])
>>> whiten(features)
array([[1.03644899, 1.26301815, 1.75565531],
       [2.68047153, 2.48776302, 2.68951452],
       [0.28591696, 0.03827328, 0.26148058]])
>>> # hàm vq - Gán mã từ một cuốn sách mã cho các quan sát.
>>> from scipy.cluster.vq import vq
>>> code_book = np.array([[1.,1.,1.,],[2.,1.,1.]])
>>> vq(features, code_book)
(array([1, 1, 0]), array([4.44859528, 9.9468588 , 0.96953597]))
>>> # hàm kmeans - Thực hiện k-mean trên một tập các vectơ quan sát tạo thành các cụm k
>>> from scipy.cluster.vq import vq, kmeans, whiten
>>> import matplotlib.pyplot as plt
>>> features = np.array([[],])
>>> from numpy import random
>>> features = np.array([[ 1.3,2.2],
                     [ 2.1,2.4],
                     [ 0.4,0.6],
                     [ 0.3,2.8],
                     [ 0.1,0.2],
                     [ 0.2,1.4],
                     [ 2.0,0.1],
                     [ 4.3,3.9],
                     [ 3.0,4.0]])
>>> whitened = whiten(features)
>>> book = np.array((whitened[0],whitened[1]))
>>> kmeans(whitened,book)
(array([[0.52090714, 0.86997969],
       [2.27745447, 2.45501118]]), 0.8168475927077372)
>>> 
>>> random.seed((500,1500))
>>> codes = 4
>>> kmeans(whitened,codes)
(array([[0.89644484, 1.76379444],
       [1.45369434, 0.07150518],
       [2.65299217, 2.82445461],
       [0.16959767, 0.52437132]]), 0.39112017893552065)
>>> # Tạo 60 điểm dữ liệu trong 2 cụm a và b
>>> pts = 60
>>> a = np.random.multivariate_normal([0, 0], [[4, 1], [1, 4]], size=pts)
>>> b = np.random.multivariate_normal([20, 10], [[14, 2], [2, 1]], size=pts)
>>> features = np.concatenate((a, b))
>>> # Whiten dữ liệu
>>> whitened = whiten(features)
>>> # Tìm 2 cụm dữ liệu
>>> codebook, distortion = kmeans(whitened, 2)
>>> # Vẽ Whiten dữ liệu và tâm cụm (màu đỏ)
>>> plt.scatter(whitened[:, 0], whitened[:, 1])

>>> plt.scatter(codebook[:, 0], codebook[:, 1], c='r')

>>> plt.show()
>>># hàm k-mean2
>>> from scipy.cluster.vq import kmeans2
>>> # Tạo mảng z có hình dạng (100, 2) chứa hỗn hợp các mẫu từ ba phân phối bình thường đa biến.
>>> np.random.seed(12345678)
>>> a = np.random.multivariate_normal([0, 6], [[2, 1], [1, 2.5]], size=45)
>>> b = np.random.multivariate_normal([1, 0], [[1, -1], [-1, 3]], size=35)
>>> c = np.random.multivariate_normal([6, 5], [[4, 0], [0, 1.]], size=25)
>>> z = np.concatenate((a, b, c))
>>> np.random.shuffle(z)
>>> # Tính toán 3 cụm
>>> centroid, label = kmeans2(z, 3, minit='points')
>>> centroid
array([[ 6.17155178,  4.85018346],
       [ 0.89380981, -0.14430193],
       [ 0.03293515,  6.16629408]])
>>> # Số điểm trong mỗi cụm
>>> counts = np.bincount(label)
>>> counts
array([24, 35, 46], dtype=int64)
>>> # Vẽ các cụm
>>> w0 = z[label == 0]
>>> w1 = z[label == 1]
>>> w2 = z[label == 2]
>>> plt.plot(w0[:, 0], w0[:, 1], 'o', alpha=0.5, label='cụm 1')
[]
>>> plt.plot(w1[:, 0], w1[:, 1], 's', alpha=0.5, label='cụm 2')
[]
>>> plt.plot(w2[:, 0], w2[:, 1], 'd', alpha=0.5, label='cụm 3')
[]
>>> plt.plot(centroid[:, 0], centroid[:, 1], 'ro', label='tâm')
[]
>>> plt.axis('equal')
(-3.2328087766702582, 8.417272880962955, -3.9724381733346927, 10.306803368482795)
>>> plt.legend(shadow=True)

>>> plt.show()
7

Mô-đun

>>># hàm fcluster: Hình thành các cụm phẳng từ cụm phân cấp được xác định bởi ma trận liên kết đã cho.
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [0, 3], [1, 4],
         [4, 0], [3, 0], [4, 1],
         [4, 4], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.9, criterion='distance')
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)
>>>
>>># hàm cophenet(Z[, Y]): Tính khoảng cách cophenetic giữa mỗi lần quan sát trong cụm phân cấp được xác định bởi liên kết Z.
>>> from scipy.cluster.hierarchy import single, cophenet
>>> from scipy.spatial.distance import pdist, squareform
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [1, 1], [1, 4],
         [4, 1], [2, 0], [4, 2],
         [4, 4], [3, 3], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = single(pdist(X))
>>> cophenet(Z)
array([1.        , 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 1.        , 1.        ,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 1.        , 1.        , 1.        , 1.        ,
       1.        ])
>>># Dùng hàm squareform() để output dưới dạng ma trận thông thường
>>> squareform(cophenet(Z))
array([[0.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 0.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 0.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 0.        , 2.23606798,
        1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 1.        , 2.23606798, 0.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 1.        , 2.23606798,
        0.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 0.        , 2.23606798, 1.        , 1.        ,
        1.        , 1.        ],
       [1.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 0.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 0.        , 1.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 0.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        0.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        1.        , 0.        ]])
>>># hàm fcluster - gán các điểm dữ liệu gốc cho các cụm đơn. Việc gán này chủ yếu phụ thuộc vào ngưỡng khoảng cách t - khoảng cách giữa các cụm tối đa được phép
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 1], [0, 1], [1, 0],
         [1, 1], [0, 3], [1, 1],
         [2, 3], [3, 0], [4, 1],
         [2, 2], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.6, criterion='distance')
array([ 1,  1,  3,  2, 10,  2,  8,  4,  5,  9,  6,  7], dtype=int32)
>>> 
8 cung cấp các chức năng cho cụm phân cấp và kết hợp. Các tính năng của nó bao gồm tạo các cụm phân cấp từ ma trận khoảng cách, tính toán thống kê trên các cụm, cắt liên kết để tạo ra các cụm phức tạp và hình dung các cụm với cây phả hệ. Mô-đun này có nhiều nhóm chức năng

  • Nhóm hàm
    >>># hàm fcluster: Hình thành các cụm phẳng từ cụm phân cấp được xác định bởi ma trận liên kết đã cho.
    >>> from scipy.cluster.hierarchy import ward, fcluster
    >>> from scipy.spatial.distance import pdist
    >>> X = [[0, 0], [0, 1], [1, 0],
             [0, 4], [0, 3], [1, 4],
             [4, 0], [3, 0], [4, 1],
             [4, 4], [3, 4], [4, 3]]
    >>> # Tạo ma trận Z
    >>> Z = ward(pdist(X))
    >>> fcluster(Z, t=0.9, criterion='distance')
    array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)
    >>>
    >>># hàm cophenet(Z[, Y]): Tính khoảng cách cophenetic giữa mỗi lần quan sát trong cụm phân cấp được xác định bởi liên kết Z.
    >>> from scipy.cluster.hierarchy import single, cophenet
    >>> from scipy.spatial.distance import pdist, squareform
    >>> X = [[0, 0], [0, 1], [1, 0],
             [0, 4], [1, 1], [1, 4],
             [4, 1], [2, 0], [4, 2],
             [4, 4], [3, 3], [4, 3]]
    >>> # Tạo ma trận Z
    >>> Z = single(pdist(X))
    >>> cophenet(Z)
    array([1.        , 1.        , 2.23606798, 1.        , 2.23606798,
           2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
           2.23606798, 1.        , 2.23606798, 1.        , 2.23606798,
           2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
           2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
           1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
           2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
           2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
           1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
           2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
           2.23606798, 2.23606798, 1.        , 1.        , 1.        ,
           1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
           1.        , 1.        , 1.        , 1.        , 1.        ,
           1.        ])
    >>># Dùng hàm squareform() để output dưới dạng ma trận thông thường
    >>> squareform(cophenet(Z))
    array([[0.        , 1.        , 1.        , 2.23606798, 1.        ,
            2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
            2.23606798, 2.23606798],
           [1.        , 0.        , 1.        , 2.23606798, 1.        ,
            2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
            2.23606798, 2.23606798],
           [1.        , 1.        , 0.        , 2.23606798, 1.        ,
            2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
            2.23606798, 2.23606798],
           [2.23606798, 2.23606798, 2.23606798, 0.        , 2.23606798,
            1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
            2.23606798, 2.23606798],
           [1.        , 1.        , 1.        , 2.23606798, 0.        ,
            2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
            2.23606798, 2.23606798],
           [2.23606798, 2.23606798, 2.23606798, 1.        , 2.23606798,
            0.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
            2.23606798, 2.23606798],
           [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
            2.23606798, 0.        , 2.23606798, 1.        , 1.        ,
            1.        , 1.        ],
           [1.        , 1.        , 1.        , 2.23606798, 1.        ,
            2.23606798, 2.23606798, 0.        , 2.23606798, 2.23606798,
            2.23606798, 2.23606798],
           [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
            2.23606798, 1.        , 2.23606798, 0.        , 1.        ,
            1.        , 1.        ],
           [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
            2.23606798, 1.        , 2.23606798, 1.        , 0.        ,
            1.        , 1.        ],
           [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
            2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
            0.        , 1.        ],
           [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
            2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
            1.        , 0.        ]])
    >>># hàm fcluster - gán các điểm dữ liệu gốc cho các cụm đơn. Việc gán này chủ yếu phụ thuộc vào ngưỡng khoảng cách t - khoảng cách giữa các cụm tối đa được phép
    >>> from scipy.cluster.hierarchy import ward, fcluster
    >>> from scipy.spatial.distance import pdist
    >>> X = [[0, 1], [0, 1], [1, 0],
             [1, 1], [0, 3], [1, 1],
             [2, 3], [3, 0], [4, 1],
             [2, 2], [3, 4], [4, 3]]
    >>> # Tạo ma trận Z
    >>> Z = ward(pdist(X))
    >>> fcluster(Z, t=0.6, criterion='distance')
    array([ 1,  1,  3,  2, 10,  2,  8,  4,  5,  9,  6,  7], dtype=int32)
    >>> 
    
    9,
    >>> import numpy as np
    >>># import hàm constants
    >>> from scipy import constants
    >>># Lấy giá trị hằng số theo key
    >>> constants.value(u'proton mass')
    1.67262192369e-27
    >>># Lấy đơn vị của hằng số theo key
    >>> constants.unit(u'elementary charge')
    'C'
    >>># Lấy độ chính xác tương đối của hằng số theo key
    >>> constants.precision(u'neutron mass')
    5.671887297281165e-10
    >>># import hàm find và physical_constants
    >>> from scipy.constants import find, physical_constants
    >>># Tìm các key trong từ điển các hằng số vật lý chứa‘boltzmann’?
    >>> find('structure')
    ['fine-structure constant', 'inverse fine-structure constant']
    >>># Lấy thông tin hằng số được gọi là ‘fine-structure constant’:
    >>> physical_constants['fine-structure constant']
    (0.0072973525693, '', 1.1e-12)
    >>> # Chuyển đổi nhiệt độ
    >>> convert_temperature(np.array([0, 100]), 'Celsius', 'Fahrenheit')
    array([ 32., 212.])
    >>>
    0 và 
    >>> import numpy as np
    >>># import hàm constants
    >>> from scipy import constants
    >>># Lấy giá trị hằng số theo key
    >>> constants.value(u'proton mass')
    1.67262192369e-27
    >>># Lấy đơn vị của hằng số theo key
    >>> constants.unit(u'elementary charge')
    'C'
    >>># Lấy độ chính xác tương đối của hằng số theo key
    >>> constants.precision(u'neutron mass')
    5.671887297281165e-10
    >>># import hàm find và physical_constants
    >>> from scipy.constants import find, physical_constants
    >>># Tìm các key trong từ điển các hằng số vật lý chứa‘boltzmann’?
    >>> find('structure')
    ['fine-structure constant', 'inverse fine-structure constant']
    >>># Lấy thông tin hằng số được gọi là ‘fine-structure constant’:
    >>> physical_constants['fine-structure constant']
    (0.0072973525693, '', 1.1e-12)
    >>> # Chuyển đổi nhiệt độ
    >>> convert_temperature(np.array([0, 100]), 'Celsius', 'Fahrenheit')
    array([ 32., 212.])
    >>>
    1 cắt các cụm phân cấp thành các cụm phẳng hoặc tìm gốc của rừng được hình thành bằng cách cắt bằng cách cung cấp id các cụm phẳng của mỗi quan sát
  • Nhóm hàm 
    >>> import numpy as np
    >>># import hàm constants
    >>> from scipy import constants
    >>># Lấy giá trị hằng số theo key
    >>> constants.value(u'proton mass')
    1.67262192369e-27
    >>># Lấy đơn vị của hằng số theo key
    >>> constants.unit(u'elementary charge')
    'C'
    >>># Lấy độ chính xác tương đối của hằng số theo key
    >>> constants.precision(u'neutron mass')
    5.671887297281165e-10
    >>># import hàm find và physical_constants
    >>> from scipy.constants import find, physical_constants
    >>># Tìm các key trong từ điển các hằng số vật lý chứa‘boltzmann’?
    >>> find('structure')
    ['fine-structure constant', 'inverse fine-structure constant']
    >>># Lấy thông tin hằng số được gọi là ‘fine-structure constant’:
    >>> physical_constants['fine-structure constant']
    (0.0072973525693, '', 1.1e-12)
    >>> # Chuyển đổi nhiệt độ
    >>> convert_temperature(np.array([0, 100]), 'Celsius', 'Fahrenheit')
    array([ 32., 212.])
    >>>
    2, 
    >>> import numpy as np
    >>># import hàm constants
    >>> from scipy import constants
    >>># Lấy giá trị hằng số theo key
    >>> constants.value(u'proton mass')
    1.67262192369e-27
    >>># Lấy đơn vị của hằng số theo key
    >>> constants.unit(u'elementary charge')
    'C'
    >>># Lấy độ chính xác tương đối của hằng số theo key
    >>> constants.precision(u'neutron mass')
    5.671887297281165e-10
    >>># import hàm find và physical_constants
    >>> from scipy.constants import find, physical_constants
    >>># Tìm các key trong từ điển các hằng số vật lý chứa‘boltzmann’?
    >>> find('structure')
    ['fine-structure constant', 'inverse fine-structure constant']
    >>># Lấy thông tin hằng số được gọi là ‘fine-structure constant’:
    >>> physical_constants['fine-structure constant']
    (0.0072973525693, '', 1.1e-12)
    >>> # Chuyển đổi nhiệt độ
    >>> convert_temperature(np.array([0, 100]), 'Celsius', 'Fahrenheit')
    array([ 32., 212.])
    >>>
    3, 
    >>> import numpy as np
    >>># import hàm constants
    >>> from scipy import constants
    >>># Lấy giá trị hằng số theo key
    >>> constants.value(u'proton mass')
    1.67262192369e-27
    >>># Lấy đơn vị của hằng số theo key
    >>> constants.unit(u'elementary charge')
    'C'
    >>># Lấy độ chính xác tương đối của hằng số theo key
    >>> constants.precision(u'neutron mass')
    5.671887297281165e-10
    >>># import hàm find và physical_constants
    >>> from scipy.constants import find, physical_constants
    >>># Tìm các key trong từ điển các hằng số vật lý chứa‘boltzmann’?
    >>> find('structure')
    ['fine-structure constant', 'inverse fine-structure constant']
    >>># Lấy thông tin hằng số được gọi là ‘fine-structure constant’:
    >>> physical_constants['fine-structure constant']
    (0.0072973525693, '', 1.1e-12)
    >>> # Chuyển đổi nhiệt độ
    >>> convert_temperature(np.array([0, 100]), 'Celsius', 'Fahrenheit')
    array([ 32., 212.])
    >>>
    4, 
    >>> import numpy as np
    >>># import hàm constants
    >>> from scipy import constants
    >>># Lấy giá trị hằng số theo key
    >>> constants.value(u'proton mass')
    1.67262192369e-27
    >>># Lấy đơn vị của hằng số theo key
    >>> constants.unit(u'elementary charge')
    'C'
    >>># Lấy độ chính xác tương đối của hằng số theo key
    >>> constants.precision(u'neutron mass')
    5.671887297281165e-10
    >>># import hàm find và physical_constants
    >>> from scipy.constants import find, physical_constants
    >>># Tìm các key trong từ điển các hằng số vật lý chứa‘boltzmann’?
    >>> find('structure')
    ['fine-structure constant', 'inverse fine-structure constant']
    >>># Lấy thông tin hằng số được gọi là ‘fine-structure constant’:
    >>> physical_constants['fine-structure constant']
    (0.0072973525693, '', 1.1e-12)
    >>> # Chuyển đổi nhiệt độ
    >>> convert_temperature(np.array([0, 100]), 'Celsius', 'Fahrenheit')
    array([ 32., 212.])
    >>>
    5, 
    >>> import numpy as np
    >>># import hàm constants
    >>> from scipy import constants
    >>># Lấy giá trị hằng số theo key
    >>> constants.value(u'proton mass')
    1.67262192369e-27
    >>># Lấy đơn vị của hằng số theo key
    >>> constants.unit(u'elementary charge')
    'C'
    >>># Lấy độ chính xác tương đối của hằng số theo key
    >>> constants.precision(u'neutron mass')
    5.671887297281165e-10
    >>># import hàm find và physical_constants
    >>> from scipy.constants import find, physical_constants
    >>># Tìm các key trong từ điển các hằng số vật lý chứa‘boltzmann’?
    >>> find('structure')
    ['fine-structure constant', 'inverse fine-structure constant']
    >>># Lấy thông tin hằng số được gọi là ‘fine-structure constant’:
    >>> physical_constants['fine-structure constant']
    (0.0072973525693, '', 1.1e-12)
    >>> # Chuyển đổi nhiệt độ
    >>> convert_temperature(np.array([0, 100]), 'Celsius', 'Fahrenheit')
    array([ 32., 212.])
    >>>
    6, 
    >>> import numpy as np
    >>># import hàm constants
    >>> from scipy import constants
    >>># Lấy giá trị hằng số theo key
    >>> constants.value(u'proton mass')
    1.67262192369e-27
    >>># Lấy đơn vị của hằng số theo key
    >>> constants.unit(u'elementary charge')
    'C'
    >>># Lấy độ chính xác tương đối của hằng số theo key
    >>> constants.precision(u'neutron mass')
    5.671887297281165e-10
    >>># import hàm find và physical_constants
    >>> from scipy.constants import find, physical_constants
    >>># Tìm các key trong từ điển các hằng số vật lý chứa‘boltzmann’?
    >>> find('structure')
    ['fine-structure constant', 'inverse fine-structure constant']
    >>># Lấy thông tin hằng số được gọi là ‘fine-structure constant’:
    >>> physical_constants['fine-structure constant']
    (0.0072973525693, '', 1.1e-12)
    >>> # Chuyển đổi nhiệt độ
    >>> convert_temperature(np.array([0, 100]), 'Celsius', 'Fahrenheit')
    array([ 32., 212.])
    >>>
    7, 
    >>> import numpy as np
    >>># import hàm constants
    >>> from scipy import constants
    >>># Lấy giá trị hằng số theo key
    >>> constants.value(u'proton mass')
    1.67262192369e-27
    >>># Lấy đơn vị của hằng số theo key
    >>> constants.unit(u'elementary charge')
    'C'
    >>># Lấy độ chính xác tương đối của hằng số theo key
    >>> constants.precision(u'neutron mass')
    5.671887297281165e-10
    >>># import hàm find và physical_constants
    >>> from scipy.constants import find, physical_constants
    >>># Tìm các key trong từ điển các hằng số vật lý chứa‘boltzmann’?
    >>> find('structure')
    ['fine-structure constant', 'inverse fine-structure constant']
    >>># Lấy thông tin hằng số được gọi là ‘fine-structure constant’:
    >>> physical_constants['fine-structure constant']
    (0.0072973525693, '', 1.1e-12)
    >>> # Chuyển đổi nhiệt độ
    >>> convert_temperature(np.array([0, 100]), 'Celsius', 'Fahrenheit')
    array([ 32., 212.])
    >>>
    8 và 
    >>> import numpy as np
    >>># import hàm constants
    >>> from scipy import constants
    >>># Lấy giá trị hằng số theo key
    >>> constants.value(u'proton mass')
    1.67262192369e-27
    >>># Lấy đơn vị của hằng số theo key
    >>> constants.unit(u'elementary charge')
    'C'
    >>># Lấy độ chính xác tương đối của hằng số theo key
    >>> constants.precision(u'neutron mass')
    5.671887297281165e-10
    >>># import hàm find và physical_constants
    >>> from scipy.constants import find, physical_constants
    >>># Tìm các key trong từ điển các hằng số vật lý chứa‘boltzmann’?
    >>> find('structure')
    ['fine-structure constant', 'inverse fine-structure constant']
    >>># Lấy thông tin hằng số được gọi là ‘fine-structure constant’:
    >>> physical_constants['fine-structure constant']
    (0.0072973525693, '', 1.1e-12)
    >>> # Chuyển đổi nhiệt độ
    >>> convert_temperature(np.array([0, 100]), 'Celsius', 'Fahrenheit')
    array([ 32., 212.])
    >>>
    9 cho các cụm liên kết
  • Hàm nhóm 
    from matplotlib import pyplot as plt
    import numpy as np 
    # Tạo hàm trpng miền thời gian
    # Tần số theo đơn vị Hz
    fre  = 10
    #Tỉ lệ mẫu
    fre_samp = 100
    #Thời gian
    t = np.linspace(0, 2, 2 * fre_samp, endpoint = False )
    # Hàm sin(f*2pi*t)+ cos(2f*2pi*t)
    a = np.sin(fre  * 2 * np.pi * t) + np.cos(2*fre  * 2 * np.pi * t)
    # Vẽ hàm
    figure, axis = plt.subplots()
    axis.plot(t, a)
    axis.set_xlabel ('Thời gian (s)')
    axis.set_ylabel ('Cường độ tín hiệu')
    plt.figure(1)
    
    # Sử dụng hàm fft chuyển sang miền tần số
    from scipy import fft
    # Thực hiện biến đổi fft
    A = fft.fft(a)
    frequency = fft.fftfreq(len(a)) * fre_samp
    # Vẽ hàm trong miền tần số
    figure, axis = plt.subplots()
    axis.stem(frequency, np.abs(A), use_line_collection=True)
    axis.set_xlabel('Tần số (Hz)')
    axis.set_ylabel('Độ lớn phổ tần số')
    axis.set_xlim(-fre_samp / 2, fre_samp/ 2)
    axis.set_ylim(-5, 120)
    plt.show()
    
    0, 
    from matplotlib import pyplot as plt
    import numpy as np 
    # Tạo hàm trpng miền thời gian
    # Tần số theo đơn vị Hz
    fre  = 10
    #Tỉ lệ mẫu
    fre_samp = 100
    #Thời gian
    t = np.linspace(0, 2, 2 * fre_samp, endpoint = False )
    # Hàm sin(f*2pi*t)+ cos(2f*2pi*t)
    a = np.sin(fre  * 2 * np.pi * t) + np.cos(2*fre  * 2 * np.pi * t)
    # Vẽ hàm
    figure, axis = plt.subplots()
    axis.plot(t, a)
    axis.set_xlabel ('Thời gian (s)')
    axis.set_ylabel ('Cường độ tín hiệu')
    plt.figure(1)
    
    # Sử dụng hàm fft chuyển sang miền tần số
    from scipy import fft
    # Thực hiện biến đổi fft
    A = fft.fft(a)
    frequency = fft.fftfreq(len(a)) * fre_samp
    # Vẽ hàm trong miền tần số
    figure, axis = plt.subplots()
    axis.stem(frequency, np.abs(A), use_line_collection=True)
    axis.set_xlabel('Tần số (Hz)')
    axis.set_ylabel('Độ lớn phổ tần số')
    axis.set_xlim(-fre_samp / 2, fre_samp/ 2)
    axis.set_ylim(-5, 120)
    plt.show()
    
    1, 
    from matplotlib import pyplot as plt
    import numpy as np 
    # Tạo hàm trpng miền thời gian
    # Tần số theo đơn vị Hz
    fre  = 10
    #Tỉ lệ mẫu
    fre_samp = 100
    #Thời gian
    t = np.linspace(0, 2, 2 * fre_samp, endpoint = False )
    # Hàm sin(f*2pi*t)+ cos(2f*2pi*t)
    a = np.sin(fre  * 2 * np.pi * t) + np.cos(2*fre  * 2 * np.pi * t)
    # Vẽ hàm
    figure, axis = plt.subplots()
    axis.plot(t, a)
    axis.set_xlabel ('Thời gian (s)')
    axis.set_ylabel ('Cường độ tín hiệu')
    plt.figure(1)
    
    # Sử dụng hàm fft chuyển sang miền tần số
    from scipy import fft
    # Thực hiện biến đổi fft
    A = fft.fft(a)
    frequency = fft.fftfreq(len(a)) * fre_samp
    # Vẽ hàm trong miền tần số
    figure, axis = plt.subplots()
    axis.stem(frequency, np.abs(A), use_line_collection=True)
    axis.set_xlabel('Tần số (Hz)')
    axis.set_ylabel('Độ lớn phổ tần số')
    axis.set_xlim(-fre_samp / 2, fre_samp/ 2)
    axis.set_ylim(-5, 120)
    plt.show()
    
    2, 
    from matplotlib import pyplot as plt
    import numpy as np 
    # Tạo hàm trpng miền thời gian
    # Tần số theo đơn vị Hz
    fre  = 10
    #Tỉ lệ mẫu
    fre_samp = 100
    #Thời gian
    t = np.linspace(0, 2, 2 * fre_samp, endpoint = False )
    # Hàm sin(f*2pi*t)+ cos(2f*2pi*t)
    a = np.sin(fre  * 2 * np.pi * t) + np.cos(2*fre  * 2 * np.pi * t)
    # Vẽ hàm
    figure, axis = plt.subplots()
    axis.plot(t, a)
    axis.set_xlabel ('Thời gian (s)')
    axis.set_ylabel ('Cường độ tín hiệu')
    plt.figure(1)
    
    # Sử dụng hàm fft chuyển sang miền tần số
    from scipy import fft
    # Thực hiện biến đổi fft
    A = fft.fft(a)
    frequency = fft.fftfreq(len(a)) * fre_samp
    # Vẽ hàm trong miền tần số
    figure, axis = plt.subplots()
    axis.stem(frequency, np.abs(A), use_line_collection=True)
    axis.set_xlabel('Tần số (Hz)')
    axis.set_ylabel('Độ lớn phổ tần số')
    axis.set_xlim(-fre_samp / 2, fre_samp/ 2)
    axis.set_ylim(-5, 120)
    plt.show()
    
    3, 
    from matplotlib import pyplot as plt
    import numpy as np 
    # Tạo hàm trpng miền thời gian
    # Tần số theo đơn vị Hz
    fre  = 10
    #Tỉ lệ mẫu
    fre_samp = 100
    #Thời gian
    t = np.linspace(0, 2, 2 * fre_samp, endpoint = False )
    # Hàm sin(f*2pi*t)+ cos(2f*2pi*t)
    a = np.sin(fre  * 2 * np.pi * t) + np.cos(2*fre  * 2 * np.pi * t)
    # Vẽ hàm
    figure, axis = plt.subplots()
    axis.plot(t, a)
    axis.set_xlabel ('Thời gian (s)')
    axis.set_ylabel ('Cường độ tín hiệu')
    plt.figure(1)
    
    # Sử dụng hàm fft chuyển sang miền tần số
    from scipy import fft
    # Thực hiện biến đổi fft
    A = fft.fft(a)
    frequency = fft.fftfreq(len(a)) * fre_samp
    # Vẽ hàm trong miền tần số
    figure, axis = plt.subplots()
    axis.stem(frequency, np.abs(A), use_line_collection=True)
    axis.set_xlabel('Tần số (Hz)')
    axis.set_ylabel('Độ lớn phổ tần số')
    axis.set_xlim(-fre_samp / 2, fre_samp/ 2)
    axis.set_ylim(-5, 120)
    plt.show()
    
    4, 
    from matplotlib import pyplot as plt
    import numpy as np 
    # Tạo hàm trpng miền thời gian
    # Tần số theo đơn vị Hz
    fre  = 10
    #Tỉ lệ mẫu
    fre_samp = 100
    #Thời gian
    t = np.linspace(0, 2, 2 * fre_samp, endpoint = False )
    # Hàm sin(f*2pi*t)+ cos(2f*2pi*t)
    a = np.sin(fre  * 2 * np.pi * t) + np.cos(2*fre  * 2 * np.pi * t)
    # Vẽ hàm
    figure, axis = plt.subplots()
    axis.plot(t, a)
    axis.set_xlabel ('Thời gian (s)')
    axis.set_ylabel ('Cường độ tín hiệu')
    plt.figure(1)
    
    # Sử dụng hàm fft chuyển sang miền tần số
    from scipy import fft
    # Thực hiện biến đổi fft
    A = fft.fft(a)
    frequency = fft.fftfreq(len(a)) * fre_samp
    # Vẽ hàm trong miền tần số
    figure, axis = plt.subplots()
    axis.stem(frequency, np.abs(A), use_line_collection=True)
    axis.set_xlabel('Tần số (Hz)')
    axis.set_ylabel('Độ lớn phổ tần số')
    axis.set_xlim(-fre_samp / 2, fre_samp/ 2)
    axis.set_ylim(-5, 120)
    plt.show()
    
    5 và 
    from matplotlib import pyplot as plt
    import numpy as np 
    # Tạo hàm trpng miền thời gian
    # Tần số theo đơn vị Hz
    fre  = 10
    #Tỉ lệ mẫu
    fre_samp = 100
    #Thời gian
    t = np.linspace(0, 2, 2 * fre_samp, endpoint = False )
    # Hàm sin(f*2pi*t)+ cos(2f*2pi*t)
    a = np.sin(fre  * 2 * np.pi * t) + np.cos(2*fre  * 2 * np.pi * t)
    # Vẽ hàm
    figure, axis = plt.subplots()
    axis.plot(t, a)
    axis.set_xlabel ('Thời gian (s)')
    axis.set_ylabel ('Cường độ tín hiệu')
    plt.figure(1)
    
    # Sử dụng hàm fft chuyển sang miền tần số
    from scipy import fft
    # Thực hiện biến đổi fft
    A = fft.fft(a)
    frequency = fft.fftfreq(len(a)) * fre_samp
    # Vẽ hàm trong miền tần số
    figure, axis = plt.subplots()
    axis.stem(frequency, np.abs(A), use_line_collection=True)
    axis.set_xlabel('Tần số (Hz)')
    axis.set_ylabel('Độ lớn phổ tần số')
    axis.set_xlim(-fre_samp / 2, fre_samp/ 2)
    axis.set_ylim(-5, 120)
    plt.show()
    
    6 cho tính toán thống kê trên hệ thống phân cấp
  • Hàm 
    from matplotlib import pyplot as plt
    import numpy as np 
    # Tạo hàm trpng miền thời gian
    # Tần số theo đơn vị Hz
    fre  = 10
    #Tỉ lệ mẫu
    fre_samp = 100
    #Thời gian
    t = np.linspace(0, 2, 2 * fre_samp, endpoint = False )
    # Hàm sin(f*2pi*t)+ cos(2f*2pi*t)
    a = np.sin(fre  * 2 * np.pi * t) + np.cos(2*fre  * 2 * np.pi * t)
    # Vẽ hàm
    figure, axis = plt.subplots()
    axis.plot(t, a)
    axis.set_xlabel ('Thời gian (s)')
    axis.set_ylabel ('Cường độ tín hiệu')
    plt.figure(1)
    
    # Sử dụng hàm fft chuyển sang miền tần số
    from scipy import fft
    # Thực hiện biến đổi fft
    A = fft.fft(a)
    frequency = fft.fftfreq(len(a)) * fre_samp
    # Vẽ hàm trong miền tần số
    figure, axis = plt.subplots()
    axis.stem(frequency, np.abs(A), use_line_collection=True)
    axis.set_xlabel('Tần số (Hz)')
    axis.set_ylabel('Độ lớn phổ tần số')
    axis.set_xlim(-fre_samp / 2, fre_samp/ 2)
    axis.set_ylim(-5, 120)
    plt.show()
    
    7 để định hình các cụm phức tạp
  • Nhóm hàm 
    from matplotlib import pyplot as plt
    import numpy as np 
    # Tạo hàm trpng miền thời gian
    # Tần số theo đơn vị Hz
    fre  = 10
    #Tỉ lệ mẫu
    fre_samp = 100
    #Thời gian
    t = np.linspace(0, 2, 2 * fre_samp, endpoint = False )
    # Hàm sin(f*2pi*t)+ cos(2f*2pi*t)
    a = np.sin(fre  * 2 * np.pi * t) + np.cos(2*fre  * 2 * np.pi * t)
    # Vẽ hàm
    figure, axis = plt.subplots()
    axis.plot(t, a)
    axis.set_xlabel ('Thời gian (s)')
    axis.set_ylabel ('Cường độ tín hiệu')
    plt.figure(1)
    
    # Sử dụng hàm fft chuyển sang miền tần số
    from scipy import fft
    # Thực hiện biến đổi fft
    A = fft.fft(a)
    frequency = fft.fftfreq(len(a)) * fre_samp
    # Vẽ hàm trong miền tần số
    figure, axis = plt.subplots()
    axis.stem(frequency, np.abs(A), use_line_collection=True)
    axis.set_xlabel('Tần số (Hz)')
    axis.set_ylabel('Độ lớn phổ tần số')
    axis.set_xlim(-fre_samp / 2, fre_samp/ 2)
    axis.set_ylim(-5, 120)
    plt.show()
    
    8, 
    from matplotlib import pyplot as plt
    import numpy as np 
    # Tạo hàm trpng miền thời gian
    # Tần số theo đơn vị Hz
    fre  = 10
    #Tỉ lệ mẫu
    fre_samp = 100
    #Thời gian
    t = np.linspace(0, 2, 2 * fre_samp, endpoint = False )
    # Hàm sin(f*2pi*t)+ cos(2f*2pi*t)
    a = np.sin(fre  * 2 * np.pi * t) + np.cos(2*fre  * 2 * np.pi * t)
    # Vẽ hàm
    figure, axis = plt.subplots()
    axis.plot(t, a)
    axis.set_xlabel ('Thời gian (s)')
    axis.set_ylabel ('Cường độ tín hiệu')
    plt.figure(1)
    
    # Sử dụng hàm fft chuyển sang miền tần số
    from scipy import fft
    # Thực hiện biến đổi fft
    A = fft.fft(a)
    frequency = fft.fftfreq(len(a)) * fre_samp
    # Vẽ hàm trong miền tần số
    figure, axis = plt.subplots()
    axis.stem(frequency, np.abs(A), use_line_collection=True)
    axis.set_xlabel('Tần số (Hz)')
    axis.set_ylabel('Độ lớn phổ tần số')
    axis.set_xlim(-fre_samp / 2, fre_samp/ 2)
    axis.set_ylim(-5, 120)
    plt.show()
    
    9, python-m pip install --user scipy
    0 và python-m pip install --user scipy
    1. Đây là cấu trúc dữ liệu và hàm để biểu diễn cấu trúc phân cấp cấu trúc như cây đối tượng
  • Nhóm hàm python-m pip install --user scipy
    2, python-m pip install --user scipy
    3, python-m pip install --user scipy
    4, python-m pip install --user scipy
    5 và python-m pip install --user scipy
    6. Đây là các vị trí từ để kiểm tra tính hợp lệ của ma trận liên kết và ma trận không nhất quán cũng như để kiểm tra sự đẳng cấp cấu trúc của hai phép gán cụm phức tạp
  • Hàm python-m pip install --user scipy
    7 để đặt danh sách mã màu matplotlib nhằm sử dụng cho cây phả hệ

Ví dụ. Một số chức năng trong mô-đun

>>># hàm fcluster: Hình thành các cụm phẳng từ cụm phân cấp được xác định bởi ma trận liên kết đã cho.
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [0, 3], [1, 4],
         [4, 0], [3, 0], [4, 1],
         [4, 4], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.9, criterion='distance')
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)
>>>
>>># hàm cophenet(Z[, Y]): Tính khoảng cách cophenetic giữa mỗi lần quan sát trong cụm phân cấp được xác định bởi liên kết Z.
>>> from scipy.cluster.hierarchy import single, cophenet
>>> from scipy.spatial.distance import pdist, squareform
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [1, 1], [1, 4],
         [4, 1], [2, 0], [4, 2],
         [4, 4], [3, 3], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = single(pdist(X))
>>> cophenet(Z)
array([1.        , 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 1.        , 1.        ,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 1.        , 1.        , 1.        , 1.        ,
       1.        ])
>>># Dùng hàm squareform() để output dưới dạng ma trận thông thường
>>> squareform(cophenet(Z))
array([[0.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 0.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 0.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 0.        , 2.23606798,
        1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 1.        , 2.23606798, 0.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 1.        , 2.23606798,
        0.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 0.        , 2.23606798, 1.        , 1.        ,
        1.        , 1.        ],
       [1.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 0.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 0.        , 1.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 0.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        0.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        1.        , 0.        ]])
>>># hàm fcluster - gán các điểm dữ liệu gốc cho các cụm đơn. Việc gán này chủ yếu phụ thuộc vào ngưỡng khoảng cách t - khoảng cách giữa các cụm tối đa được phép
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 1], [0, 1], [1, 0],
         [1, 1], [0, 3], [1, 1],
         [2, 3], [3, 0], [4, 1],
         [2, 2], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.6, criterion='distance')
array([ 1,  1,  3,  2, 10,  2,  8,  4,  5,  9,  6,  7], dtype=int32)
>>> 
8

>>># hàm fcluster: Hình thành các cụm phẳng từ cụm phân cấp được xác định bởi ma trận liên kết đã cho.
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [0, 3], [1, 4],
         [4, 0], [3, 0], [4, 1],
         [4, 4], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.9, criterion='distance')
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)
>>>
>>># hàm cophenet(Z[, Y]): Tính khoảng cách cophenetic giữa mỗi lần quan sát trong cụm phân cấp được xác định bởi liên kết Z.
>>> from scipy.cluster.hierarchy import single, cophenet
>>> from scipy.spatial.distance import pdist, squareform
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [1, 1], [1, 4],
         [4, 1], [2, 0], [4, 2],
         [4, 4], [3, 3], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = single(pdist(X))
>>> cophenet(Z)
array([1.        , 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 1.        , 1.        ,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 1.        , 1.        , 1.        , 1.        ,
       1.        ])
>>># Dùng hàm squareform() để output dưới dạng ma trận thông thường
>>> squareform(cophenet(Z))
array([[0.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 0.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 0.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 0.        , 2.23606798,
        1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 1.        , 2.23606798, 0.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 1.        , 2.23606798,
        0.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 0.        , 2.23606798, 1.        , 1.        ,
        1.        , 1.        ],
       [1.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 0.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 0.        , 1.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 0.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        0.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        1.        , 0.        ]])
>>># hàm fcluster - gán các điểm dữ liệu gốc cho các cụm đơn. Việc gán này chủ yếu phụ thuộc vào ngưỡng khoảng cách t - khoảng cách giữa các cụm tối đa được phép
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 1], [0, 1], [1, 0],
         [1, 1], [0, 3], [1, 1],
         [2, 3], [3, 0], [4, 1],
         [2, 2], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.6, criterion='distance')
array([ 1,  1,  3,  2, 10,  2,  8,  4,  5,  9,  6,  7], dtype=int32)
>>> 

Để tìm hiểu thêm thông tin các chức năng trong mô-đun

>>># hàm fcluster: Hình thành các cụm phẳng từ cụm phân cấp được xác định bởi ma trận liên kết đã cho.
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [0, 3], [1, 4],
         [4, 0], [3, 0], [4, 1],
         [4, 4], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.9, criterion='distance')
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12], dtype=int32)
>>>
>>># hàm cophenet(Z[, Y]): Tính khoảng cách cophenetic giữa mỗi lần quan sát trong cụm phân cấp được xác định bởi liên kết Z.
>>> from scipy.cluster.hierarchy import single, cophenet
>>> from scipy.spatial.distance import pdist, squareform
>>> X = [[0, 0], [0, 1], [1, 0],
         [0, 4], [1, 1], [1, 4],
         [4, 1], [2, 0], [4, 2],
         [4, 4], [3, 3], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = single(pdist(X))
>>> cophenet(Z)
array([1.        , 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 1.        , 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 1.        , 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       2.23606798, 2.23606798, 1.        , 1.        , 1.        ,
       1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
       1.        , 1.        , 1.        , 1.        , 1.        ,
       1.        ])
>>># Dùng hàm squareform() để output dưới dạng ma trận thông thường
>>> squareform(cophenet(Z))
array([[0.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 0.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 0.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 0.        , 2.23606798,
        1.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [1.        , 1.        , 1.        , 2.23606798, 0.        ,
        2.23606798, 2.23606798, 1.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 1.        , 2.23606798,
        0.        , 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 0.        , 2.23606798, 1.        , 1.        ,
        1.        , 1.        ],
       [1.        , 1.        , 1.        , 2.23606798, 1.        ,
        2.23606798, 2.23606798, 0.        , 2.23606798, 2.23606798,
        2.23606798, 2.23606798],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 0.        , 1.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 0.        ,
        1.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        0.        , 1.        ],
       [2.23606798, 2.23606798, 2.23606798, 2.23606798, 2.23606798,
        2.23606798, 1.        , 2.23606798, 1.        , 1.        ,
        1.        , 0.        ]])
>>># hàm fcluster - gán các điểm dữ liệu gốc cho các cụm đơn. Việc gán này chủ yếu phụ thuộc vào ngưỡng khoảng cách t - khoảng cách giữa các cụm tối đa được phép
>>> from scipy.cluster.hierarchy import ward, fcluster
>>> from scipy.spatial.distance import pdist
>>> X = [[0, 1], [0, 1], [1, 0],
         [1, 1], [0, 3], [1, 1],
         [2, 3], [3, 0], [4, 1],
         [2, 2], [3, 4], [4, 3]]
>>> # Tạo ma trận Z
>>> Z = ward(pdist(X))
>>> fcluster(Z, t=0.6, criterion='distance')
array([ 1,  1,  3,  2, 10,  2,  8,  4,  5,  9,  6,  7], dtype=int32)
>>> 
8, các bạn có thể xem tại

2. Hằng số gói (python-m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nos0)

Gói

>>> # khai báo numpy
>>> import numpy as np
>>> # Hàm mrid()
>>> np.mgrid[0:5,0:4]
array([[[0, 0, 0, 0],
        [1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3],
        [4, 4, 4, 4]],

       [[0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3]]])
>>> # Hàm mrid() số ảo
>>> np.mgrid[0:5:4j,0:4:3j]
array([[[0.        , 0.        , 0.        ],
        [1.66666667, 1.66666667, 1.66666667],
        [3.33333333, 3.33333333, 3.33333333],
        [5.        , 5.        , 5.        ]],

       [[0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ]]])
>>> # Hàm đa thức
>>> from numpy import poly1d
>>> p = poly1d([2,6])
>>> print(p)
 
2 x + 6
>>> # Bình phương hàm p
>>> print(p*p)
   2
4 x + 24 x + 36
>>> # Đạo hàm của p
>>> print(p.deriv())
 
2
>>> # Giá trị p tại x = 1, 2, 10
>>> p([1,2,10])
array([ 8, 10, 26])
>>> # Hàm select() để lấy ra array theo điều kiện
>>> x = np.arange(15)
>>> dieu_kien = [x< 10, x>=5]
>>> chon_ra = [x, x**3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8, 27, 64,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
>>> dieu_kien = [x>12, x<3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 13, 14])
>>> 
0 bao gồm hằng số vật lý, toán học và đơn vị. Trong gói này có một số hàm để gọi giá trị hằng số, chuyển đổi các đơn vị

Ví dụ. Một số hàm trong gói 

>>> # khai báo numpy
>>> import numpy as np
>>> # Hàm mrid()
>>> np.mgrid[0:5,0:4]
array([[[0, 0, 0, 0],
        [1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3],
        [4, 4, 4, 4]],

       [[0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3],
        [0, 1, 2, 3]]])
>>> # Hàm mrid() số ảo
>>> np.mgrid[0:5:4j,0:4:3j]
array([[[0.        , 0.        , 0.        ],
        [1.66666667, 1.66666667, 1.66666667],
        [3.33333333, 3.33333333, 3.33333333],
        [5.        , 5.        , 5.        ]],

       [[0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ],
        [0.        , 2.        , 4.        ]]])
>>> # Hàm đa thức
>>> from numpy import poly1d
>>> p = poly1d([2,6])
>>> print(p)
 
2 x + 6
>>> # Bình phương hàm p
>>> print(p*p)
   2
4 x + 24 x + 36
>>> # Đạo hàm của p
>>> print(p.deriv())
 
2
>>> # Giá trị p tại x = 1, 2, 10
>>> p([1,2,10])
array([ 8, 10, 26])
>>> # Hàm select() để lấy ra array theo điều kiện
>>> x = np.arange(15)
>>> dieu_kien = [x< 10, x>=5]
>>> chon_ra = [x, x**3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8, 27, 64,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])
>>> dieu_kien = [x>12, x<3]
>>> np.select(dieu_kien, chon_ra)
array([ 0,  1,  8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 13, 14])
>>> 
0

>>> import numpy as np
>>># import hàm constants
>>> from scipy import constants
>>># Lấy giá trị hằng số theo key
>>> constants.value(u'proton mass')
1.67262192369e-27
>>># Lấy đơn vị của hằng số theo key
>>> constants.unit(u'elementary charge')
'C'
>>># Lấy độ chính xác tương đối của hằng số theo key
>>> constants.precision(u'neutron mass')
5.671887297281165e-10
>>># import hàm find và physical_constants
>>> from scipy.constants import find, physical_constants
>>># Tìm các key trong từ điển các hằng số vật lý chứa‘boltzmann’?
>>> find('structure')
['fine-structure constant', 'inverse fine-structure constant']
>>># Lấy thông tin hằng số được gọi là ‘fine-structure constant’:
>>> physical_constants['fine-structure constant']
(0.0072973525693, '', 1.1e-12)
>>> # Chuyển đổi nhiệt độ
>>> convert_temperature(np.array([0, 100]), 'Celsius', 'Fahrenheit')
array([ 32., 212.])
>>>

3. Biến đổi gói Fourier (python-m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nos3, python-m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nos4)

Gói python-m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nos5 là gói biến đổi Fourier được phát triển trước, nay trở thành thảnh di sản và được thay thế bằng gói python-m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nos6. Gói python-m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nos6 có hàm đa dạng giúp giải quyết dễ dàng biến đổi Fourier. Packages are partition to the group functions

  • Biến đổi Fourier fast (FFTs) bao gồm 18 hàm như python-m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nos8, python-m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nos9, special0, v. v
  • Biến đổi Sin và Cosine rời rạc (DST và DCT) bao gồm 8 hàm như special1, special2, special3, v. v
  • Các chức năng trợ giúp với 7 hàm như special4, special5, special6, v. v
  • Kiểm soát phụ trợ như special7, special8, special9 và cluster0

Ví dụ. Biến đổi Fourier nhanh bằng hàm fft lên hàm a(t) = sin(10*2πt) + cos(20*2πt)

from matplotlib import pyplot as plt
import numpy as np 
# Tạo hàm trpng miền thời gian
# Tần số theo đơn vị Hz
fre  = 10
#Tỉ lệ mẫu
fre_samp = 100
#Thời gian
t = np.linspace(0, 2, 2 * fre_samp, endpoint = False )
# Hàm sin(f*2pi*t)+ cos(2f*2pi*t)
a = np.sin(fre  * 2 * np.pi * t) + np.cos(2*fre  * 2 * np.pi * t)
# Vẽ hàm
figure, axis = plt.subplots()
axis.plot(t, a)
axis.set_xlabel ('Thời gian (s)')
axis.set_ylabel ('Cường độ tín hiệu')
plt.figure(1)

# Sử dụng hàm fft chuyển sang miền tần số
from scipy import fft
# Thực hiện biến đổi fft
A = fft.fft(a)
frequency = fft.fftfreq(len(a)) * fre_samp
# Vẽ hàm trong miền tần số
figure, axis = plt.subplots()
axis.stem(frequency, np.abs(A), use_line_collection=True)
axis.set_xlabel('Tần số (Hz)')
axis.set_ylabel('Độ lớn phổ tần số')
axis.set_xlim(-fre_samp / 2, fre_samp/ 2)
axis.set_ylim(-5, 120)
plt.show()

Picture function in the domain time

Sử dụng python nội suy numpy

Hàm số ảnh trong tần số

Sử dụng python nội suy numpy

Các bạn có thể tham khảo thêm các hàm biến đổi Fourier hữu ích khác tại đây

pause

Mình vừa giới thiệu cho các bạn vài nét tổng quan cũng như 3 submodule đầu tiên của Scipy. Các mô hình con tiếp theo sẽ được trình bày trong phần tiếp theo. Cảm ơn các bạn đã đọc. If you are at section 2