In [2]:
import csv, re, string
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.pipeline import Pipeline
In [15]:
PUNCTUATION = re.compile('[%s]' % re.escape(string.punctuation))
VALID_CLASSES = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'T', 'X', 'Z']
In [16]:
data = []
with open('data/category-training.csv', 'r') as f:
inputreader = csv.reader(f, delimiter=',', quotechar='"')
for r in inputreader:
# Concatenate the occupation and employer strings together and remove
# punctuation. Both occupation and employer will be used in prediction.
text = PUNCTUATION.sub('', ' '.join(r[0:2]))
if len(r[2]) > 1 and r[2][0] in VALID_CLASSES:
# We're only attempting to classify the first character of the
# industry prefix ("A", "B", etc.) -- not the whole thing. That's
# what the r[2][0] piece is about.
data.append([text, r[2][0]])
In [18]:
texts = np.array([el[0] for el in data])
classes = np.array([el[1] for el in data])
In [19]:
print texts
In [20]:
print classes
In [21]:
pipeline = Pipeline([
('vectorizer', CountVectorizer(
ngram_range=(1,2),
stop_words='english',
min_df=2,
max_df=len(texts))),
('classifier', LogisticRegression())
])
In [22]:
pipeline.fit(np.asarray(texts), np.asarray(classes))
Out[22]:
In [27]:
print pipeline.predict(['LAWYER'])
In [28]:
print pipeline.predict(['SKADDEN ARPS'])
In [29]:
print pipeline.predict(['COMPUTER PROGRAMMER'])
In [ ]: