def write_rts_model(model, key, connection_string): RTS_odbc = RxOdbcData(connection_string, table="RTS") serialized_model = rx_serialize_model(model, realtime_scoring_only=True) rx_write_object(RTS_odbc, key=key, value=serialized_model, serialize=False, compress=None, overwrite=True)
model_type = "linear" conn_str = 'Driver=SQL Server;Server=<Server Name>;Database=MLDB;Uid=<User Name>;Pwd=<Password>;' cnxn = pyodbc.connect(conn_str) inputsql = 'select "RentalCount", "Year", "Month", "Day", "WeekDay", "Snow", "Holiday", "FWeekDay" from dbo.rental_data where Year < 2015' rental_train_data = pd.read_sql(inputsql, cnxn) # Used to sample data # train = rental_train_data.sample(frac=0.8, random_state=1) # test = rental_train_data.loc[~ rental_train_data.index.isin(train.index)] # print("Train {0} / test {1}".format(len(train), len(test))) rental_train_data["Holiday"] = rental_train_data["Holiday"].astype("category") rental_train_data["Snow"] = rental_train_data["Snow"].astype("category") rental_train_data["WeekDay"] = rental_train_data["WeekDay"].astype("category") if model_type == "linear": linmod_model = rx_lin_mod("RentalCount ~ Month + Day + WeekDay + Snow + Holiday", data = rental_train_data) trained_model = rx_serialize_model(linmod_model, realtime_scoring_only = True) if model_type == "dtree": dtree_model = rx_dtree("RentalCount ~ Month + Day + WeekDay + Snow + Holiday", data = rental_train_data) trained_model = rx_serialize_model(dtree_model, realtime_scoring_only = True) print(rx_summary("~ Month + Day + WeekDay + Snow + Holiday", rental_train_data)) # Dump learned model to file with open(r'c:\temp\trained_model.pickle', mode='wb') as f: f.write(trained_model) cursor=cnxn.cursor() cursor.execute("INSERT INTO rental_models(model_name, lang, native_model) VALUES(?, ?, ?)", (model_type + "_model", "Python", trained_model)) cnxn.commit()
conn_str = 'Driver=SQL Server;Server=SoL01.techsummit.local;Database=MLDB;Uid={0};Pwd={1};'.format("sa",os.getenv("SQLPass")) cnxn = pyodbc.connect(conn_str) inputsql =''' SELECT C1, C2 FROM ( VALUES ('A',1),('B',2),('C',3),('D',4),('E',5),('F',6),('G',7),('H',8),('I',9),('J',10), ('K',11),('L',12),('M',13),('N',14),('O',15),('P',16),('Q',17),('R',18),('S',19),('T',20), ('U',21),('V',22),('W',23),('X',24),('Y',25),('Z',26) ) AS T1(C1, C2) ''' train_data = pd.read_sql(inputsql, cnxn) train_data["C1"] = train_data["C1"].astype("category") print(train_data["C1"] ) # model = rx_lin_mod("C2 ~ C1", data = train_data) model = rx_fast_linear("C2 ~ C1", data = train_data, method = "regression") print(rx_summary("C2 ~ C1", train_data)) trained_model = rx_serialize_model(model, realtime_scoring_only = True) # Dump learned model to file with open(r'c:\temp\trained_model_{0}.pickle'.format(model_file), mode='wb') as f: f.write(trained_model) p_data = pd.DataFrame([0,5,10,99,4], columns=["C1"]) p_data["C1"] = train_data["C1"].astype("category") print(p_data["C1"]) pred = rx_predict(model, data = p_data) print(pred)