Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. from sklearn.metrics import precision_recall_fscore_support
  2. from sklearn.feature_extraction.text import TfidfVectorizer
  3.  
  4. param_grid = [
  5.   {'C': [1, 10, 100, 1000], 'kernel': ['linear']},
  6.   {'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']},
  7.  ]
  8. kf = cross_validation.KFold(len(totaldata), n_folds=5)
  9. X = totaldata
  10. y = totallabel
  11. clf = GridSearchCV(SVC(C=1), param_grid, cv=5)
  12.  
  13. for train_index, test_index in kf:
  14.     y_train, y_test = y[train_index], y[test_index]  
  15.     vectorizer = TfidfVectorizer(ngram_range=(1,2))
  16.     #print train_index
  17.     c_train = [con for ind, con in enumerate(content_list) if ind in train_index]
  18.     X_train = vectorizer.fit_transform(c_train)
  19.     c_test = [con for ind, con in enumerate(content_list) if ind in test_index]
  20.     X_test = vectorizer.transform(c_test)
  21.     clf.fit(X_train, y_train)
  22.     y_pred = clf.predict(X_test)
  23.     precision, recall, fbeta, support = precision_recall_fscore_support(y_test, y_pred, average='macro')
  24.     print 'macro', precision, recall, fbeta, support
  25.     precision, recall, fbeta, support = precision_recall_fscore_support(y_test, y_pred, average='micro')
  26.     print 'micro', precision, recall, fbeta, support
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement