一:__init__.py(主函数)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.io import loadmat
import function as f #引入所需要的函数,自建文件
data = loadmat(r'******') #加载数据
print(data['X'].shape)
print(data['y'].shape)
rows = data['X'].shape[0]
params = data['y'].shape[1]
all_theta = np.zeros((10, params + 1))
X = np.insert(data['X'], 0, values = np.ones(rows), axis = 1)
theta = np.zeros(params + 1)
y_0 = np.array([1 if label == 0 else 0 for label in data['y']])
y_0 = np.reshape(y_0, (rows, 1))
print(np.unique(data['y']))#看下有几类标签
all_theta = f.one_vs_all(data['X'], data['y'], 10, 1)
print(all_theta)
#使用predict_all函数为每个实例生成类预测,看看我们的分类器是如何工作的
y_pred = f.predict_all(data['X'], all_theta)
correct = [1 if a == b else 0 for (a, b) in zip(y_pred, data['y'])]
accuracy = (sum(map(int, correct)) / float(len(correct)))
print ('accuracy = {0}%'.format(accuracy * 100))
二:function.py(所需函数文件)
import numpy as np
def sigmoid(z):
return 1 / (1 + np.exp(-z))
#代价函数
def cost(theta, X, y, learningRate):
theta =np.matrix(theta)
X = np.matrix(X)
y = np.matrix(y)
#计算损失函数,不含正则化
h = sigmoid(X * theta.T)
cross_cost = np.multiply(-y, np.log(h)) - np.multiply((1 - y), np.log(1 - h))
#计算正则化部分
reg = (learningRate / (2 * len(X))) * np.sum(np.power(theta[1:], 2))
whole_cost = np.sum(cross_cost) / len(X) + reg
return whole_cost
#向量化的梯度函数
def gradient(theta, X, y, learningRate):
theta = np.matrix(theta)
X = np.matrix(X)
y = np.matrix(y)
#计算梯度
error = sigmoid(X * theta.T) - y #误差
grad = ((X.T * error) / len(X)).T + (learningRate / len(X)) * theta
#由于j=0时不需要正则化,所以这里重置一下
grad[0, 0] = np.sum(np.multiply(error, X[:, 0])) / len(X)
return np.array(grad).ravel()
#该函数计算10个分类器中的每个分类器的最终权重,并将权重返回为k X(n + 1)数组,其中n是参数数量。
from scipy.optimize import minimize
def one_vs_all(X, y, num_labels, learning_rate):
rows = X.shape[0]
params = X.shape[1]
all_theta = np.zeros((num_labels, params + 1))
X = np.insert(X, 0, values=np.ones(rows), axis=1) # 插了一列1
# labels are 1-indexed instead of 0-indexed
for i in range(1, num_labels + 1):
theta = np.zeros(params + 1)
y_i = np.array([1 if label == i else 0 for label in y])
y_i = np.reshape(y_i, (rows, 1))
# minimize the objective function
fmin = minimize(fun=cost, x0=theta, args=(X, y_i, learning_rate), method='TNC', jac=gradient) # 参数位置保证正确
all_theta[i - 1, :] = fmin.x
return all_theta
def predict_all(X, all_theta):
#获得矩阵的维度信息
rows = X.shape[0]
params = X.shape[1]
num_labels = all_theta.shape[0]
#把矩阵X加入一行零元素
X = np.insert(X, 0, values = np.ones(rows), axis = 1)
#把矩阵X和theta转换成numpy矩阵
X = np.matrix(X)
all_theta = np.matrix(all_theta)
#计算样本属于每一类的概率
h = sigmoid(X * all_theta.T)
#找到样本预测概率中的最大的值(因为我们数组是零索引 所以需要 加 1)
h_argxmax = np.argmax(h, axis = 1) + 1
return h_argxmax