![深度学习python实现](https://i-blog.csdnimg.cn/blog_migrate/e734965dbb1f360e3b18ffa300943ff2.png)
深度学习python实现
Python深度学习-实现 (Python Deep Learning - Implementations)
In this implementation of Deep learning, our objective is to predict the customer attrition or churning data for a certain bank - which customers are likely to leave this bank service. The Dataset used is relatively small and contains 10000 rows with 14 columns. We are using Anaconda distribution, and frameworks like Theano, TensorFlow and Keras. Keras is built on top of Tensorflow and Theano which function as its backends.
在实施深度学习的过程中,我们的目标是预测特定银行的客户流失或搅动数据-哪些客户可能会退出该银行服务。 使用的数据集相对较小,包含10000行和14列。 我们正在使用Anaconda发行版以及Theano,TensorFlow和Keras之类的框架。 Keras建立在Tensorflow和Theano的后端,后者充当后端。
# Artificial Neural Network
# Installing Theano
pip install --upgrade theano
# Installing Tensorflow
pip install –upgrade tensorflow
# Installing Keras
pip install --upgrade keras
步骤1:资料预处理 (Step 1: Data preprocessing)
In[]:
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the database
dataset = pd.read_csv('Churn_Modelling.csv')
第2步 (Step 2)
We create matrices of the features of dataset and the target variable, which is column 14, labeled as “Exited”.
我们创建数据集特征和目标变量的矩阵,该变量位于第14列,标记为“已退出”。
The initial look of data is as shown below −
数据的初始外观如下所示-
In[]:
X = dataset.iloc[:, 3:13].values
Y = dataset.iloc[:, 13].values
X
输出量 (Output)
![Step Output](https://i-blog.csdnimg.cn/blog_migrate/13d1108645ce60f663bc7217288307b0.png)
第三步 (Step 3)
Y
输出量 (Output)
array([1, 0, 1, ..., 1, 1, 0], dtype = int64)
第4步 (Step 4)
We make the analysis simpler by encoding string variables. We are using the ScikitLearn function ‘LabelEncoder’ to automatically encode the different labels in the columns with values between 0 to n_classes-1.
通过编码字符串变量,我们使分析更加简单。 我们正在使用ScikitLearn函数“ LabelEncoder”自动对列中值在0到n_classes-1之间的不同标签进行编码。
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:,1] = labelencoder_X_1.fit_transform(X[:,1])
labelencoder_X_2 = LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])
X
输出量 (Output)
![Step4 Output](https://i-blog.csdnimg.cn/blog_migrate/c233d8c83ebe309afbcff7b5eca0b981.png)
In the above output,country names are replaced by 0, 1 and 2; while male and female are replaced by 0 and 1.
在上面的输出中,国家名称被0、1和2替换; 而将男性和女性分别替换为0和1。
第5步 (Step 5)
Labelling Encoded Data
标记编码数据
We use the same ScikitLearn library and another function called the OneHotEncoder to just pass the column number creating a dummy variable.
我们使用相同的ScikitLearn库和另一个称为OneHotEncoder的函数来传递列号,从而创建一个虚拟变量。
onehotencoder = OneHotEncoder(categorical features = [1])
X = onehotencoder.fit_transform(X).toarray()
X = X[:, 1:]
X
Now, the first 2 columns represent the country and the 4th column represents the gender.
现在,前两列代表国家,第四列代表性别。
输出量 (Output)
![Step5 Output](https://i-blog.csdnimg.cn/blog_migrate/4c9d4a098b309fac330798a65a004ad5.png)
We always divide our data into training and testing part; we train our model on training data and then we check the accuracy of a model on testing data which helps in evaluating the efficiency of model.
我们始终将数据分为培训和测试部分; 我们在训练数据上训练模型,然后在测试数据上检查模型的准确性,这有助于评估模型的效率。
第6步 (Step 6)
We are using ScikitLearn’s train_test_split function to split our data into training set and test set. We keep the train- to- test split ratio as 80:20.
我们正在使用ScikitLearn的train_test_split函数将数据分为训练集和测试集。 我们将火车与测试的比率保持为80:20。
#Splitting the dataset into the Training set and the Test Set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
Some variables have values in thousands while some have values in tens or ones. We scale the data so that they are more representative.
有些变量的值是数千,而有些变量的值是十或一。 我们缩放数据,以便它们更具代表性。
步骤7 (Step 7)
In this code, we are fitting and transforming the training data using the StandardScaler function. We standardize our scaling so that we use the same fitted method to transform/scale test data.
在此代码中,我们使用StandardScaler函数拟合和转换训练数据。 我们将缩放比例标准化,以便我们使用相同的拟合方法来转换/缩放测试数据。
# Feature Scaling
fromsklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
输出量 (Output)
![step7 output](https://i-blog.csdnimg.cn/blog_migrate/6a65a5e344e38b61db3784fee9322189.png)
The data is now scaled properly. Finally, we are done with our data pre-processing. Now,we will start with our model.
现在,数据已正确缩放。 最后,我们完成了数据预处理。 现在,我们将从模型开始。
步骤8 (Step 8)
We import the required Modules here. We need the Sequential module for initializing the neural network and the dense module to add the hidden layers.
我们在此处导入所需的模块。 我们需要用于初始化神经网络的顺序模块和需要添加隐藏层的密集模块。
# Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense
步骤9 (Step 9)
We will name the model as Classifier as our aim is to classify customer churn. Then we use the Sequential module for initialization.
我们将模型命名为“分类器”,因为我们的目的是对客户流失进行分类。 然后,我们使用顺序模块进行初始化。
#Initializing Neural Network
classifier = Sequential()
第10步 (Step 10)
We add the hidden layers one by one using the dense function. In the code below, we will see many arguments.
我们使用稠密功能一一添加隐藏层。 在下面的代码中,我们将看到许多参数。
Our first parameter is output_dim. It is the number of nodes we add to this layer. init is the initialization of the Stochastic Gradient Decent. In a Neural Network we assign weights to each node. At initialization, weights should be near to zero and we randomly initialize weights using the uniform function. The input_dim parameter is needed only for first layer, as the model does not know the number of our input variables. Here the total number of input variables is 11. In the second layer, the model automatically knows the number of input variables from the first hidden layer.
我们的第一个参数是output_dim 。 这是我们添加到此层的节点数。 init是随机渐变体面的初始化。 在神经网络中,我们为每个节点分配权重。 初始化时,权重应接近零,我们使用统一函数随机初始化权重。 由于模型不知道输入变量的数量,因此仅在第一层需要input_dim参数。 此处,输入变量的总数为11。在第二层中,模型会自动从第一隐藏层中知道输入变量的数目。
Execute the following line of code to addthe input layer and the first hidden layer −
执行以下代码行以添加输入层和第一个隐藏层-
classifier.add(Dense(units = 6, kernel_initializer = 'uniform',
activation = 'relu', input_dim = 11))
Execute the following line of code to add the second hidden layer −
执行以下代码行以添加第二个隐藏层-
classifier.add(Dense(units = 6, kernel_initializer = 'uniform',
activation = 'relu'))
Execute the following line of code to add the output layer −
执行以下代码行添加输出层-
classifier.add(Dense(units = 1, kernel_initializer = 'uniform',
activation = 'sigmoid'))
步骤11 (Step 11)
Compiling the ANN
编译神经网络
We have added multiple layers to our classifier until now. We will now compile them using the compile method. Arguments added in final compilation control complete the neural network.So,we need to be careful in this step.
到目前为止,我们已经在分类器中添加了多层。 现在,我们将使用compile方法对其进行编译 。 在最终编译控件中添加的参数完善了神经网络。因此,在此步骤中我们需要小心。
Here is a brief explanation of the arguments.
这是对参数的简要说明。
First argument is Optimizer.This is an algorithm used to find the optimal set of weights. This algorithm is called the Stochastic Gradient Descent (SGD). Here we are using one among several types, called the ‘Adam optimizer’. The SGD depends on loss, so our second parameter is loss. If our dependent variable is binary, we use logarithmic loss function called ‘binary_crossentropy’, and if our dependent variable has more than two categories in output, then we use ‘categorical_crossentropy’. We want to improve performance of our neural network based on accuracy, so we add metrics as accuracy.
第一个参数是Optimizer,这是一种用于找到最佳权重集的算法。 该算法称为随机梯度下降(SGD) 。 在这里,我们使用几种类型中的一种,称为“亚当优化器”。 SGD取决于损失,因此我们的第二个参数是损失。 如果我们的因变量是二进制,则使用称为'binary_crossentropy'的对数损失函数,如果我们的因变量在输出中具有两个以上的类别,则使用'categorical_crossentropy' 。 我们希望基于准确性来改善神经网络的性能,因此我们添加了度量作为准确性。
# Compiling Neural Network
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
步骤12 (Step 12)
A number of codes need to be executed in this step.
在此步骤中需要执行许多代码。
将ANN拟合到训练集 (Fitting the ANN to the Training Set)
We now train our model on the training data. We use the fit method to fit our model. We also optimize the weights to improve model efficiency. For this, we have to update the weights. Batch size is the number of observations after which we update the weights. Epoch is the total number of iterations. The values of batch size and epoch are chosen by the trial and error method.
现在,我们在训练数据上训练模型。 我们使用拟合方法来拟合我们的模型。 我们还优化权重以提高模型效率。 为此,我们必须更新权重。 批次大小是观察值的数量,之后我们将更新权重。 时代是迭代的总数。 批次大小和时期的值是通过反复试验方法选择的。
classifier.fit(X_train, y_train, batch_size = 10, epochs = 50)
进行预测并评估模型 (Making predictions and evaluating the model)
# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)
预测一个新的观察 (Predicting a single new observation)
# Predicting a single new observation
"""Our goal is to predict if the customer with the following data will leave the bank:
Geography: Spain
Credit Score: 500
Gender: Female
Age: 40
Tenure: 3
Balance: 50000
Number of Products: 2
Has Credit Card: Yes
Is Active Member: Yes
步骤13 (Step 13)
Predicting the test set result
预测测试结果
The prediction result will give you probability of the customer leaving the company. We will convert that probability into binary 0 and 1.
预测结果将给您客户离开公司的可能性。 我们将那个概率转换为二进制0和1。
# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)
new_prediction = classifier.predict(sc.transform
(np.array([[0.0, 0, 500, 1, 40, 3, 50000, 2, 1, 1, 40000]])))
new_prediction = (new_prediction > 0.5)
步骤14 (Step 14)
This is the last step where we evaluate our model performance. We already have original results and thus we can build confusion matrix to check the accuracy of our model.
这是我们评估模型性能的最后一步。 我们已经有了原始结果,因此可以构建混淆矩阵来检查模型的准确性。
Making the Confusion Matrix
制作混乱矩阵
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
print (cm)
输出量 (Output)
loss: 0.3384 acc: 0.8605
[ [1541 54]
[230 175] ]
From the confusion matrix, the Accuracy of our model can be calculated as −
根据混淆矩阵,我们模型的精度可以计算为-
Accuracy = 1541+175/2000=0.858
We achieved 85.8% accuracy, which is good.
我们达到了85.8%的准确度 ,这是很好的。
正向传播算法 (The Forward Propagation Algorithm)
In this section, we will learn how to write code to do forward propagation (prediction) for a simple neural network −
在本节中,我们将学习如何为简单的神经网络编写代码以进行正向传播(预测)-
![Forward Propagation Algorithm](https://i-blog.csdnimg.cn/blog_migrate/1e862712d972eb2ac67edc8806f699ab.png)
Each data point is a customer. The first input is how many accounts they have, and the second input is how many children they have. The model will predict how many transactions the user makes in the next year.
每个数据点都是一个客户。 第一个输入是他们有多少个帐户,第二个输入是他们有多少个孩子。 该模型将预测用户在明年进行的交易次数。
The input data is pre-loaded as input data, and the weights are in a dictionary called weights. The array of weights for the first node in the hidden layer are in weights [‘node_0’], and for the second node in the hidden layer are in weights[‘node_1’] respectively.
输入数据被预加载为输入数据,权重在称为权重的字典中。 隐藏层中第一个节点的权重数组以权重['node_0']表示,隐藏层中第二个节点的权重数组分别以权重['node_1']表示。
The weights feeding into the output node are available in weights.
馈入输出节点的权重可用权重提供。
整流线性激活函数 (The Rectified Linear Activation Function)
An "activation function" is a function that works at each node. It transforms the node's input into some output.
“激活功能”是在每个节点上起作用的功能。 它将节点的输入转换为某些输出。
The rectified linear activation function (called ReLU) is widely used in very high-performance networks. This function takes a single number as an input, returning 0 if the input is negative, and input as the output if the input is positive.
整流的线性激活函数(称为ReLU )广泛用于非常高性能的网络中。 此函数将一个数字作为输入,如果输入为负,则返回0,如果输入为正,则返回输入。
Here are some examples −
这是一些例子-
- relu(4) = 4 relu(4)= 4
- relu(-2) = 0 relu(-2)= 0
We fill in the definition of the relu() function−
我们填写relu()函数的定义-
- We use the max() function to calculate the value for the output of relu(). 我们使用max()函数来计算relu()的输出值。
- We apply the relu() function to node_0_input to calculate node_0_output. 我们将relu()函数应用于node_0_input来计算node_0_output。
- We apply the relu() function to node_1_input to calculate node_1_output. 我们将relu()函数应用于node_1_input以计算node_1_output。
import numpy as np
input_data = np.array([-1, 2])
weights = {
'node_0': np.array([3, 3]),
'node_1': np.array([1, 5]),
'output': np.array([2, -1])
}
node_0_input = (input_data * weights['node_0']).sum()
node_0_output = np.tanh(node_0_input)
node_1_input = (input_data * weights['node_1']).sum()
node_1_output = np.tanh(node_1_input)
hidden_layer_output = np.array(node_0_output, node_1_output)
output =(hidden_layer_output * weights['output']).sum()
print(output)
def relu(input):
'''Define your relu activation function here'''
# Calculate the value for the output of the relu function: output
output = max(input,0)
# Return the value just calculated
return(output)
# Calculate node 0 value: node_0_output
node_0_input = (input_data * weights['node_0']).sum()
node_0_output = relu(node_0_input)
# Calculate node 1 value: node_1_output
node_1_input = (input_data * weights['node_1']).sum()
node_1_output = relu(node_1_input)
# Put node values into array: hidden_layer_outputs
hidden_layer_outputs = np.array([node_0_output, node_1_output])
# Calculate model output (do not apply relu)
odel_output = (hidden_layer_outputs * weights['output']).sum()
print(model_output)# Print model output
输出量 (Output)
0.9950547536867305
-3
将网络应用于许多观测/数据行 (Applying the network to many Observations/rows of data)
In this section, we will learn how to define a function called predict_with_network(). This function will generate predictions for multiple data observations, taken from network above taken as input_data. The weights given in above network are being used. The relu() function definition is also being used.
在本节中,我们将学习如何定义一个名为predict_with_network()的函数。 此函数将针对来自上方网络的多个数据观测值生成预测,作为输入数据。 使用上述网络中给出的权重。 还使用了relu()函数定义。
Let us define a function called predict_with_network() that accepts two arguments - input_data_row and weights - and returns a prediction from the network as the output.
让我们定义一个名为predict_with_network()的函数,该函数接受两个参数(input_data_row和weights),并从网络返回一个预测作为输出。
We calculate the input and output values for each node, storing them as: node_0_input, node_0_output, node_1_input, and node_1_output.
我们计算每个节点的输入和输出值,并将它们存储为:node_0_input,node_0_output,node_1_input和node_1_output。
To calculate the input value of a node, we multiply the relevant arrays together and compute their sum.
为了计算节点的输入值,我们将相关数组相乘并计算它们的总和。
To calculate the output value of a node, we apply the relu()function to the input value of the node. We use a ‘for loop’ to iterate over input_data −
为了计算节点的输出值,我们将relu()函数应用于节点的输入值。 我们使用'for循环'遍历input_data-
We also use our predict_with_network() to generate predictions for each row of the input_data - input_data_row. We also append each prediction to results.
我们还使用predict_with_network()为input_data-input_data_row的每一行生成预测。 我们还将每个预测附加到结果中。
# Define predict_with_network()
def predict_with_network(input_data_row, weights):
# Calculate node 0 value
node_0_input = (input_data_row * weights['node_0']).sum()
node_0_output = relu(node_0_input)
# Calculate node 1 value
node_1_input = (input_data_row * weights['node_1']).sum()
node_1_output = relu(node_1_input)
# Put node values into array: hidden_layer_outputs
hidden_layer_outputs = np.array([node_0_output, node_1_output])
# Calculate model output
input_to_final_layer = (hidden_layer_outputs*weights['output']).sum()
model_output = relu(input_to_final_layer)
# Return model output
return(model_output)
# Create empty list to store prediction results
results = []
for input_data_row in input_data:
# Append prediction to results
results.append(predict_with_network(input_data_row, weights))
print(results)# Print results
输出量 (Output)
[0, 12]
Here we have used the relu function where relu(26) = 26 and relu(-13)=0 and so on.
在这里,我们使用了relu函数,其中relu(26)= 26和relu(-13)= 0等等。
深度多层神经网络 (Deep multi-layer neural networks)
Here we are writing code to do forward propagation for a neural network with two hidden layers. Each hidden layer has two nodes. The input data has been preloaded as input_data. The nodes in the first hidden layer are called node_0_0 and node_0_1.
在这里,我们正在编写代码以对具有两个隐藏层的神经网络进行正向传播。 每个隐藏层都有两个节点。 输入数据已预加载为input_data 。 第一隐藏层中的节点称为node_0_0和node_0_1。
Their weights are pre-loaded as weights['node_0_0'] and weights['node_0_1'] respectively.
它们的权重分别预加载为weights ['node_0_0']和weights ['node_0_1']。
The nodes in the second hidden layer are called node_1_0 and node_1_1. Their weights are pre-loaded as weights['node_1_0'] and weights['node_1_1'] respectively.
第二隐藏层中的节点称为node_1_0和node_1_1 。 它们的权重分别预加载为weights ['node_1_0']和weights ['node_1_1'] 。
We then create a model output from the hidden nodes using weights pre-loaded as weights['output'].
然后,我们使用预加载为weights ['output']的权重从隐藏节点创建模型输出。
![Deep Multi Layer](https://i-blog.csdnimg.cn/blog_migrate/ed95502d67bbaa6d63260ad80b9ad094.png)
We calculate node_0_0_input using its weights weights['node_0_0'] and the given input_data. Then apply the relu() function to get node_0_0_output.
我们使用权重weights ['node_0_0']和给定的input_data来计算node_0_0_input。 然后应用relu()函数获取node_0_0_output。
We do the same as above for node_0_1_input to get node_0_1_output.
我们对node_0_1_input进行与上述相同的操作,以获取node_0_1_output。
We calculate node_1_0_input using its weights weights['node_1_0'] and the outputs from the first hidden layer - hidden_0_outputs. We then apply the relu() function to get node_1_0_output.
我们使用权重weights ['node_1_0']和第一个隐藏层的输出-hidden_0_outputs来计算node_1_0_input。 然后,我们应用relu()函数来获取node_1_0_output。
We do the same as above for node_1_1_input to get node_1_1_output.
我们对node_1_1_input进行与上述相同的操作,以获取node_1_1_output。
We calculate model_output using weights['output'] and the outputs from the second hidden layer hidden_1_outputs array. We do not apply the relu()function to this output.
我们使用weights ['output']和第二个隐藏层hidden_1_outputs数组的输出来计算model_output。 我们不将relu()函数应用于此输出。
![Multi Hidden Layer](https://i-blog.csdnimg.cn/blog_migrate/acdb9b3bd5dd4b1282fe22cc8e29643a.png)
import numpy as np
input_data = np.array([3, 5])
weights = {
'node_0_0': np.array([2, 4]),
'node_0_1': np.array([4, -5]),
'node_1_0': np.array([-1, 1]),
'node_1_1': np.array([2, 2]),
'output': np.array([2, 7])
}
def predict_with_network(input_data):
# Calculate node 0 in the first hidden layer
node_0_0_input = (input_data * weights['node_0_0']).sum()
node_0_0_output = relu(node_0_0_input)
# Calculate node 1 in the first hidden layer
node_0_1_input = (input_data*weights['node_0_1']).sum()
node_0_1_output = relu(node_0_1_input)
# Put node values into array: hidden_0_outputs
hidden_0_outputs = np.array([node_0_0_output, node_0_1_output])
# Calculate node 0 in the second hidden layer
node_1_0_input = (hidden_0_outputs*weights['node_1_0']).sum()
node_1_0_output = relu(node_1_0_input)
# Calculate node 1 in the second hidden layer
node_1_1_input = (hidden_0_outputs*weights['node_1_1']).sum()
node_1_1_output = relu(node_1_1_input)
# Put node values into array: hidden_1_outputs
hidden_1_outputs = np.array([node_1_0_output, node_1_1_output])
# Calculate model output: model_output
model_output = (hidden_1_outputs*weights['output']).sum()
# Return model_output
return(model_output)
output = predict_with_network(input_data)
print(output)
输出量 (Output)
364
翻译自: https://www.tutorialspoint.com/python_deep_learning/python_deep_learning_implementations.htm
深度学习python实现