python prediction
Python hosting: Host, run, and code Python in the cloud!
Natural Language Processing with PythonWe can use natural language processing to make predictions.
Example: Given a product review, a computer can predict if its positive or negative based on the text.
In this article you will learn how to make a prediction program based on natural language processing.
Related course: Natural Language Processing with Python
nlp prediction example
Given a name, the classifier will predict if it’s a male or female.
To create our analysis program, we have several steps:
- Data preparation
- Feature extraction
- Training
- Prediction
Data preparation
The first step is to prepare data.
We use the names set included with nltk.
|
This dataset is simply a collection of tuples. To give you an idea of what the dataset looks like:
|
You can define your own set of tuples if you wish, its simply a list containing many tuples.
Feature extraction
Based on the dataset, we prepare our feature. The feature we will use is the last letter of a name:
We define a featureset using:
|
and the features (last letters) are extracted using:
|
Training and prediction
We train and predict using:
|
Example
A classifier has a training and a test phrase.
|
If you want to give the name during runtime, change the last line to:
|
For Python 2, use raw_input.
Leave a Reply: