<jupyter_start><jupyter_text>*多类型分类*<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.io import loadmat

data = loadmat('ex3data1.mat')
data
data['X'].shape, data['y'].shape
X, y = data['X'], data['y']
X, y
X.shape, y.shape

def plot_an_image(X):
    pick_one = np.random.randint(len(X))
    image = X[pick_one, :]
    fig, ax = plt.subplots(figsize=(1, 1))
    ax.imshow(image.reshape(20, 20).T, cmap = 'gray_r')
    plt.xticks([])
    plt.yticks([])
plot_an_image(X)
def plot_some_image(X, num=10):
    if num ** 2 > len(X):
        return None
    some_index = np.random.choice(len(X), num ** 2)
#     print(type(some_index))
    images = X[some_index, :]
    print(images.shape)
    fig, ax = plt.subplots(num, num, figsize=(num, num)
    , sharex=True, sharey=True
                          ) 
    for r in range(num):
        for c in range(num):
            ax[r, c].imshow(images[num * r + c].reshape(20, 20).T, cmap='gray_r')
    plt.xticks([])
    plt.yticks([])
plot_some_image(X)
def sigmod(z):
    return 1 / (1 + np.exp(-z))
def cost_func(theta, X, y, rate):
    m = len(X)
    temp = sigmod(X @ theta)
    first = y * np.log(temp)
    second = (1 - y) * np.log(1 - temp)
    reg = theta[1:] @ theta[1:] * (rate / (2 * m))
    return -np.sum(first + second) / m + reg
def gradient_reg(theta, X, y, rate):
    m = len(X)
    reg = theta[1:] * (rate / m)
    reg = np.insert(reg, 0, values=0, axis=0)
    first = (X.T @ (sigmod(X @ theta) - y)) / m
    return first + reg

X = np.insert(X, 0, values=1, axis=1)
X.shape
y = y.flatten()
y.shape
from scipy.optimize import minimize

def one_vs_all(X, y, rate=1, k=10):
    n = X.shape[1]
#     print(n)
    theta_all = np.zeros((k, n))
    
    for i in range(1, k + 1):
        theta_i = np.zeros(n,)
        res = minimize(fun=cost_func,
                      x0=theta_i,
                      args=(X, y==i, rate),
                      method='TNC',
                      jac=gradient_reg)
        theta_all[i-1,:] = res.x
    return theta_all
theta_final = one_vs_all(X, y)
theta_final
theta_final.shape
def predict(X, theta_final):
    h = sigmod(X@theta_final.T) #(5000, 401) (10, 401) 
    h_argmax = np.argmax(h, axis=1)
    return h_argmax + 1
y_pred = predict(X, theta_final)

accuracy = np.mean(y_pred == y)
accuracy<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.165890816758358, 'fraction_numerical': 0.022005924672027083, 'mean_word_length': 2.581818181818182, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '162951', 'license': 'Apache-2.0', 'n_tokens_mistral': 1004, 'n_tokens_neox': 953, 'n_words': 219, 'path': 'StudyNotesOfML/3. Neural netwok learning/1. Multivariate classification.ipynb', 'repo_name': 'yang-233/ML', 'size': '2363'}