Created
August 9, 2016 17:45
-
-
Save patilarpith/6737a60c9f23fd379104ba892c0c576f to your computer and use it in GitHub Desktop.
Quora Answer Classifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import numpy as np | |
f = sys.stdin | |
# f = open("../input00.txt") | |
[n, m] = map(int, f.readline().strip().split()) | |
X = np.zeros((n, m), dtype="float") | |
Y = np.zeros(n) | |
train_ids = [] | |
for i in xrange(n): | |
line = f.readline().strip().split() | |
train_ids.append(line[0]) | |
Y[i] = int(line[1]) | |
for feat in line[2:]: | |
[feat_index, feat_val] = feat.split(":") | |
X[i][int(feat_index) - 1] = float(feat_val) | |
q = int(f.readline().strip()) | |
X_test = np.zeros((q, m), dtype="float") | |
test_ids = [] | |
for i in xrange(q): | |
line = f.readline().strip().split() | |
test_ids.append(line[0]) | |
for feat in line[1:]: | |
[feat_index, feat_val] = feat.split(":") | |
X_test[i][int(feat_index) - 1] = float(feat_val) | |
# training | |
from sklearn import preprocessing | |
from sklearn import svm | |
xtrain_std = preprocessing.StandardScaler().fit_transform(X) | |
ytrain = Y | |
xtest_std = preprocessing.StandardScaler().fit_transform(X_test) | |
def learn(): | |
clf = svm.LinearSVC(C = 10**1) | |
clf.fit(xtrain_std, ytrain) | |
return clf | |
def test(clf): | |
return clf.predict(xtest_std) | |
clf = learn() | |
ypred = test(clf) | |
for i in xrange(len(ypred)): | |
print test_ids[i] + " " + ("+1" if (ypred[i] > 0) else "-1") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment