Pattern Classification: Maximum Likelihood Estimation

1. Problem description

write picture description here
write picture description here

2. Analysis of the core idea of ​​the algorithm

write picture description here

3. Code and running results

a.py

import xlrd
import numpy as np


# 读取数据
def read_data(k):
    x = []
    data = xlrd.open_workbook("lab2_data.xlsx")
    table = data.sheets()[0]
    rows = table.nrows
    for i in range(1, rows):
        row_value = table.row_values(i)
        if row_value[3] == k:
            x.append(row_value)
    return x


# 计算均值
def get_u(x):
    u = np.mean(x)
    return u


# 计算方差
def get_sigmal(x):
    sigmal = np.cov(x)
    return sigmal


def main():
    u = [0, 0, 0]
    sigmal = [0, 0, 0]
    data = read_data(1)

    for i in range(3):
        xi = [x[i] for x in filter(lambda x: x, data)]
        print("x", i + 1, xi)
        u[i] = get_u(xi)
        print("u", i + 1, u[i])
        sigmal[i] = get_sigmal(xi)
        print("sigmal", i + 1, sigmal[i])


if __name__ == '__main__':
    main()

The mean and variance of the three features xi in class ω1 are:
write picture description here

b.py

import xlrd
import numpy as np


# 读取数据
def read_data(k):
    x = []
    data = xlrd.open_workbook("lab2_data.xlsx")
    table = data.sheets()[0]
    rows = table.nrows
    for i in range(1, rows):
        row_value = table.row_values(i)
        if row_value[3] == k:
            x.append(row_value)
    return x


# 计算均值
def get_u(x):
    u = np.mean(np.mat(x).T, axis=0)  # 求每列的均值
    return u


# 计算协方差
def get_sigmal(x):
    sigmal = np.cov(x)
    # sigmal = np.cov(np.mat(x).T)
    return sigmal


def main():
    u = [0, 0, 0]
    sigmal = [0, 0, 0]
    data = read_data(1)

    for i in range(3):
        xi = [x[i] for x in filter(lambda x: x, data)], [x[(i+1) % 3] for x in filter(lambda x: x, data)]
        print("x", i + 1, (i+1) % 3 + 1, "\n", np.mat(xi))
        u[i] = get_u(xi)
        print("u", i + 1, (i+1) % 3 + 1, "\n", np.mat(u[i]))
        sigmal[i] = get_sigmal(xi)
        print("sigmal", i + 1, (i+1) % 3 + 1, "\n", np.mat(sigmal[i]))


if __name__ == '__main__':
    main()

The mean and variance of any combination of two features in class ω1 are:
write picture description here

c.py

import xlrd
import numpy as np


# 读取数据
def read_data(k):
    x = []
    data = xlrd.open_workbook("lab2_data.xlsx")
    table = data.sheets()[0]
    rows = table.nrows
    for i in range(1, rows):
        row_value = table.row_values(i)
        if row_value[3] == k:
            x.append(row_value)
    return x


# 计算均值
def get_u(x):
    u = np.mean(x, axis=0)
    return u


# 计算协方差
def get_sigmal(x):
    sigmal = np.cov(np.mat(x).T)
    return sigmal


def main():
    data = read_data(1)
    xi = [x[:3] for x in filter(lambda x: x, data)]
    print("x", "\n", np.mat(xi))
    u = get_u(xi)
    print("u", "\n", np.mat(u))
    sigmal = get_sigmal(xi)
    print("sigmal", "\n", np.mat(sigmal))


if __name__ == '__main__':
    main()

The mean and variance of the 3 feature combinations in class ω1 are:
write picture description here

d.py

import xlrd
import numpy as np


# 读取数据
def read_data(k):
    x = []
    data = xlrd.open_workbook("lab2_data.xlsx")
    table = data.sheets()[0]
    rows = table.nrows
    for i in range(1, rows):
        row_value = table.row_values(i)
        if row_value[3] == k:
            x.append(row_value)
    return x


# 计算均值
def get_u(x):
    u = np.mean(x)
    return u


# 计算方差
def get_sigmal(x):
    sigmal = np.cov(x)
    return sigmal


def main():
    u = [0, 0, 0]
    sigmal = [0, 0, 0]
    data = read_data(2)

    for i in range(3):
        xi = [x[i] for x in filter(lambda x: x, data)]
        print("x", i + 1, "\n", xi)
        u[i] = get_u(xi)
        print("u", i + 1, "\n", u[i])
        sigmal[i] = get_sigmal(xi)
        print("sigmal", i + 1, "\n", sigmal[i])


if __name__ == '__main__':
    main()

The three parameters in the mean and covariance matrix in class ω2 are:
write picture description here

Please correct me if there is any mistake

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325952468&siteId=291194637