logistics多分类
multiclassification
#DATASET: https://archive.ics.uci.edu/ml/datasets/Glass+Identification
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import sklearn
import sklearn.preprocessing as pre
df=pd.read_csv('data\glassi\glass.data')
X,y=df.iloc[:,1:-1],df.iloc[:,-1]
X,y=np.array(X),np.array(y)
for idx,class_name in enumerate(sorted(list(set(y)))):
y[y==class_name]=idx
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.15,random_state=66)
f_mean, f_std = np.mean(X_train, axis=0), np.std(X_train, axis=0)
X_train = (X_train - f_mean) / f_std
X_test = (X_test - f_mean) / f_std #add a constant parameter
X_train = np.concatenate((np.ones((X_train.shape[0], 1)), X_train), axis=1)
X_test = np.concatenate((np.ones((X_test.shape[0], 1)), X_test), axis=1)
#gradient descent function def get_classifier(X_train,y_train,num_epoch=10000,alpha=0.01):
theta=np.zeros(X_train.shape[1])
for epoch in range(num_epoch):
logist=np.dot(X_train,theta)
h=1/(1+np.exp(-logist)) #hypothesis function
cross_entropy_loss=(-y_train*np.log(h)-(1-y_train)*np.log(1-h)).mean()
gradient=np.dot((h-y_train),X_train)/y_train.size
theta-=alpha*gradient #update
return theta
def multi_classifier(X_train,y_train):
num_class=np.unique(y_train)
parameter=np.zeros((len(num_class),X_train.shape[1])) #each has an array of parameters
for i in num_class:
label_t=np.zeros_like(y_train) #use label_t to label the target class!!!
num_class=np.unique(y_train)
label_t[y_train==num_class[i]]=1 #important,
parameter[i,:]=get_classifier(X_train,label_t) #each array stands for one class's parameter
return parameter
params = multi_classifier(X_train, y_train)
def pred(parameter,X_test,y_test):
f_size=X_test.shape
l_size=y_test.shape
assert (f_size[0]==l_size[0])
logist=np.dot(X_test,np.transpose(parameter)).squeeze()
prob=1/(1+np.exp(-logist))
pred=np.argmax(prob,axis=1)
accuracy = np.sum(pred == y_test) / l_size[0] * 100
return prob, pred, accuracy
_, preds, accu = pred(params, X_test, y_test)
print("Prediction: {}\n".format(preds))
print("Accuracy: {:.3f}%".format(accu))
Prediction: [0 1 0 4 1 5 1 0 0 1 0 1 0 0 5 1 1 1 1 0 5 4 0 1 5 0 0 1 1 0 3 1 0] Accuracy: 66.667%
logistics多分类的更多相关文章
- logistics二分类
binaryclassification #DATASET: https://archive.ics.uci.edu/ml/datasets/Glass+Identificationimport nu ...
- sklearn多分类问题
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- Python_sklearn机器学习库学习笔记(三)logistic regression(逻辑回归)
# 逻辑回归 ## 逻辑回归处理二元分类 %matplotlib inline import matplotlib.pyplot as plt #显示中文 from matplotlib.font_m ...
- R数据分析:二分类因变量的混合效应,多水平logistics模型介绍
今天给大家写广义混合效应模型Generalised Linear Random Intercept Model的第一部分 ,混合效应logistics回归模型,这个和线性混合效应模型一样也有好几个叫法 ...
- 多分类Logistics回归公式的梯度上升推导&极大似然证明sigmoid函数的由来
https://blog.csdn.net/zhy8623080/article/details/73188671 也即softmax公式
- 机器学习实战4:Adaboost提升:病马实例+非均衡分类问题
Adaboost提升算法是机器学习中很好用的两个算法之一,另一个是SVM支持向量机:机器学习面试中也会经常提问到Adaboost的一些原理:另外本文还介绍了一下非平衡分类问题的解决方案,这个问题在面试 ...
- 笔记+R︱Logistics建模简述(logit值、sigmoid函数)
本笔记源于CDA-DSC课程,由常国珍老师主讲.该训练营第一期为风控主题,培训内容十分紧凑,非常好,推荐:CDA数据科学家训练营 ---------------------------------- ...
- 笔记︱风控分类模型种类(决策、排序)比较与模型评估体系(ROC/gini/KS/lift)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 本笔记源于CDA-DSC课程,由常国珍老师主讲 ...
- logistics回归简单应用(二)
警告:本文为小白入门学习笔记 网上下载的数据集链接:https://pan.baidu.com/s/1NwSXJOCzgihPFZfw3NfnfA 密码: jmwz 不知道这个数据集干什么用的,根据直 ...
随机推荐
- 网络编程-UDP echo server
1. UDP简介 UDP 和TCP 的区别包括 1. 面向字节流和面向报文 2. TCP必须要建立连接后才能进行数据交换,但是UDP则并没有连接的建立和释放过程.面向字节流说明,tcp报文段(segm ...
- Linux硬盘安装步骤
网上找了许多用DVD镜像硬盘安装FC5的文章,可是都不系统,为了全中国的广大菜鸟们,云计算架构师 抽了很多时间来写这篇详细的安装文章,希望对初次接触LINUX或者刚刚入门的朋友有所帮助. 一.预备知识 ...
- [CCTF] pwn350
0x00: 之前打了CCTF,在CCTF的过程中遇到一个比较有意思的思路,记录一下. 0x01: 可以看到,这是一个 fmt 的漏洞,不过很简单,接收的输入都在stack中,可以确定输入在栈中的位置, ...
- 一个关于STL list使用 小示例
#include <list> #include <string> using namespace std; typedef struct DiskInfo_st { int ...
- noi.ac #543 商店
我们考虑可并堆维护,从深到浅贪心选取. 用priority_queue启发式合并的话,是60pts: #include<iostream> #include<cstdio> # ...
- 两个线程,一个线程打印1~52,另一个线程打印字母A-Z,打印顺序为12A34B56C……5152Z
使用wait,notify实现 public class Test { public synchronized void a() { for (int i = 1; i <= 52; i++) ...
- thinkphp is NULL表达式写法
thinkphp 中如果这样写 $where['status']=array('EQ','NULL'),打印出来sql是WHERE ( `status` = 'NULL' ):而我想要的是 `sta ...
- JavaWeb_(Hibernate框架)Hibernate中事务
Hibernate中事务 事务的性质 事物的隔离级别 配置事务的隔离级别 事务的性质 原子性:原子,不可再分,一个操作不能分为更小的操作,要么全都执行,要么全不执行. 一致性:事务在完成时,必须使得所 ...
- 初学 Nginx (一) SSI 的作用
SSI:Server Side Include,是一种基于服务端的网页制作技术, Nginx ssi 的例子如下: It took a little while to figure this out ...
- DRL Hands-on book
代码:https://github.com/PacktPublishing/Deep-Reinforcement-Learning-Hands-On Chapter 1 What is Reinfor ...