Skip to content

Instantly share code, notes, and snippets.

@GMadorell
Created February 17, 2014 15:49
Show Gist options
  • Select an option

  • Save GMadorell/9053034 to your computer and use it in GitHub Desktop.

Select an option

Save GMadorell/9053034 to your computer and use it in GitHub Desktop.
Simple method to translate text to numbers giving a different number to every different text.
def text_to_number(text_array):
"""
@param text_array: Numpy array of strings.
@return: Numpy array of integers. Each different input string will be
assigned a different incremental number.
"""
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(text_array).toarray()
return X.argmax(1)
def main():
sample_text = \
"""barcelona
madrid
barcelona
bilbao
valencia
sabadell"""
text = np.array(sample_text.split("\n"))
X = text_to_number(text)
pprint(X)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment