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. 运行一次node服务后,再次运行报错

    由于工作内容在git上,而系统又是window,大家知道,window自带的终端是不能进行git操作的,所以下载了一个git进行代码的更新提交. 我在git上运行了node服务后,由于不同的项目,我需 ...

  2. 【leetcode】1278. Palindrome Partitioning III

    题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...

  3. BZOJ 1923: [Sdoi2010]外星千足虫 高斯消元+bitset

    高斯消元求解异或方程组,可以多学一下 $bitset$ 在位运算中的各种神奇操作. #include <cstdio> #include <bitset> #define N ...

  4. Battle ships HDU - 5093二分匹配

    Battle shipsHDU - 5093 题目大意:n*m的地图,*代表海洋,#代表冰山,o代表浮冰,海洋上可以放置船舰,但是每一行每一列只能有一个船舰(类似象棋的車),除非同行或者同列的船舰中间 ...

  5. Codevs 4373 窗口(线段树 单调队列 st表)

    4373 窗口 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只 ...

  6. Nginx配置中的log_format用法梳理

    nginx服务器日志相关指令主要有两条,一条是log_format,用来设置日志格式,另外一条是access_log,用来指定日志文件的存放路径.格式和缓存大小,一般在nginx的配置文件中日记配置( ...

  7. Konrad and Company Evaluation

    F. Konrad and Company Evaluation 参考:[codeforces 1230F]Konrad and Company Evaluation-暴力 思路:题意分析见参考博客. ...

  8. c# 调用CMD命令并获取输出结果

    private static string CMDPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + " ...

  9. java获取来访者mac信息

    根据IP获取对应的Mac地址,支持win10+Linux package com.simonjia.util.other; /** * @Author: SimonHu * @Date: 2019/6 ...

  10. Why are C# structs immutable?

    Compiler Error CS1612 Cannot modify the return value of 'expression' because it is not a variable cl ...