实现了一个神经网络-二进制分类
让我们在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)