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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using NetSVMLight; | |
using System.IO; | |
namespace NetSVMLightConsoleApplication1 | |
{ | |
class Program |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using NetSVMLight; | |
namespace NetSVMLightConsoleApplication1 | |
{ | |
class Program | |
{ |
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 simplejson as json | |
import urllib2, urllib, string | |
read_token = 'abc | |
write_token = 'def' | |
def GetJsonResponse(requestUrl, content = None): | |
request = urllib2.Request(requestUrl) | |
response = urllib2.urlopen(request, content) | |
result = json.load(response) |
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
package FacebookGraph; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.URL; |
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
package TwitterGraph; | |
import java.io.BufferedWriter; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.List; | |
import java.util.Properties; |
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
def Classify(self, featureVector): #featureVector is a simple list like the ones that we use to train | |
probabilityPerLabel = {} #store the final probability for each class label | |
for label in self.labelCounts: | |
logProb = 0 | |
for featureValue in featureVector: | |
logProb += math.log(self.featureCounts[(label, self.featureNameList[featureVector.index(featureValue)], featureValue)]/self.labelCounts[label]) | |
probabilityPerLabel[label] = (self.labelCounts[label]/sum(self.labelCounts.values())) * math.exp(logProb) | |
print probabilityPerLabel | |
return max(probabilityPerLabel, key = lambda classLabel: probabilityPerLabel[classLabel]) |
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
def TrainClassifier(self): | |
for fv in self.featureVectors: | |
self.labelCounts[fv[len(fv)-1]] += 1 #udpate count of the label | |
for counter in range(0, len(fv)-1): | |
self.featureCounts[(fv[len(fv)-1], self.featureNameList[counter], fv[counter])] += 1 | |
for label in self.labelCounts: #increase label counts (smoothing). remember that the last feature is actually the label | |
for feature in self.featureNameList[:len(self.featureNameList)-1]: | |
self.labelCounts[label] += len(self.features[feature]) |
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
def GetValues(self): | |
file = open(self.trainingFile, 'r') | |
for line in file: | |
if line[0] != '@': #start of actual data | |
self.featureVectors.append(line.strip().lower().split(',')) | |
else: #feature definitions | |
if line.strip().lower().find('@data') == -1 and (not line.lower().startswith('@relation')): | |
self.featureNameList.append(line.strip().split()[1]) | |
self.features[self.featureNameList[len(self.featureNameList) - 1]] = line[line.find('{')+1: line.find('}')].strip().split(',') | |
file.close() |
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
from __future__ import division | |
import collections | |
import math | |
class Model: | |
def __init__(self, arffFile): | |
self.trainingFile = arffFile | |
self.features = {} #all feature names and their possible values (including the class label) | |
self.featureNameList = [] #this is to maintain the order of features as in the arff | |
self.featureCounts = collections.defaultdict(lambda: 1)#contains tuples of the form (label, feature_name, feature_value) |
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
#Author: Krishnamurthy Koduvayur Viswanathan | |
from __future__ import division | |
import collections | |
import math | |
class Model: | |
def __init__(self, arffFile): | |
self.trainingFile = arffFile | |
self.features = {} #all feature names and their possible values (including the class label) |