Reader small image

You're reading from  Hands-On Neural Networks with Keras

Product typeBook
Published inMar 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789536089
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Niloy Purkait
Niloy Purkait
author image
Niloy Purkait

Niloy Purkait is a technology and strategy consultant by profession. He currently resides in the Netherlands, where he offers his consulting services to local and international companies alike. He specializes in integrated solutions involving artificial intelligence, and takes pride in navigating his clients through dynamic and disruptive business environments. He has a masters in Strategic Management from Tilburg University, and a full specialization in data science from Michigan University. He has advanced industry grade certifications from IBM, in subjects like signal processing, cloud computing, machine and deep learning. He is also perusing advanced academic degrees in several related fields, and is a self-proclaimed lifelong learner.
Read more about Niloy Purkait

Right arrow

Vectorizing features

The following function will take our training data of 25,000 lists of integers, where each list is a review. In return, it spits out one-hot encoded vectors for each of the integer lists it received from our training set. Then, we simply redefine our training and test features by using this function to transform our integer lists into a 2D tensor of one-hot encoded review vectors:

import numpy as np
def vectorize_features(features):

#Define the number of total words in our corpus
#make an empty 2D tensor of shape (25000, 12000)
dimension=12000
review_vectors=np.zeros((len(features), dimension))

#interate over each review
#set the indices of our empty tensor to 1s
for location, feature in enumerate(features):
review_vectors[location, feature]=1
return review_vectors

x_train = vectorize_features(x_train)
x_test = vectorize_features(x_test)

You can see the result of...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Hands-On Neural Networks with Keras
Published in: Mar 2019Publisher: PacktISBN-13: 9781789536089

Author (1)

author image
Niloy Purkait

Niloy Purkait is a technology and strategy consultant by profession. He currently resides in the Netherlands, where he offers his consulting services to local and international companies alike. He specializes in integrated solutions involving artificial intelligence, and takes pride in navigating his clients through dynamic and disruptive business environments. He has a masters in Strategic Management from Tilburg University, and a full specialization in data science from Michigan University. He has advanced industry grade certifications from IBM, in subjects like signal processing, cloud computing, machine and deep learning. He is also perusing advanced academic degrees in several related fields, and is a self-proclaimed lifelong learner.
Read more about Niloy Purkait