<jupyter_start><jupyter_text>Vectorization<jupyter_code>#Import library
import numpy
import time
x = numpy.random.rand(100000000)
y = numpy.random.rand(100000000)<jupyter_output><empty_output><jupyter_text>We want to perform dot product between x and y. dot product refered by x.y.x = [x1,x2,x3]y = [y1,y2,y3]dot product can be calculated asx.y = x1*y1 + x2*y2 + x3*y3 Now first check non vectorized scaler version of the task.<jupyter_code>time1 = time.time()
sum = 0
for i in range(len(y)):
    sum = sum+x[i]*y[i]
time2 = time.time()
print('Time taken by scaler program :',str(time2-time1))
print('Sum value :', str(sum))<jupyter_output>Time taken by scaler program : 68.59447741508484
Sum value : 24999772.4968<jupyter_text>Now we check vectorized version available in numpy library<jupyter_code>time1 = time.time()
sum = 0
sum = numpy.dot(x,y)
time2 = time.time()
print('Time taken by scaler program :',str(time2-time1))
print('Sum value :', str(sum))
#Creating ndarray
#Create one dimensional  array
arrlist = [1,2,3,4]
arrNumpy = numpy.array(arrlist)
#Multi dimension array
arrlist2 = [[1,2,3,4],[5,6,7,8]]
arrNumpy2 = numpy.array(arrlist2)
#Print dimention of array
print(arrNumpy2.ndim)

#Print shape of array
print(arrNumpy2.shape)
#Create array of zeros 
zero_array = numpy.zeros(10)
print('Zero Array ',zero_array)

#Create array of ones
ones_array = numpy.ones(10)
print('Ones Array', ones_array)

#Create empty array
empty_array = numpy.empty(10)
print('Empty Array', empty_array)
#Create array of numbers
#Generate numbers from 0 upto length 10
numpy.arange(10)

#Generate numbers between a range
numpy.arange(-5,14)

#Generate numbers with a fixed distance between them
numpy.arange(3,6,0.2)<jupyter_output><empty_output><jupyter_text>Indexing and Slicing<jupyter_code>#Indexing and slicing on 1D array
array = numpy.arange(15)
print(array)
print(array[2])
print(array[2:5])
array[3:9] = 34
print(array)
array2 = numpy.array([[1,2,3],[7,8,9]])
print(array2)
print(array2[1])
print(array2[1][1])
print(array2[1,1])
#More example on slicing
array = numpy.arange(10)
array2 = array2 = numpy.array([[1,2,3],[7,8,9],[13,14,15]])
print(array[3:])
print(array2[1:])
print(array2[1:,1:])<jupyter_output>[3 4 5 6 7 8 9]
[[ 7  8  9]
 [13 14 15]]
[[ 8  9]
 [14 15]]<jupyter_text>Boolean Indexing<jupyter_code>names = ['Abhishek','Ankita','Vijay','Aditya','John','Ankita']
names_array = numpy.array(names)

#Check where the condition is true
print(names_array=='Vijay')

#Include names which satisfy the condition
namelist = names_array[names_array=='Ankita']
print(namelist)

array = numpy.arange(15)
large10  = array[array>10]
print(large10)<jupyter_output>[False False  True False False False]
['Ankita' 'Ankita']
[11 12 13 14]<jupyter_text>Fast Element wise functions on array<jupyter_code>array = numpy.arange(-5,5)

#Absolute values of element
numpy.abs(array)

#Square root of each element
numpy.sqrt(numpy.abs(array))

#Square
numpy.square(array)

#Exponent
numpy.exp(array)

#Logrithm
numpy.log(numpy.abs(array))
numpy.log10(numpy.abs(array))

#Sign
numpy.sign(array)

#Ceil and Floor
numpy.ceil(array)
numpy.floor(array)

#Checking for infinite
numpy.isinf(numpy.log(numpy.abs(array)))

arrayTrig = numpy.array([0,45,90,180,270,370])

#trigonometric  functions
numpy.sin(arrayTrig)
numpy.cos(arrayTrig)
numpy.tan(arrayTrig)
numpy.sinh(arrayTrig)
numpy.cosh(arrayTrig)
numpy.tanh(arrayTrig)

array1 = numpy.arange(1,60,3)
array2 = numpy.arange(1,80,4)

#Add two array (element wise)
numpy.add(array1,array2)

#Substract two array
numpy.subtract(array2,array1)

#Multiply two array
numpy.multiply(array1,array2)

#Divide
numpy.divide(array2,array1)

#Power
numpy.power(array2,array1)

#return element wise maximum
numpy.maximum(array2,array1)

#return element wise minimum
numpy.minimum(array2,array1)

#Logical operations
numpy.logical_and(array1,array2)
numpy.logical_not(array1)
numpy.logical_or(array1,array2)
numpy.logical_xor(array1,array2)

#Sort array
numpy.sort(array1)<jupyter_output><empty_output><jupyter_text>Statistical Functions<jupyter_code>#Sum of all elements
numpy.sum(array1)

#Mean
numpy.mean(array1)

#Standard Deviation
numpy.std(array1)

#Variance
numpy.var(array1)

#Maximum element
numpy.max(array1)

#Minimum element
numpy.min(array1)

#Index of max element
numpy.argmax(array1)

#Index of max element
numpy.argmin(array1)

#Cumulative product of elements
numpy.cumprod(array1)

#Cumulative sum of elements
numpy.cumsum(array1)<jupyter_output><empty_output><jupyter_text>Set operations<jupyter_code>#Union of two array
numpy.union1d(array1,array2)

#Intersection of two array
numpy.intersect1d(array1,array2)

#Set different
numpy.setdiff1d(array1,array2)<jupyter_output><empty_output><jupyter_text>Linear Algebra Functions<jupyter_code>#Dot product sum
array1.dot(array2)

array2d1 = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
array2d2 = numpy.array([[1,1,1],[2,2,2],[3,3,3]])

#Inverse of matrix
numpy.linalg.inv(array2d1)

#Transposne
array2d1.T

#Get diagonal of matrix
numpy.diag(array2d1)

#Compute determinant
numpy.linalg.det(array2d1)

#QR Decomposition of matrix
q,r = numpy.linalg.qr(array2d1)

#Compute trace (sum of all diagonal element)
numpy.trace(array2d1)

#SVD decomposition
a,b,c = numpy.linalg.svd(array2d1)<jupyter_output><empty_output><jupyter_text>Activation Functions<jupyter_code>def sigmoid_array(x):                                        
    return 1 / (1 + numpy.exp(-x))
numbers = numpy.arange(-10,10)
sigmoids = sigmoid_array(numbers)
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(numbers, sigmoids)
ax.grid(True)
plt.show()
def tanh_array(x):                                        
    return numpy.tanh(x)
numbers = numpy.arange(-10,10)
tanh = tanh_array(numbers)
fig, ax = plt.subplots()
ax.plot(numbers, tanh)
ax.grid(True)
plt.show()
def relu_array(x):                                        
    return numpy.maximum(0,x)
### relu = relu_array(numbers)
relu = relu_array(numbers)
fig, ax = plt.subplots()
ax.plot(numbers, relu)
ax.grid(True)
plt.show()<jupyter_output><empty_output><jupyter_text>Neural Network Classifier using Keras We will build our first simple neural network classifier using keras library. For this experiment we will use iris dataset. This is a public dataset downloaded from UCI https://archive.ics.uci.edu/ml/datasets/iris For this task we need to install two additional libraries Keras and Tensorflow.use following command to install keras and tensorflow<jupyter_code>! pip install keras
! pip install tensorflow
#Import libraries
import seaborn
import numpy
import pandas 

from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder

from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.utils import np_utils
#Read iris dataset stored in data folder
data = pandas.read_csv('D:/book/data/iris.csv')
#Check initial five rows of the data
data.head()<jupyter_output><empty_output><jupyter_text>Above data has three classes in text form. To give input to our neural network model, we first need to convert them to one hot encoding. We use pandas get_dummies() function to create one hot encoded class.<jupyter_code>class_as_one_hot_encoding = pandas.get_dummies(data['class'])
#Remove class and concat one hot encoding values
del data['class']
data = pandas.concat([data, class_as_one_hot_encoding], axis=1)
#Separate independent and dependent attributes
x = data.values[:,:4]
y = data.values[:,4:]
#Training and test split
train_x, test_x, train_y, test_y = train_test_split(x, y, train_size=0.7)<jupyter_output><empty_output><jupyter_text>Keras is a library of neural network. It has a lot of hyper parameters to tune for better model creation. In the dataset which we have choose contains 4 features and one output with three possible classes. So our input layer must have four units and output layer must have three units. We have a choice of number of hidden layers and hidden units within. In this example we will use only one hidden layer. We will take 10 hidden units in it. We will use sequential model in keras. This model is used to stacking up layers.<jupyter_code>model = Sequential()<jupyter_output><empty_output><jupyter_text>Now we define our first hidden layer. This layer is connected with four attribute input layer. In Keras we have different type of layers1. Dense Layer  - Nodes are fully connected with the output layer nodes.2. Activation Layer - Contains activation functions like sigmoid, ReLu, tanh3. Dropout Layer- Used for regularization4. Flatten Layer - Flatten the input.5. Reshape Layer - Reshape the output.6. Permute Layer - Permute the dimension of input as per pattern7. Repeat vector - repeat the input n times. In our example we will use Dense layer followed by activation layer. In our first simple neural network we want to connect each input layer node to output layer node. We will use 10 hidden units in hidden layer.<jupyter_code>model.add(Dense(10, input_shape=(4,)))<jupyter_output><empty_output><jupyter_text>We will use sigmoid activation for this layer.<jupyter_code>model.add(Activation('sigmoid'))<jupyter_output><empty_output><jupyter_text>Now we define output layer. As we have 3 output classes, we use three units in output layer<jupyter_code>model.add(Dense(3))<jupyter_output><empty_output><jupyter_text>We use softmax activation function for output layer.<jupyter_code>model.add(Activation('softmax'))<jupyter_output><empty_output><jupyter_text>**Configure Learning Process**Now we configure neural network training process. We have following choices.1. Optimizer    * Stochastic Gradient Descent    * Adamax    * Adam    * Nadam    * TFOptimizer    * RMSprop2. Loss function : Loss function is used to create the model for prediction. Different type of matrices are defined for different type of data. Some of the popular loss functions are    * mean_squared_error    * mean_absolute_error    * binary-cross-entropy    * categorical_crossentropy    * poisson     Now we compile our model using compile() function. Compiling the model uses efficient library (Tensorflow in this case) to do the processing<jupyter_code>model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=["accuracy"])<jupyter_output><empty_output><jupyter_text>Now we fit the model. We can choose different set of hyper parameters which affects our training. Here we have chosen number of iteration = 100 (epochs) and batch size =1. Batch size = 1 means at one time we are exposing only one training example to our model. If we don't want to see the processing and loss calculation, we can add parameter verbose=0 in the fit function.<jupyter_code>model_info = model.fit(train_x, train_y, epochs=100, batch_size=1 )<jupyter_output>Epoch 1/100
105/105 [==============================] - 0s - loss: 1.1276 - acc: 0.4286         
Epoch 2/100
105/105 [==============================] - 0s - loss: 1.0631 - acc: 0.3524        
Epoch 3/100
105/105 [==============================] - 0s - loss: 1.0190 - acc: 0.4381        
Epoch 4/100
105/105 [==============================] - 0s - loss: 0.9865 - acc: 0.5238     
Epoch 5/100
105/105 [==============================] - 0s - loss: 0.9491 - acc: 0.4571     
Epoch 6/100
105/105 [==============================] - 0s - loss: 0.9135 - acc: 0.5905        
Epoch 7/100
105/105 [==============================] - 0s - loss: 0.8679 - acc: 0.7714     
Epoch 8/100
105/105 [==============================] - 0s - loss: 0.8219 - acc: 0.9333     
Epoch 9/100
105/105 [==============================] - 0s - loss: 0.7761 - acc: 0.9143     
Epoch 10/100
105/105 [==============================] - 0s - loss: 0.7360 - acc: 0.8857     
Epoch 11/100
105/105 [==============================] - 0s - loss:[...]<jupyter_text>We use keras evaluate() function which returns loss and accuracy<jupyter_code>loss, accuracy = model.evaluate(test_x, test_y, verbose=0)
print("Model Accuracy = {:.4f}".format(accuracy))<jupyter_output>Model Accuracy = 0.9778<jupyter_text>Convolutional Neural Networks using Keras Now we will see some complex problem where deep learning can be used to improve performance. Object recognition is the process to identify predefined objects in the images. In the following example, we will use Keras to create CNN for object recognition.We will use CIFAR-10 dataset in this task. CIFAR stands for Canadian Institute for Advanced Research (CIFAR). This dataset has 60000 images of different objects.This dataset can easily be loaded using keras library. When we execute the code, it download the images. It may take some time depending on your bandwidth.<jupyter_code># import library
from keras.datasets import cifar10
from keras.layers import Dropout
from keras.layers.convolutional import MaxPooling2D
from keras.layers import Flatten
from keras.constraints import maxnorm
from scipy.misc import toimage
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from keras.layers.convolutional import Conv2D

from keras import backend as K
K.set_image_dim_ordering('th')

#Loading the data
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
#Lets see some examples from this data

fig = plt.figure()
gs = gridspec.GridSpec(4, 4, wspace=0.0)
ax = [plt.subplot(gs[i]) for i in range(4*4)]
for i in range(16):
    ax[i].imshow(toimage(x_train[i]))
plt.show()
# Now convert the class to one hot encoding matrix
y_train_onehot = np_utils.to_categorical(y_train)
y_test_onehot = np_utils.to_categorical(y_test)<jupyter_output><empty_output><jupyter_text>We can use simple CNN architecture to start. In this example we will use two convolutional layers and a max pooling.<jupyter_code># Create the model
#Sequential model is selected to get a stack of layers.
num_classes = 10
model = Sequential()

#First convolution layer
model.add(Conv2D(32, (3, 3), padding='same',input_shape=(3, 32, 32),  activation='relu'))

#Second convolution layer
model.add(Conv2D(32, (3, 3),padding='same', activation='relu', ))

#Pooling
model.add(MaxPooling2D(pool_size=(2, 2)))

#Flatten the output
model.add(Flatten())
model.add(Dense(512, activation='relu'))

#Output class
model.add(Dense(num_classes, activation='softmax'))
# Compile model
epochs = 50
lrate = 0.05

sgd = SGD(lr=lrate, momentum=0.8, decay=lrate/epochs, nesterov=False)

#Compile the model
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

#Print summary of CNN
print(model.summary())
#Fitting the model
model.fit(x_train, y_train_onehot, validation_data=(x_test, y_test_onehot), epochs=250, batch_size=100)
# Final evaluation of the model
loss,accuracy = model.evaluate(x_test, y_test_onehot, verbose=0)
print("Model Accuracy = {:.4f}".format(accuracy))<jupyter_output>Model Accuracy = 0.1000
{'fraction_non_alphanumeric': 0.1258017103153394, 'fraction_numerical': 0.04242383752004276, 'mean_word_length': 5.530977312390925, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 84, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '298214', 'license': 'MIT', 'n_tokens_mistral': 5446, 'n_tokens_neox': 5014, 'n_words': 1482, 'path': 'Chapter 10/10. Neural Network and Deep Learning.ipynb', 'repo_name': 'bpbpublications/-', 'size': '14968'}