利用logistic回归解决多分类问题
利用logistic回归解决手写数字识别问题,数据集私聊。
from scipy.io import loadmat
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import minimize data = loadmat('ex3data1.mat') data_row = data['X'].shape #5000个200*200的矩阵,表示5000个手写数字
data_cols = data['y'].shape #5000个结果 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) first = np.multiply(-y, np.log(sigmoid(X * theta.T)))
second = np.multiply((1 - y), np.log(1 - sigmoid(X * theta.T)))
reg = (learningrate / (2 * len(X))) * np.sum(np.power(theta[:, 1:theta.shape[1]], 2))
return np.sum(first - second) / len(X) + reg def gradientReg(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)
grad[0, 0] = np.sum(np.multiply(error, X[:, 0])) / len(X) return np.array(grad).ravel() 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前加入一列1
X = np.insert(X, 0, values=np.ones(rows), axis=1) 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)) #寻找此分类器的最优参数
fmin = minimize(fun=cost, x0=theta, args=(X, y_i, learning_rate), method='TNC', jac=gradientReg)
all_theta[i-1, :] = fmin.x return all_theta num_labels = 10
learningRate = 1
rows = data['X'].shape[0]
params = data['X'].shape[1] X = np.insert(data['X'], 0, values=np.ones(rows), axis=1) theta = np.zeros(params + 1) def predict_all(X, all_theta): #在矩阵X前加入一列1
X = np.insert(X, 0, values=np.ones(rows), axis=1) X = np.matrix(X) all_theta = np.matrix(all_theta) h = sigmoid(X * all_theta.T)
h_argmax = np.argmax(h, axis=1) #在行方向寻找最大值
h_argmax = h_argmax + 1
return h_argmax all_theta = one_vs_all(data['X'], data['y'], num_labels, 1) y_predict = predict_all(data['X'], all_theta)
correct = [1 if a == b else 0 for (a, b) in zip(y_predict, data['y'])]
accuracy = (sum(correct) / float(len(correct)))
print('accuracy = {0}%'.format(accuracy * 100))
利用logistic回归解决多分类问题的更多相关文章
- Spark2.0机器学习系列之4:Logistic回归及Binary分类(二分问题)结果评估
参数设置 α: 梯度上升算法迭代时候权重更新公式中包含 α : http://blog.csdn.net/lu597203933/article/details/38468303 为了更好理解 α和 ...
- NLP之基于logistic回归的文本分类
数据集下载: 链接:https://pan.baidu.com/s/17EL37CQ-FtOXhtdZHQDPgw 提取码:0829 逻辑斯蒂回归 @ 目录 逻辑斯蒂回归 1.理论 1.1 多分类 1 ...
- 《转》Logistic回归 多分类问题的推广算法--Softmax回归
转自http://ufldl.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD%92 简介 在本节中,我们介绍Softmax回归模型,该模型是log ...
- 线性回归,logistic回归分类
学习过程 下面是一个典型的机器学习的过程,首先给出一个输入数据,我们的算法会通过一系列的过程得到一个估计的函数,这个函数有能力对没有见过的新数据给出一个新的估计,也被称为构建一个模型.就如同上面的线性 ...
- Softmax回归——logistic回归模型在多分类问题上的推广
Softmax回归 Contents [hide] 1 简介 2 代价函数 3 Softmax回归模型参数化的特点 4 权重衰减 5 Softmax回归与Logistic 回归的关系 6 Softma ...
- 机器学习实战之logistic回归分类
利用logistic回归进行分类的主要思想:根据现有数据对分类边界建立回归公式,并以此进行分类. logistic优缺点: 优点:计算代价不高,易于理解和实现.缺点:容易欠拟合,分类精度可能不高. . ...
- Logistic回归分析之多分类Logistic回归
Logistic回归分析(logit回归)一般可分为3类,分别是二元Logistic回归分析.多分类Logistic回归分析和有序Logistic回归分析.logistic回归分析类型如下所示. Lo ...
- 第五章:Logistic回归
本章内容 □sigmod函数和logistic回归分类器 □最优化理论初步□梯度下降最优化算法□数据中的缺失项处理 这会是激动人心的一章,因为我们将首次接触到最优化算法.仔细想想就会发现,其实我们日常 ...
- 对线性回归,logistic回归和一般回归的认识
原文:http://www.cnblogs.com/jerrylead/archive/2011/03/05/1971867.html#3281650 对线性回归,logistic回归和一般回归的认识 ...
随机推荐
- CQRS(Command and Query Responsibility Segregation)与EventSources实例
CQRS The CQRS pattern and event sourcing are not mere simplistic solutions to the problems associate ...
- sqliteman
2.安装文件 采用源码方式安装 可用下面地址自行下载 https://sourceforge.net/projects/sqliteman/files/sqliteman/1.2.2/ 3.安装 1) ...
- AcWing 32. 调整数组顺序使奇数位于偶数前面
习题地址 https://www.acwing.com/solution/acwing/content/2921/ 输入一个整数数组,实现一个函数来调整该数组中数字的顺序. 使得所有的奇数位于数组的前 ...
- 第04组 Alpha冲刺(1/4)
队名:斗地组 组长博客:地址 作业博客:Alpha冲刺(1/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.安排好各个组员的任务 2.收集各个组员的进度 3.写页面 4.写博客 展示Gi ...
- import和from...import
目录 一.import 模块名 二.from 模块名 import 具体的功能 三.import和from...import...的异同 一般使用import和from...import...导入模块 ...
- 完美解决MacOS catalina 升级后Vmware黑屏的问题
完美解决MacOS catalina 升级后VMware黑屏 1.关闭MacOS的rootless机制 #Rootless机制将成为对抗恶意程序的最后防线 1.尝试关闭Rootless,重启按住 Co ...
- webwork遍历数组标签
WebWork中提供了一个<ww:iterator></ww:iterator>标签用于遍历数组. 01 如果数组中是普通类型,比如String.int等类型,可以通过标签中的 ...
- 前端之CSS1
CSS基本语法和引入方式 CSS介绍 为了让网页元素的样式更加丰富,也为了让网页的内容和样式能拆分开,CSS由此而诞生,CSS是 Cascading Style Sheets 的首字母缩写,意思是层叠 ...
- 3DES对称加密算法(ABAP 语言实现版)
公司人事数据要求在系统间加密传输,而对接系统大部分是Java系统,要在不同的异构系统间能很好的加解密码,想到了标准的对称加密算法DES,因为是标准的算法,网络上存在大量公开用Java的DES算法,JA ...
- Android中几种常用的定时器和延时方法
通过实际项目的练习,掌握了几种android基本定时器和延时的用法,这里我想总结一下作为自己的收获,下面列出的是比较简洁的模式,方便简单地在程序中直接调用. 一.三种常用的定时器 1.Handler类 ...