Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quora Answer Classifier
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
You can’t perform that action at this time.