diff --git a/samples/features/machine-learning-services/python/getting-started/rental-prediction/rental_prediction.py b/samples/features/machine-learning-services/python/getting-started/rental-prediction/rental_prediction.py index 9e5b6e1e..f424e8da 100644 --- a/samples/features/machine-learning-services/python/getting-started/rental-prediction/rental_prediction.py +++ b/samples/features/machine-learning-services/python/getting-started/rental-prediction/rental_prediction.py @@ -1,6 +1,6 @@ -import pandas as pd -from sklearn.linear_model import LinearRegression -from sklearn.metrics import mean_squared_error +import pandas +import sklearn.linear_model +import sklearn.metrics from revoscalepy.computecontext.RxInSqlServer import RxInSqlServer from revoscalepy.computecontext.RxInSqlServer import RxSqlServerData @@ -9,43 +9,36 @@ from revoscalepy.etl.RxImport import rx_import_datasource def get_rental_predictions(): conn_str = 'Driver=SQL Server;Server=MYSQLSERVER;Database=TutorialDB;Trusted_Connection=True;' - column_info = { - "Year" : { "type" : "integer" }, - "Month" : { "type" : "integer" }, - "Day" : { "type" : "integer" }, - "RentalCount" : { "type" : "integer" }, - "WeekDay" : { - "type" : "factor", - "levels" : ["1", "2", "3", "4", "5", "6", "7"] - }, - "Holiday" : { - "type" : "factor", - "levels" : ["1", "0"] - }, - "Snow" : { - "type" : "factor", - "levels" : ["1", "0"] - } + column_info = { + "Year": {"type": "integer"}, + "Month": {"type": "integer"}, + "Day": {"type": "integer"}, + "RentalCount": {"type": "integer"}, + "WeekDay": { + "type": "factor", + "levels": ["1", "2", "3", "4", "5", "6", "7"], + }, + "Holiday": { + "type": "factor", + "levels": ["1", "0"], + }, + "Snow": { + "type": "factor", + "levels": ["1", "0"], } + } data_source = RxSqlServerData(table="dbo.rental_data", - connectionString=conn_str, colInfo=column_info) - computeContext = RxInSqlServer( - connectionString = conn_str, - numTasks = 1, - autoCleanup = False - ) - - + connectionString=conn_str, + colInfo=column_info) RxInSqlServer(connectionString=conn_str, numTasks=1, autoCleanup=False) - + # import data source and convert to pandas dataframe - df = pd.DataFrame(rx_import_datasource(data_source)) + df = pandas.DataFrame(rx_import_datasource(data_source)) print("Data frame:", df) - # Get all the columns from the dataframe. - columns = df.columns.tolist() - # Filter the columns to remove ones we don't want. - columns = [c for c in columns if c not in ["Year"]] + # Get all the columns from the dataframe and filter out the ones we don't + # want. + columns = [x for x in df.columns if x == "Year"] # Store the variable we'll be predicting on. target = "RentalCount" # Generate the training set. Set random_state to be able to replicate results. @@ -56,14 +49,16 @@ def get_rental_predictions(): print("Training set shape:", train.shape) print("Testing set shape:", test.shape) # Initialize the model class. - lin_model = LinearRegression() + lin_model = sklearn.linear_model.LinearRegression() # Fit the model to the training data. lin_model.fit(train[columns], train[target]) # Generate our predictions for the test set. lin_predictions = lin_model.predict(test[columns]) print("Predictions:", lin_predictions) # Compute error between our test predictions and the actual values. - lin_mse = mean_squared_error(lin_predictions, test[target]) + lin_mse = sklearn.metrics.mean_squared_error(lin_predictions, test[target]) print("Computed error:", lin_mse) -get_rental_predictions() + +if __name__ == "__main__": + get_rental_predictions()