def file_load(): """ Loads countries from file. """ file = tkinter.filedialog.askopenfile() if file: try: self.colorer.countries = parse.load(file) except (FileNotFoundError, ValueError, IndexError): self.colorer.countries = parse.load(COUNTRIES.splitlines()) file.close() self.colorer.set_colors() self.draw_countries()
def content_boosted(setno=0): training, test, metadata = parse.load(setno) ratingMatrix = constructMatrix(training, metadata) simMat = sf.cosineMatrix(ratingMatrix) np.savetxt('../output/siml.txt', simMat) predict = utils.predictRating(simMat, ratingMatrix) np.savetxt('../output/pred.txt', predict) userRating = utils.constructRatingMatrix(training, metadata) v = np.copy(userRating) user = int(metadata['users']) item = int(metadata['items']) for i in range(user): for j in range(item): if v[i][j] == 0: v[i][j] = predict[j][i] np.savetxt('../output/virt.txt', v) np.savetxt('../output/ratingMatrix.txt', ratingMatrix) hw = cal.getHwau(ratingMatrix.transpose()) sw = utils.calculateSW(ratingMatrix.transpose()) simMat = sf.cosineMatrix(ratingMatrix) np.savetxt('../output/similar.txt', simMat) print 'sim done!' prediction = utils.contentBoostPred(simMat, ratingMatrix, hw, sw, v) np.savetxt('../output/predict.txt', prediction) print 'prediction done!' predictionOnTest = prediction[test[:, 0].astype(int) - 1, test[:, 1].astype(int) - 1] error = predictionOnTest - test[:, 2] return np.abs(error).mean()
def main(): path = sys.argv[1] with open(path) as f: data = load(f) tbl = format_table(data) print tbl
def test_parse(self): country1 = country.Country([[1, 1], [10, 1], [10, 10], [1, 10]]) country2 = country.Country([[5, 5], [12, 5], [12, 12], [5, 12]]) country3 = country.Country([[100, 100], [100, 200], [200, 200], [200, 100]]) country4 = country.Country([[200, 200], [300, 200], [300, 300], [200, 300]]) graph = country.Graph([country1, country2, country3, country4]) string = parse.save(graph) graph2 = parse.load(string.splitlines()) self.assertEqual(graph, graph2)
def load(fname): def to_default(dic): rd=defaultdict(lambda :defaultdict(lambda :None)) rd.update(dic) return rd rd=parse.load(fname) rd['Init']=to_default(rd['Init']) rd['Goal']=to_default(rd['Goal']) return rd
def test_coloring(self): countries = """10, 10; 200, 10; 200, 150; 10, 200 200, 100; 300, 60; 250, 10; 200, 10 200, 100; 300, 60; 200, 200""" graph = parse.load([line.strip() for line in countries.splitlines()]) colorer = coloring.Colorer(graph) colorer.set_colors() errors = False for item in graph.items: for neigh in graph.get_neighbours(item): if item[1] == neigh[1]: errors = True self.assertFalse(errors)
def __init__(self): self.root = tkinter.Tk() self.canvas = tkinter.Canvas() self.menu = tkinter.Menu() self.create_menu(tkinter.ACTIVE) self.locked = False self.root.config(menu=self.menu) self.canvas.configure(width=1024, height=768) self.edit = True self.temp_points = [] try: with open('maps/default.txt') as country_file: countries = parse.load(country_file) except (FileNotFoundError, ValueError, IndexError): countries = parse.load(COUNTRIES.splitlines()) self.colorer = Colorer(countries) self.draw_countries() self.canvas.pack() self.root.bind('<Button-1>', self.on_mouse_click) self.root.bind('<Key>', self.on_key_down) self.root.title('mapcoloring') self.root.mainloop()
def main(): input_path = sys.argv[1] data_path = sys.argv[2] gnuplot_path = sys.argv[3] png_path = sys.argv[4] cpu_name = sys.argv[5] with open(input_path) as f: data = load(f) keys = sorted(data[1].keys()) with open(data_path, 'wt') as f: gnuplot_data(f, data) with open(gnuplot_path, 'wt') as f: gnuplot_script(f, keys, data_path, png_path, cpu_name)
def main(load=True, setno): training, test, metadata = parse.load(setno) if not load: ratingMatrix = utils.constructRatingMatrix(training, metadata) similarity = sf.cosineMatrix(ratingMatrix) np.savetxt("similarity%s.txt" % (setno), similarity) print "similarity done" prediction = utils.predictRating(similarity, ratingMatrix) np.savetxt("prediction%s.txt" % (setno), prediction) print "prediction done" else: with open("similarity.txt") as f: similarity = np.loadtxt(f) with open("prediction.txt") as f: prediction = np.loadtxt(f) import pdb pdb.set_trace() # import pdb; pdb.set_trace(); predictionOnTest = prediction[test[:, 0].astype(int) - 1, test[:, 1].astype(int) - 1] error = predictionOnTest - test[:, 2] return np.abs(error).mean()
def main(load=True, setno): training, test, metadata = parse.load(setno) if not load: ratingMatrix = utils.constructRatingMatrix(training, metadata) similarity = sf.cosineMatrix(ratingMatrix) np.savetxt('similarity%s.txt' % (setno), similarity) print "similarity done" prediction = utils.predictRating(similarity, ratingMatrix) np.savetxt('prediction%s.txt' % (setno), prediction) print "prediction done" else: with open('similarity.txt') as f: similarity = np.loadtxt(f) with open('prediction.txt') as f: prediction = np.loadtxt(f) import pdb pdb.set_trace() #import pdb; pdb.set_trace(); predictionOnTest = prediction[test[:, 0].astype(int) - 1, test[:, 1].astype(int) - 1] error = predictionOnTest - test[:, 2] return np.abs(error).mean()
def main(load=True, setno=0): training, test, metadata = parse.load(setno) if not load: ratingMatrix = utils.constructRatingMatrix(training, metadata) start = clock() # similarity = sf.pearsonMatrix(ratingMatrix) similarity = sf.pearsonMatrix(ratingMatrix) end = clock() print 'run time: %d' % (end - start) np.savetxt('../output/siml/similarity%s.txt' % (setno), similarity) print "similarity done" prediction = utils.predictRating(similarity, ratingMatrix) testUserId = 0 testArray = np.copy(prediction[testUserId]) utils.quicksort(testArray, 0, testArray.size - 1) numOfRecommend = 3 topKResults = np.zeros(numOfRecommend) for i in range(numOfRecommend): topKResults[i] = testArray[testArray.size - 1 - i] for index in range(testArray.size): for result in topKResults: if prediction[testUserId][index] == result: print index np.savetxt('../output/pred/prediction%s.txt' % (setno), prediction) print "prediction done" else: with open('../dataset/similarity.txt') as f: similarity = np.loadtxt(f) with open('../dataset/prediction.txt') as f: prediction = np.loadtxt(f) # import pdb; pdb.set_trace() predictionOnTest = prediction[test[:, 0].astype(int) - 1, test[:, 1].astype(int) - 1] error = predictionOnTest - test[:, 2] return np.abs(error).mean()
def main(): d = load(sys.argv[1]) render(d)
return (self.functor==obj.functor and self.arguments==obj.arguments and self.value==obj.value) def __repr__(self): return ''.join(['~' if self.value==False else '',self.functor,'(',','.join(self.arguments),')']) class State(object): def __init__(self,dic): ''' dic like {'At':{('A','B'):True}, {'In':{('C','D'):True,('E','F'):False}}} ''' self._dic=dic self.prop_list=[] self.prop_map={} for functor,props in dic.items(): for arg,value in props.items(): prop=Propsition(functor=functor,arguments=arg,value=value) self.prop_list.append(prop) self.prop_map[(functor,arg)]=prop def is_true(self,functor,arguments): if self.prop_map.has_key((functor,arguments)): return self.prop_map[(functor,arguments)].value else: return None def __repr__(self): return '\n'.join([prop.__repr__() for prop in self.prop_list]) pp=load('plane_problem.txt') Init=State(pp['Init']) Goal=State(pp['Goal'])
def load(fname): rd=parse.load(fname) dic=defaultdict(lambda :defaultdict(lambda :False)) dic.update(rd['Init']) dic['Init']=dic return rd
import numpy as np import parse as ps import sim_train as st # Main code (do stuff here) if __name__ == "__main__": x_train, y_train, ing, c_train, ids_train = ps.load("train") x_test, _, _, _, ids_test = ps.load("test") predictions = st.train_correlation(x_train, ing, c_train) y_train_list = [] for el in y_train: y_train_list.append(''.join(str(e) for e in st.un_onehot(el, c_train))) acc = st.accuracy_corrolation(predictions, y_train_list) print("Accuracy: {}%".format(acc * 100))
def makeDic(data): dataDic = {} for i in data: dataDic.setdefault(i[0], {}).update({i[1]: i[2]}) return dataDic def makeArray(dic): arr = [] for user, items in dic.iteritems(): for k, v in items.iteritems(): row = [] row.append(user) row.append(k) row.append(v) arr.append(row) return arr print "test" training, test, metadata = parse.load(1) start = clock() d = makeDic(training) for k, v in d.items(): print "user %s" % k, v arr = makeArray(d) end = clock() print "time: %s" % (end - start) # for i in arr: # print i
#!/usr/bin/env python import sys import sqlite3 from parse import load # load_db.py data/*.2014-03-22.msgpack paths = sys.argv[1:] assert paths conn = sqlite3.connect('data/textpile.db') cur = conn.cursor() # load postings (unknown labels) sql = 'insert into doc (title, body, url, published_date) values (?,?,?,?)' cur.executemany(sql, ((doc['title'], doc['desc'], doc['url'], doc['published']) for doc in load(paths))) print "loaded %d docs" % cur.rowcount cur.close() conn.commit()