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 不知道这个数据集干什么用的,根据直 ...
随机推荐
- tqdm模块
tqdm 是 Python 进度条库. tqdm库下面有2个类我们经常使用: 1. 2. 可以在 Python 长循环中添加一个进度提示信息用法:tqdm(iterator) trange(i) 是 ...
- Acwing-204-表达整数的奇怪方式(扩展中国剩余定理)
链接: https://www.acwing.com/problem/content/206/ 题意: 给定2n个整数a1,a2,-,an和m1,m2,-,mn,求一个最小的非负整数x,满足∀i∈[1 ...
- jpa多对一映射
1.插入 建一个部门类Dept和一个员工类Emp: Emp对Dept是多对一的关系:因为一个部门有多个员工,而一个员工只有一个部门: Emp类中添加一个Dept的属性: @ManyToOne注解表 ...
- 利用msyqlfont + plsql 客户端 完成msyql数据向oracle的转移
方法一: 1.这是mysqlfont 连接工具 ,选中表右键点击 输出->csv文件 2.选择导出的文件为ANSI型,因为csv文件excel打开的默认编码方式为ANSI这样可以防止中文在exc ...
- vue中改变数组或对象,页面没做出对应的渲染
原文链接 数组更新检测 变异方法 Vue 包含一组观察数组的变异方法,所以它们也将会触发视图更新.这些方法如下: push() pop() shift() unshift() splice() sor ...
- Linux之zookeeper开机启动
1.用cd 命令切换到/etc/rc.d/init.d/目录下 [root@bogon ~]# cd /etc/rc.d/init.d 2.用touch zookeeper创建一个文件 [root@b ...
- HDU 5867 Water problem ——(模拟,水题)
我发这题只是想说明:有时候确实需要用水题来找找自信的~ 代码如下: #include <stdio.h> #include <algorithm> #include <s ...
- light4j轻量级微服务应用
最近对light-4j轻框架比较感兴趣,于是对现有应用做了一次重构,现将其间的一些点滴所得分享出来. 项目打包 pom.xml配置了两个profile:debug支持mvn exec:exec启动应用 ...
- Thymeleaf Multiple Template Locations using Spring Boot
1. Overview In this tutorial, we'll see how we can define multiple template locations using Thymelea ...
- Mybatis框架学习1:入门
一框架介绍 1.Mybatis介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google c ...