fork download
  1. from sklearn.feature_extraction.text import TfidfVectorizer
  2. from sklearn.metrics import classification_report
  3. from sklearn.svm import SVC
  4.  
  5. training_corpus = [
  6. ("I am exhausted of this work", "class_B"),
  7. ("I can't cooperate with this", "class_B"),
  8. ("He is my badest enemy", "class_B"),
  9. ("My management is poor", "class_B"),
  10. ("I love this burger", "class_A"),
  11. ("This is an brilliant place!", "class_A"),
  12. ("I feel very good about these dates", "class_A"),
  13. ("This is my best work", "class_A"),
  14. ("What an awesome view", "class_A"),
  15. ("I do not like this dish", "class_B")
  16. ]
  17.  
  18. test_corpus = [
  19. ("I am not feeling well, today", "class_B"),
  20. ("I feel brilliant", "class_A"),
  21. ("Gray is a friend of mine", "class_A"),
  22. ("I do not enjoy my job", "class_B")
  23. ]
  24.  
  25. train_data = []
  26. train_labels = []
  27. for row in training_corpus:
  28. train_data.append(row[0])
  29. train_labels.append(row[1])
  30.  
  31. test_data = []
  32. test_labels = []
  33. for row in test_corpus:
  34. test_data.append(row[0])
  35. test_labels.append(row[1])
  36.  
  37. vectorizer = TfidfVectorizer(min_df=4, max_df=0.9)
  38. train_vectors = vectorizer.fit_transform(train_data)
  39. test_vectors = vectorizer.transform(test_data)
  40.  
  41. model = SVC(kernel="linear")
  42. model.fit(train_vectors, train_labels)
  43.  
  44. prediction = model.predict(test_vectors)
  45.  
  46. print(prediction)
  47. print(classification_report(test_labels, prediction))
  48.  
Success #stdin #stdout 0.35s 65360KB
stdin
Standard input is empty
stdout
['class_A' 'class_A' 'class_B' 'class_A']
              precision    recall  f1-score   support

     class_A       0.33      0.50      0.40         2
     class_B       0.00      0.00      0.00         2

   micro avg       0.25      0.25      0.25         4
   macro avg       0.17      0.25      0.20         4
weighted avg       0.17      0.25      0.20         4