Last active
August 29, 2015 14:07
-
-
Save nyango/53a263e47789294c0564 to your computer and use it in GitHub Desktop.
naivebayes.py
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 csv | |
trainReader = csv.reader(open('traindata.csv', 'rb')) | |
nya = [] | |
for row in trainReader: | |
nya.append(row) | |
# train data | |
noarr = filter(lambda row: row[-1] == 'No', nya) | |
yesarr = filter(lambda row: row[-1] == 'Yes', nya) | |
testReader = csv.reader(open('testdata.csv', 'rb')) | |
testdata = [] | |
for row in testReader: | |
testdata.append(row) | |
# functions | |
def p_y(char,i,yesarr): | |
return 1.0*len(filter(lambda row: row[i] == char, yesarr))/len(yesarr) | |
def pstr_y(arr,basearr): | |
res = 1.0 | |
for i,val in enumerate(arr): | |
res = res*p_y(val,i,basearr) | |
return res | |
def pred(row): | |
rr = row[0:-1] | |
return pstr_y(rr,yesarr) >= pstr_y(rr,noarr) | |
# apply | |
for row in testdata: | |
print 'Yes' if pred(row) else 'No' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment