binaryclassification

#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')
df.head()
  id RI Na Mg Al Si K Ca Ba Fe class
0 1 1.52101 13.64 4.49 1.10 71.78 0.06 8.75 0.0 0.0 1
1 2 1.51761 13.89 3.60 1.36 72.73 0.48 7.83 0.0 0.0 1
2 3 1.51618 13.53 3.55 1.54 72.99 0.39 7.78 0.0 0.0 1
3 4 1.51766 13.21 3.69 1.29 72.61 0.57 8.22 0.0 0.0 1
4 5 1.51742 13.27 3.62 1.24 73.08 0.55 8.07 0.0 0.0 1
X,y=df.iloc[:,1:-1],df.iloc[:,-1]
X,y=np.array(X),np.array(y)

#change the value the element

for idx,class_name in enumerate(sorted(list(set(y)))):
y[y==class_name]=idx

y
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5], dtype=int64)
#make the matrix's elements 2 value
#if element doesn't equals to 1 then make it 0
#'1' stands for the '2' class for i in range(len(y)):
if y[i]!=1:
y[i]=0
#split our training dataset randomly

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=44)
X_train.shape,y_train.shape,X_test.shape,y_test.shape
((181, 9), (181,), (33, 9), (33,))
f_mean=np.mean(X_train,axis=0)
f_std=np.std(X_train,axis=0)
f_mean,f_std
(array([1.51832884e+00, 1.33736464e+01, 2.69287293e+00, 1.46425414e+00,
7.26391160e+01, 5.17016575e-01, 8.95314917e+00, 1.71104972e-01,
6.02762431e-02]),
array([0.00300427, 0.79769555, 1.42353328, 0.49169919, 0.77056863,
0.69105168, 1.42892902, 0.5002639 , 0.10131419]))
#standardize training set

X_train=(X_train-f_mean)/f_std
X_test=(X_test-f_mean)/f_std
theta = np.zeros((X_train.shape[1] + 1))
theta.shape
(10,)
#add 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)
X_train.shape,X_test.shape,theta.shape
((181, 10), (33, 10), (10,))
#initialize the parameter

np.random.seed(42)
theta = np.random.rand(*theta.shape)
theta
array([0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864,
0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258])
#cross_entropy_loss: loss function
#h: hypothesis function
#gradient: gradient function num_epoch=500000
for epoch in range(num_epoch):
logist = np.dot(X_train, theta)
h = 1 / (1 + np.exp(-logist))
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 = theta - 0.01*gradient
if epoch%100000==0:
print('Epoch={}\tLoss={}'.format(epoch,cross_entropy_loss))
Epoch=0	Loss=0.9770836920534414
Epoch=100000 Loss=0.5884129057196792
Epoch=200000 Loss=0.5828823869347305
Epoch=300000 Loss=0.5798937167992417
Epoch=400000 Loss=0.5782071252958373
h_test = 1 / (1 + np.exp(-np.dot(X_test, theta)))

#accurancy
((h_test > 0.5) == y_test).sum() / y_test.size
0.8484848484848485

logistics二分类的更多相关文章

  1. R数据分析:二分类因变量的混合效应,多水平logistics模型介绍

    今天给大家写广义混合效应模型Generalised Linear Random Intercept Model的第一部分 ,混合效应logistics回归模型,这个和线性混合效应模型一样也有好几个叫法 ...

  2. 【原】Spark之机器学习(Python版)(二)——分类

    写这个系列是因为最近公司在搞技术分享,学习Spark,我的任务是讲PySpark的应用,因为我主要用Python,结合Spark,就讲PySpark了.然而我在学习的过程中发现,PySpark很鸡肋( ...

  3. Kaggle实战之二分类问题

    0. 前言 1. MNIST 数据集 2. 二分类器 3. 效果评测 4. 多分类器与误差分析 5. Kaggle 实战 0. 前言 "尽管新技术新算法层出不穷,但是掌握好基础算法就能解决手 ...

  4. 准确率(Accuracy), 精确率(Precision), 召回率(Recall)和F1-Measure(对于二分类问题)

    首先我们可以计算准确率(accuracy),其定义是: 对于给定的测试数据集,分类器正确分类的样本数与总样本数之比.也就是损失函数是0-1损失时测试数据集上的准确率. 下面在介绍时使用一下例子: 一个 ...

  5. 监督学习——logistic进行二分类(python)

    线性回归及sgd/bgd的介绍: 监督学习--随机梯度下降算法(sgd)和批梯度下降算法(bgd) 训练数据形式:          (第一列代表x1,第二列代表 x2,第三列代表 数据标签 用 0/ ...

  6. keras实现简单性别识别(二分类问题)

    keras实现简单性别识别(二分类问题) 第一步:准备好需要的库 tensorflow  1.4.0 h5py 2.7.0 hdf5 1.8.15.1 Keras     2.0.8 opencv-p ...

  7. Logistic回归二分类Winner or Losser----台大李宏毅机器学习作业二(HW2)

    一.作业说明 给定训练集spam_train.csv,要求根据每个ID各种属性值来判断该ID对应角色是Winner还是Losser(0.1分类). 训练集介绍: (1)CSV文件,大小为4000行X5 ...

  8. matlab-逻辑回归二分类(Logistic Regression)

    逻辑回归二分类 今天尝试写了一下逻辑回归分类,把代码分享给大家,至于原理的的话请戳这里 https://blog.csdn.net/laobai1015/article/details/7811321 ...

  9. tensorflow实现二分类

    读万卷书,不如行万里路.之前看了不少机器学习方面的书籍,但是实战很少.这次因为项目接触到tensorflow,用一个最简单的深层神经网络实现分类和回归任务. 首先说分类任务,分类任务的两个思路: 如果 ...

随机推荐

  1. 【学习备份】ajax添加小例子

    实现目的:保存模板编号.模板名称到数据库 1.js引用 <script src="../../../../../../../js/jquery-1.10.2.min.js"& ...

  2. ubuntu设置窗口最大化

    三行命令搞定Ubuntu 16.04下安装VMware Tools!!!!!!!!! 由于下载的是ubuntu-16.04.3-desktop-amd64,需要安装vmware tools,以往提取的 ...

  3. BZOJ 2946 [Poi2000]公共串 (二分+Hash/二分+后缀数组/后缀自动机)

    求多串的最长公共字串. 法1: 二分长度+hash 传送门 法2: 二分+后缀数组 传送门 法3: 后缀自动机 拿第一个串建自动机,然后用其他串在上面匹配.每次求出SAM上每个节点的最长匹配长度后,再 ...

  4. lombok使用及常用注解

    简介 大部分项目中都必不可少的包含数据库实体(Entity).数据载体(dto,dataObject),而这两部分都包含着大量的没有业务逻辑的setter.getter.空参构造,同时我们一般要复写类 ...

  5. [人物存档]【AI少女】【捏脸数据】精灵

    AISChaF_20191101204710076.png

  6. PHP回顾(面向对象)

    类中的成员属性不能够用函数为其赋值.public age = rand(1,100);//这是错误的: __get()   __set()  __isset() __unset() final 用来修 ...

  7. .NET面试题系列(二十一)C#中Equals和==的比较

    序言 值类型的比较 ; ; Console.WriteLine("Equals和= =(等于号)的比较"); Console.WriteLine("i.Equals(j) ...

  8. Fantasy of a Summation (LightOJ - 1213)(快速幂+简单思维)

    题解:根据题目给的程序,就是计算给的这个序列,进行k次到n的循环,每个数需要加的次数是k*n^(k-1),所以快速幂取模,算计一下就可以了. #include <bits/stdc++.h> ...

  9. Java集合框架之LinkedHashSet

    简述 LinkedHashSet底层使用 LinkedHashMap 来保存所有元素,它继承自 HashSet,其所有的方法操作上又与 HashSet 相同,因此 LinkedHashSet 的实现上 ...

  10. struct streaming中的监听器StreamingQueryListener

    在struct streaming提供了一个类,用来监听流的启动.停止.状态更新 StreamingQueryListener 实例化:StreamingQueryListener 后需要实现3个函数 ...