Skip to content

Instantly share code, notes, and snippets.

@ashunigion
Created June 11, 2019 23:45
Show Gist options
  • Save ashunigion/040cee5865dd6f3e480acf72afe4d019 to your computer and use it in GitHub Desktop.
Save ashunigion/040cee5865dd6f3e480acf72afe4d019 to your computer and use it in GitHub Desktop.
Padding the smaller reviews and truncating the large reviews
def pad_features(reviews_ints, seq_length):
''' Return features of review_ints, where each review is padded with 0's
or truncated to the input seq_length.
'''
features = []
## implement function
for review in reviews_ints:
if len(review)<seq_length:
features.append(list(np.zeros(seq_length-len(review)))+review)
elif len(review)>seq_length:
features.append(review[:seq_length])
else:
features.append(review)
features = np.array(features)
return features
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment