神经网络图像二分类

 

实现了一个神经网络-二进制分类

让我们在python中实现一个用于二进制分类的基本神经网络,该神经网络用于在给定图像为0或1时进行分类。

In [277]:

 

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
%matplotlib inline
import itertools
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn import  metrics

2.1数据准备

第一步是加载和准备数据集

数据来源:https://github.com/woshizhangrong/train_raw

In [278]:

 

train = pd.read_csv("E:/Digit_Recognizer/train1.csv")

In [279]:

 

sns.countplot(train['label'])

Out[279]:

<matplotlib.axes._subplots.AxesSubplot at 0x243f59e8>

In [280]:

 

# Check for null and missing values
train.isnull().any().describe()

Out[280]:

count       785
unique        1
top       False
freq        785
dtype: object

我检查损坏的图像(里面缺少值)。

在训练和测试数据集中没有缺失值。所以我们可以安全的继续。

In [281]:

 

# include only the rows having label = 0 or 1 (binary classification)
X = train[train['label'].isin([0, 1])]
# target variable
Y = train[train['label'].isin([0, 1])]['label']
# remove the label from X
X = X.drop(['label'], axis = 1)

2.2实现激活函数

我们将使用sigmoid激活函数,因为它输出0和1之间的值,所以这是一个很好的选择,对于二元分类问题

In [282]:

 

# implementing a sigmoid activation function
def sigmoid(z):
    s = 1.0/ (1 + np.exp(-z))    
    return s

2.3定义神经网络架构

创建一个有三层的模型——输入,隐藏,输出。

In [283]:

 

def network_architecture(X, Y):
    # nodes in input layer
    n_x = X.shape[0] 
    # nodes in hidden layer
    n_h = 10          
    # nodes in output layer
    n_y = Y.shape[0] 
    return (n_x, n_h, n_y)
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值