**
**
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.io as scio #用来读取matlab格式的数据集
from scipy.optimize import minimize
https://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.io.loadmat.html
python存取mat文件
https://cloud.tencent.com/developer/article/1390390
#读取数据集
def load_data(path):
data = scio.loadmat(path)
x = data['X'] #大写
y = data['y']
return x,y
x,y = load_data('ex3data1.mat')
print(np.unique(y)) #共有十类标签
x.shape,y.shape
[ 1 2 3 4 5 6 7 8 9 10]
((5000, 400), (5000, 1))
0 1 2 … 399
0 1 2 … 399
. . . …
0 1 2 … 399
如上理解,一行就是一张图片的400个像素浮点数表示
https://blog.csdn.net/a2224998/article/details/45499881
def plot_an_image(x):
#打印一张图片
pick_one = np.random.randint(0,4999)
image = x[pick_one,:] #随机选取一行
fig ,ax = plt.subplots(figsize = (1,1))#获取子图的方式,这里主要用来控制图像大小
ax.matshow(image.reshape((20,20)),cmap = 'gray_r')#矩阵可视化
plt.title('{}'.format(y[pick_one]))
plt.xticks([]) # 去除刻度,美观
plt.yticks([])
plt.show()
plot_an_image(x)#(⊙o⊙)…图像方向看着好让人别扭,还有10这个数字是用0表示(?)...
https://blog.csdn.net/qq_18343569/article/details/50920433
用ex2练习中正则化的logistic回归的代价函数
然后用矩阵乘法完成计算
def sigmoid(z):
return 1 / (1 + np.exp(-z))
#正则化代价函数
def regularized_cost_function(theta,x,y,lam):
m = x.shape[0]#总例数
j = (y.dot(np.log(sigmoid(x.dot(theta))))+(1-y).dot(np.log(1-sigmoid(x.dot(theta)))))/(-m)
#加上惩罚项
penalty = lam*(theta.dot(theta))/(2*m)
return j+penalty
#计算偏导数
def regularized_gradient_descent(theta,x,y,lam):
m = x.shape[0]
partial_j = ((sigmoid(x.dot(theta))-y).T).dot(x)/m
partial_penalty = lam*theta/m
partial_penalty[0] = 0 #第一项不惩罚
return partial_j+partial_penalty
def one_vs_all(x,y,lam,k):
all_theta = np.zeros((k,x.shape[1]))
#(10,401)一共十类,加入一个x0,参数矩阵多一列theta0,所以共401*10个参数
for i in range(1,k+1):
part_theta = np.zeros(x.shape[1])
resu_y = np.array([1 if label == i else 0 for label in y])
part_theta = minimize(fun=regularized_cost_function, x0=part_theta, args=(x, resu_y, lam), method='TNC',
jac=regularized_gradient_descent, options={'disp': True})
all_theta[i-1,:] = part_theta.x #i从1到10,而下标是从0到9
return all_theta
def predict_all(x,all_theta):
h = sigmoid(x.dot(all_theta.T)) # 这里的all_theta需要转置
h_argmax = np.argmax(h,axis=1) #返回沿轴axis最大值的索引
h_argmax = h_argmax + 1
return h_argmax
#这里的h共5000行,10列,每行代表一个样本
#每列是预测对应数字的概率
#返回的h_argmax是一个array,包含5000个样本对应的预测值。
raw_x, raw_y = load_data('ex3data1.mat')
x = np.insert(raw_x,0,1,axis=1) #(5000,401)
y = raw_y.flatten()
all_theta = one_vs_all(x,y,1,10)
https://blog.csdn.net/u013713117/article/details/53965572
y_pred = predict_all(x,all_theta)
accuracy = np.mean(y_pred == y)
print('accuracy : {}%'.format(accuracy*100))
accuracy : 94.48%
from scipy.io import loadmat
import numpy as np
def sigmoid(z):
return 1/(1+np.exp(-z))
#有一个输入层一个隐含层一个输出层
def predictNN(theta1,theta2,a1):
z = sigmoid(a1.dot(theta1.T)) #输入层到隐含层的计算
a2 = np.column_stack((np.ones(z.shape[0]),z)) #Add a2 = 1
a3 =sigmoid(a2.dot(theta2.T)) #隐含层到输出层
p = np.argmax(a3, axis=1)
p = p + 1
return p
#返回的p是一个array,包含5000个样本对应的预测值。
weights=loadmat('ex3weights.mat')#读取已经训练好的权重,即参数值
data=loadmat('ex3data1.mat')
X=data['X']
y=data['y']
theta1=weights['Theta1']
theta2=weights['Theta2']
X=np.column_stack((np.ones(X.shape[0]),X)) #加一个x0,为偏执单元
p=predictNN(theta1,theta2,X)
print("train accuracy:{}%".format(np.mean((y.flatten() == p)*100)))
train accuracy:97.52%
参考学习博客:
https://blog.csdn.net/Cowry5/article/details/80367832
https://blog.csdn.net/csdn_inside/article/details/83829420