def main(): args = parse_options() client = Regression('127.0.0.1', 9199, '') # train num = 0 if args.traindata: with open(args.traindata, 'r') as traindata: for data in traindata: # skip comments if not len(data) or data.startswith('#'): continue num += 1 rent, distance, space, age, stair, aspect = map(str.strip, data.strip().split(',')) d = Datum({ 'aspect': aspect, 'distance': float(distance), 'space': float(space), 'age': float(age), 'stair': float(stair) }) train_data = [[float(rent), d]] # train client.train(train_data) # print train number print 'train ...', num # anaylze with open(args.analyzedata, 'r') as analyzedata: for data in analyzedata: if not len(data) or data.startswith('#'): continue distance, space, age, stair, aspect = map(str.strip, data.strip().split(',')) d = Datum({ 'aspect': str(aspect), 'distance': float(distance), 'space': float(space), 'age': float(age), 'stair': float(stair) }) analyze_data = [d] result = client.estimate(analyze_data) print 'rent ....', round(result[0], 1)
def main(): cl = Regression("127.0.0.1", 9199, "test") d = Datum() for i in xrange(10): d.add_number('x', 1) d.add_number('y', 4) cl.train([[10.0, d]]) d = Datum() d.add_number('x', 1) d.add_number('y', 4) result = cl.estimate([d]) print("{0:30.30f}".format(result[0]))
class RegressionTest(unittest.TestCase): def setUp(self): self.config = { "method": "PA", "converter": { "string_filter_types": {}, "string_filter_rules": [], "num_filter_types": {}, "num_filter_rules": [], "string_types": {}, "string_rules": [{"key": "*", "type": "str", "sample_weight": "bin", "global_weight": "bin"}], "num_types": {}, "num_rules": [{"key": "*", "type": "num"}] }, "parameter": { "sensitivity" : 0.1, "regularization_weight" : 3.402823e+38 } } TestUtil.write_file('config_regression.json', json.dumps(self.config)) self.srv = TestUtil.fork_process('regression', port, 'config_regression.json') try: self.cli = Regression(host, port, "name") except: TestUtil.kill_process(self.srv) raise def tearDown(self): if self.cli: self.cli.get_client().close() TestUtil.kill_process(self.srv) def test_get_client(self): self.assertTrue(isinstance(self.cli.get_client(), msgpackrpc.client.Client)) def test_get_config(self): config = self.cli.get_config() self.assertEqual(json.dumps(json.loads(config), sort_keys=True), json.dumps(self.config, sort_keys=True)) def test_train(self): d = Datum({"skey1": "val1", "skey2": "val2", "nkey1": 1.0, "nkey2": 2.0}) data = [[1.0, d]] self.assertEqual(self.cli.train(data), 1) def test_estimate(self): d = Datum({"skey1": "val1", "skey2": "val2", "nkey1": 1.0, "nkey2": 2.0}) data = [d] result = self.cli.estimate(data) def test_save(self): self.assertEqual(len(self.cli.save("regression.save_test.model")), 1) def test_load(self): model_name = "regression.load_test.model" self.cli.save(model_name) self.assertEqual(self.cli.load(model_name), True) def test_get_status(self): self.cli.get_status()
def main(): args = parse_options() client = Regression('127.0.0.1', 9199, '') # train num = 0 if args.traindata: with open(args.traindata, 'r') as traindata: for data in traindata: # skip comments if not len(data) or data.startswith('#'): continue num += 1 rent, distance, space, age, stair, aspect = map(str.strip, data.strip().split(',')) d = Datum({ 'aspect': aspect, 'distance': float(distance), 'space': float(space), 'age': float(age), 'stair': float(stair) }) train_data = [[float(rent), d]] # train client.train(train_data) # print train number print 'train ...', num # anaylze with open(args.analyzedata, 'r') as analyzedata: myhome = yaml.load(analyzedata) d = Datum({ 'aspect': str(myhome['aspect']), 'distance': float(myhome['distance']), 'space': float(myhome['space']), 'age': float(myhome['age']), 'stair': float(myhome['stair']) }) analyze_data = [d] result = client.estimate(analyze_data) print 'rent ....', round(result[0], 1)
def train(training_data): client = Regression("127.0.0.1", 9199, "boston") client.clear() for data in training_data: score, d = make_datum(data) client.train([[score, d]])
if __name__ == '__main__': options, remainder = parse_args() client = Regression(options.server_ip, options.server_port, '') print(client.get_config()) print(client.get_status()) train_list = cross_validation_python() data_train, data_test, label_train, label_test = train_test_split(train_list[0], train_list[1]) for label, dat in izip(label_train, data_train): print(dat[0]) data_dict = json.loads(dat[0]) datum = Datum(data_dict) client.train([[float(label), datum]]) print(client.get_status()) print(client.save("tutorial_regression")) print(client.load("tutorial_regression")) print(client.get_config()) count_ok = 0 count_ng = 0 for label, dat in izip(label_test, data_test): data_dict = json.loads(dat[0]) datum = Datum(data_dict) ans = client.estimate([datum])
class RegressionTest(unittest.TestCase): def setUp(self): self.config = { "method": "PA1", "converter": { "string_filter_types": {}, "string_filter_rules": [], "num_filter_types": {}, "num_filter_rules": [], "string_types": {}, "string_rules": [{ "key": "*", "type": "str", "sample_weight": "bin", "global_weight": "bin" }], "num_types": {}, "num_rules": [{ "key": "*", "type": "num" }] }, "parameter": { "sensitivity": 0.1, "regularization_weight": 3.402823e+38 } } TestUtil.write_file('config_regression.json', json.dumps(self.config)) self.srv = TestUtil.fork_process('regression', port, 'config_regression.json') try: self.cli = Regression(host, port, "name") except: TestUtil.kill_process(self.srv) raise def tearDown(self): if self.cli: self.cli.get_client().close() TestUtil.kill_process(self.srv) def test_get_client(self): self.assertTrue( isinstance(self.cli.get_client(), msgpackrpc.client.Client)) def test_get_config(self): config = self.cli.get_config() self.assertEqual(json.dumps(json.loads(config), sort_keys=True), json.dumps(self.config, sort_keys=True)) def test_train(self): d = Datum({ "skey1": "val1", "skey2": "val2", "nkey1": 1.0, "nkey2": 2.0 }) data = [[1.0, d]] self.assertEqual(self.cli.train(data), 1) def test_estimate(self): d = Datum({ "skey1": "val1", "skey2": "val2", "nkey1": 1.0, "nkey2": 2.0 }) data = [d] result = self.cli.estimate(data) def test_save(self): self.assertEqual(len(self.cli.save("regression.save_test.model")), 1) def test_load(self): model_name = "regression.load_test.model" self.cli.save(model_name) self.assertEqual(self.cli.load(model_name), True) def test_get_status(self): self.cli.get_status()