Skip to content

Instantly share code, notes, and snippets.

@patilarpith
Created August 9, 2016 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patilarpith/6737a60c9f23fd379104ba892c0c576f to your computer and use it in GitHub Desktop.
Save patilarpith/6737a60c9f23fd379104ba892c0c576f to your computer and use it in GitHub Desktop.
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