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 不知道这个数据集干什么用的,根据直 ...
随机推荐
- Python模拟浏览器前进后退操作
# 模拟浏览器前进后退操作 # 代码中引入selenium版本为:3.4.3 # 通过Chrom浏览器访问发起请求 # Chrom版本:59 ,chromdriver:2.3 # 需要对应版本的Chr ...
- @SpringQueryMap注解 feign的get传参方式(转)
spring cloud项目使用feign的时候都会发现一个问题,就是get方式无法解析对象参数.其实feign是支持对象传递的,但是得是Map形式,而且不能为空,与spring在机制上不兼容,因此无 ...
- Python的f.seek(offset, whence)函数
file.seek()方法标准格式是:seek(offset,whence=0)offset:开始的偏移量,也就是代表需要移动偏移的字节数whence:给offset参数一个定义,表示要从哪个位置开始 ...
- Gym 100971A Treasure Island BFS 思维题
A - Treasure Island Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- codeforces297B
Fish Weight CodeForces - 297B It is known that there are k fish species in the polar ocean, numbered ...
- POJ1990--POJ 1990 MooFest(树状数组)
Time Limit: 1000MSMemory Limit: 30000K Total Submissions: 8141Accepted: 3674 Description Every year, ...
- State Threads之co-routine的创建和stack的管理
1. 综述 协程库 State Threads Library 是一个基于 setjmp/longjmp 实现的 C 语言版用户线程库或协程库(user level thread). 基本协程例子: ...
- 黑马vue---13、事件修饰符的介绍
黑马vue---13.事件修饰符的介绍 一.总结 一句话总结: .stop 阻止冒泡:input type="button" value="戳他" @click ...
- EBS 修改系统颜色
1)修改 配置文件: Java 色彩设计,选择相应的颜色 2)清理高速缓存 注:如果不清理缓存,则要等15分钟后才显示变成新设定的颜色
- jxbrowser 实现自定义右键菜单
https://blog.csdn.net/shuaizai88/article/details/73743691 public static void main(String[] args) { J ...