Created
February 17, 2014 15:49
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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