Skip to content

Commit 480c615

Browse files
authored
Merge pull request WmHHooper#13 from WmHHooper/master
Update 11/1
2 parents 88e179b + d698bc0 commit 480c615

17 files changed

Lines changed: 1148 additions & 25 deletions

grading/bayesian-submissions.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import importlib
2+
import traceback
3+
from grading.util import roster, print_table
4+
# from logic import FolKB
5+
# from utils import expr
6+
import os
7+
from sklearn.naive_bayes import GaussianNB
8+
gnb = GaussianNB()
9+
10+
def indent(howMuch = 1):
11+
space = ' '
12+
for i in range(1, howMuch):
13+
space += ' '
14+
return space
15+
16+
def printKB(label, kb):
17+
print(indent(), label + ' example:')
18+
print(indent(2), 'knowledge base:')
19+
for clause in kb.clauses:
20+
print(indent(3), str(clause))
21+
22+
def printResults(query, gen, limit=3):
23+
for count in range(limit):
24+
try:
25+
long = next(gen)
26+
except StopIteration:
27+
print()
28+
return
29+
short = {}
30+
for v in long:
31+
if v in query.args:
32+
short[v] = long[v]
33+
print(short, end=' ')
34+
print('...')
35+
36+
def tryOne(label, frame):
37+
fit = gnb.fit(frame.data, frame.target)
38+
print('')
39+
print_table(fit.theta_,
40+
header=[frame.feature_names],
41+
topLeft=['Means:'],
42+
leftColumn=frame.target_names,
43+
numfmt='%6.3f',
44+
njust='center',
45+
tjust='rjust',
46+
)
47+
y_pred = fit.predict(frame.data)
48+
print("Number of mislabeled points out of a total %d points : %d"
49+
% (len(frame.data), (frame.target != y_pred).sum()))
50+
51+
def tryExamples(examples):
52+
for label in examples:
53+
tryOne(label, examples[label])
54+
55+
submissions = {}
56+
scores = {}
57+
58+
message1 = 'Submissions that compile:'
59+
60+
root = os.getcwd()
61+
for student in roster:
62+
try:
63+
os.chdir(root + '/submissions/' + student)
64+
# http://stackoverflow.com/a/17136796/2619926
65+
mod = importlib.import_module('submissions.' + student + '.myBayes')
66+
submissions[student] = mod.Examples
67+
message1 += ' ' + student
68+
except ImportError:
69+
pass
70+
except:
71+
traceback.print_exc()
72+
73+
os.chdir(root)
74+
75+
print(message1)
76+
print('----------------------------------------')
77+
78+
for student in roster:
79+
if not student in submissions.keys():
80+
continue
81+
scores[student] = []
82+
try:
83+
examples = submissions[student]
84+
print('Bayesian Networks from:', student)
85+
tryExamples(examples)
86+
except:
87+
traceback.print_exc()
88+
89+
print(student + ' scores ' + str(scores[student]) + ' = ' + str(sum(scores[student])))
90+
print('----------------------------------------')

grading/neuralNet-submissions.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import importlib
2+
import traceback
3+
from grading.util import roster, print_table
4+
# from logic import FolKB
5+
# from utils import expr
6+
import os
7+
from sklearn.neural_network import MLPClassifier
8+
mlpc = MLPClassifier()
9+
10+
def indent(howMuch = 1):
11+
space = ' '
12+
for i in range(1, howMuch):
13+
space += ' '
14+
return space
15+
16+
def tryOne(label, fAndP):
17+
frame = fAndP['frame']
18+
if 'mlpc' in fAndP.keys():
19+
clf = fAndP['mlpc']
20+
else:
21+
clf = mlpc
22+
fit = clf.fit(frame.data, frame.target)
23+
print('')
24+
# print_table(fit.theta_,
25+
# header=[frame.feature_names],
26+
# topLeft=[label],
27+
# leftColumn=frame.target_names,
28+
# numfmt='%6.3f',
29+
# njust='center',
30+
# tjust='rjust',
31+
# )
32+
y_pred = fit.predict(frame.data)
33+
print("Number of mislabeled points out of a total %d points : %d"
34+
% (len(frame.data), (frame.target != y_pred).sum()))
35+
36+
def tryExamples(examples):
37+
for label in examples:
38+
example = examples[label]
39+
main = getattr(example, 'main', None)
40+
if main != None:
41+
example.main()
42+
else:
43+
tryOne(label, example)
44+
45+
submissions = {}
46+
scores = {}
47+
48+
message1 = 'Submissions that compile:'
49+
50+
root = os.getcwd()
51+
for student in roster:
52+
try:
53+
os.chdir(root + '/submissions/' + student)
54+
# http://stackoverflow.com/a/17136796/2619926
55+
mod = importlib.import_module('submissions.' + student + '.myNN')
56+
submissions[student] = mod.Examples
57+
message1 += ' ' + student
58+
except ImportError:
59+
pass
60+
except:
61+
traceback.print_exc()
62+
63+
os.chdir(root)
64+
65+
print(message1)
66+
print('----------------------------------------')
67+
68+
for student in roster:
69+
if not student in submissions.keys():
70+
continue
71+
scores[student] = []
72+
try:
73+
examples = submissions[student]
74+
print('Bayesian Networks from:', student)
75+
tryExamples(examples)
76+
except:
77+
traceback.print_exc()
78+
79+
print(student + ' scores ' + str(scores[student]) + ' = ' + str(sum(scores[student])))
80+
print('----------------------------------------')

grading/util.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
'zzzsolutions',
1515
]
1616

17-
def print_table(table, header=None, sep=' ',numfmt='%g',
18-
njust='rjust', tjust='ljust'):
17+
def print_table(table, header=[], leftColumn=[], topLeft=[],
18+
sep=' ', numfmt='%g', njust='rjust', tjust='ljust'):
1919
"""Print a list of lists as a table, so that columns line up nicely.
2020
header, if specified, will be printed as the first rows.
2121
sep is the separator between columns, e.g. '|' or ', '
@@ -26,21 +26,40 @@ def print_table(table, header=None, sep=' ',numfmt='%g',
2626
"""
2727
if len(table) == 0:
2828
return
29-
justs = [njust if isnumber(x) else tjust for x in table[0]]
3029

31-
if header:
30+
31+
pTable = []
32+
if len(header) > 0:
3233
r = 0
3334
for row in header:
34-
table.insert(r, row)
35+
pTable.insert(r, row)
36+
r += 1
37+
38+
pTable.extend([[(numfmt % x) if isnumber(x) else x for x in row]
39+
for row in table])
40+
41+
if len(leftColumn) > 0:
42+
#justs.insert(0, njust if isnumber(leftColumn[0]) else tjust)
43+
pLeft = []
44+
if header:
45+
hr = 0
46+
for h in header:
47+
topLeft.append(' ')
48+
pLeft.insert(0, topLeft[hr])
49+
hr += 1
50+
for cell in leftColumn:
51+
pLeft.append(cell)
52+
r = 0
53+
for row in pTable:
54+
row.insert(0, pLeft[r])
3555
r += 1
3656

37-
table = [[(numfmt % x) if isnumber(x) else x for x in row]
38-
for row in table]
57+
justs = [njust if isnumber(x) else tjust for x in pTable[len(header)]]
3958

4059
sizes = list(
4160
map(lambda seq: max(map(len, seq)),
42-
list(zip(*[map(str, row) for row in table]))))
61+
list(zip(*[map(str, row) for row in pTable]))))
4362

44-
for row in table:
63+
for row in pTable:
4564
print(sep.join(getattr(
4665
str(x), j)(size) for (j, size, x) in zip(justs, sizes, row)))

submissions/Porter/billionaires.db

2.58 MB
Binary file not shown.

submissions/Porter/billionaires.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
'''
2+
Hello student. Thank you for downloading a CORGIS library. However, you do not need to open this library. Instead you should use the following:
3+
4+
import billionaires
5+
6+
If you opened the file because you are curious how this library works, then well done! We hope that you find it a useful learning experience. However, you should know that this code is meant to solve somewhat esoteric pedagogical problems, so it is often not best practices.
7+
'''
8+
9+
import sys as _sys
10+
import os as _os
11+
import json as _json
12+
import sqlite3 as _sql
13+
import difflib as _difflib
14+
15+
class _Constants(object):
16+
'''
17+
Global singleton object to hide some of the constants; some IDEs reveal internal module details very aggressively, and there's no other way to hide stuff.
18+
'''
19+
_HEADER = {'User-Agent':
20+
'CORGIS Billionaires library for educational purposes'}
21+
_PYTHON_3 = _sys.version_info >= (3, 0)
22+
_TEST = False
23+
_HARDWARE = 1000
24+
25+
if _Constants._PYTHON_3:
26+
import urllib.request as _request
27+
from urllib.parse import quote_plus as _quote_plus
28+
from urllib.error import HTTPError as _HTTPError
29+
else:
30+
import urllib2 as _urllib2
31+
from urllib import quote_plus as _quote_plus
32+
from urllib2 import HTTPError as _HTTPError
33+
34+
class DatasetException(Exception):
35+
''' Thrown when there is an error loading the dataset for some reason.'''
36+
pass
37+
38+
_Constants._DATABASE_NAME = "billionaires.db"
39+
if not _os.access(_Constants._DATABASE_NAME, _os.F_OK):
40+
raise DatasetException("Error! Could not find a \"{0}\" file. Make sure that there is a \"{0}\" in the same directory as \"{1}.py\"! Spelling is very important here.".format(_Constants._DATABASE_NAME, __name__))
41+
elif not _os.access(_Constants._DATABASE_NAME, _os.R_OK):
42+
raise DatasetException("Error! Could not read the \"{0}\" file. Make sure that it readable by changing its permissions. You may need to get help from your instructor.".format(_Constants._DATABASE_NAME, __name__))
43+
elif not _os.access(_Constants._DATABASE_NAME, _os.W_OK):
44+
_sys.stderr.write('The local cache (\" \") will not be updated. Make sure that it is writable by changing its permissions. You may need to get help from your instructor.\n'.format(_Constants._DATABASE_NAME))
45+
_sys.stderr.flush()
46+
47+
_Constants._DATABASE = _sql.connect(_Constants._DATABASE_NAME)
48+
49+
class _Auxiliary(object):
50+
@staticmethod
51+
def _parse_type(value, type_func):
52+
"""
53+
Attempt to cast *value* into *type_func*, returning *default* if it fails.
54+
"""
55+
default = type_func(0)
56+
if value is None:
57+
return default
58+
try:
59+
return type_func(value)
60+
except ValueError:
61+
return default
62+
63+
@staticmethod
64+
def _byteify(input):
65+
"""
66+
Force the given input to only use `str` instead of `bytes` or `unicode`.
67+
This works even if the input is a dict, list,
68+
"""
69+
if isinstance(input, dict):
70+
return {_Auxiliary._byteify(key): _Auxiliary._byteify(value) for key, value in input.items()}
71+
elif isinstance(input, list):
72+
return [_Auxiliary._byteify(element) for element in input]
73+
elif _Constants._PYTHON_3 and isinstance(input, str):
74+
return str(input.encode('ascii', 'replace').decode('ascii'))
75+
elif not _Constants._PYTHON_3 and isinstance(input, unicode):
76+
return str(input.encode('ascii', 'replace').decode('ascii'))
77+
else:
78+
return input
79+
80+
@staticmethod
81+
def _guess_schema(input):
82+
if isinstance(input, dict):
83+
return {str(key.encode('ascii', 'replace').decode('ascii')):
84+
_Auxiliary._guess_schema(value) for key, value in input.items()}
85+
elif isinstance(input, list):
86+
return [_Auxiliary._guess_schema(input[0])] if input else []
87+
else:
88+
return type(input)
89+
90+
91+
92+
################################################################################
93+
# Domain Objects
94+
################################################################################
95+
96+
97+
98+
99+
100+
################################################################################
101+
# Interfaces
102+
################################################################################
103+
104+
105+
106+
def get_billionaires():
107+
"""
108+
Returns information about all the billionaires.
109+
110+
"""
111+
if False:
112+
# If there was a Test version of this method, it would go here. But alas.
113+
pass
114+
else:
115+
rows = _Constants._DATABASE.execute("SELECT data FROM billionaires".format(
116+
hardware=_Constants._HARDWARE))
117+
data = [r[0] for r in rows]
118+
data = [_Auxiliary._byteify(_json.loads(r)) for r in data]
119+
120+
return _Auxiliary._byteify(data)
121+
122+
123+
################################################################################
124+
# Internalized testing code
125+
################################################################################
126+
127+
def _test_interfaces():
128+
from pprint import pprint as _pprint
129+
from timeit import default_timer as _default_timer
130+
# Production test
131+
print("Production get_billionaires")
132+
start_time = _default_timer()
133+
result = get_billionaires()
134+
135+
print("{} entries found.".format(len(result)))
136+
_pprint(_Auxiliary._guess_schema(result))
137+
138+
print("Time taken: {}".format(_default_timer() - start_time))
139+
140+
141+
if __name__ == '__main__':
142+
from optparse import OptionParser as _OptionParser
143+
_parser = _OptionParser()
144+
_parser.add_option("-t", "--test", action="store_true",
145+
default=False,
146+
help="Execute the interfaces to test them.")
147+
(_options, _args) = _parser.parse_args()
148+
149+
if _options.test:
150+
_test_interfaces()

0 commit comments

Comments
 (0)