Based on email spam classifier algorithm.
- Find out what your features are.
- Create training set.
- Create cross validation set.
- Create test set.
- Decide what algorithm should be used.
- Better to start with quick algorithm to test how it all works.
- Run your algorithm with training date to get as small as possible cost function.
- Run againt cross validation set.
- Optimize algorithm using cross validation resuts.
- Iterate.
- Test againt test data.
Features x: Choose n amount of words indicative of sentence to block / not block.
Find most frequently occuring words in the training set. This is your feature vector.
Optionally normalize the data:
- Use stemmer software (Porter Stemmer).
For example word "story or stories" would be normalized to "stori"
from nltk import PorterStemmer
word = PorterStemmer().stem("stories")
- Normalize all words to lowercase.
- Fix misspellings.
Create feature vector from received sentence:
Vector contains boolean value for all blocked words, so we get vector nx1.
If blocked word appears in the sentence then the boolean value in the feature vector would be set to true (1).
Example:
Blocked words: "work", "git", "story", "backlog" (n = 4)
Sentence: "I added new story to our backlog."
Feature vector:
[0, 0, 1, 1]
This is the input value to machine learning algorithm.
x = features of sentences (e-mail, chat...)
y = boolean classifier: block (1) or don't block (0)
n = number of features
m = number of training examples
theta = weight variable (this is the value calculated by the learning algorithm)
- Create vocabulary list.
- Find out n most used words in training data.
- Every word has an index in the list.
- Create matrix X from training data.
- Feature vector for all training examples (matrix of mxn).
- Create classifier vector y (vector mx1). These are the valid results to train on.
- Calculate cost function and gradient
- Initialize theta to random values.
- Calculate optimal theta using minimize function.
- For example in python use scipy and Newton Conjugate Gradient.
- Create feature vector of the received message.
- Calculate y using optimal theta.
- Create predict function to decide if the output is positive or negative.
- Output value between 0..1 should be changed to binary output true or false.