-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKNN.py
35 lines (32 loc) · 1.16 KB
/
KNN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import math
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.neighbors import KNeighborsClassifier
from sklearn import neighbors
import numpy as np
class knn:
def __init__(self, reviews, target):
self.vectorizer = CountVectorizer(ngram_range=(1,3), max_df=1)
corpus = []
thing = np.asarray(target)
for __, json_obj in reviews:
corpus.append(json_obj['text'])
self.X = self.vectorizer.fit_transform(corpus)
self.estimate = KNeighborsClassifier(n_neighbors=1)
self.estimate.fit(self.X,thing)
pass
def classify_all(self, parsed_review_sentences):
model_classified = []
for __, json_obj in parsed_review_sentences:
model_classified += [self.classify(json_obj['text'])]
return model_classified
def classify(self, sentence):
L = self.vectorizer.transform([sentence]).toarray()
value = self.estimate.predict(L)
if (value == 0):
return "spring"
elif (value == 1):
return "summer"
elif (value == 2):
return "fall"
elif (value == 3):
return "winter"