diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..2ef5e28 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..31e2352 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vscode +.idea +env +science diff --git a/README.md b/README.md new file mode 100644 index 0000000..4c754d9 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# example-datascience \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/__pycache__/Hello.cpython-37.pyc b/__pycache__/Hello.cpython-37.pyc new file mode 100644 index 0000000..2052a38 Binary files /dev/null and b/__pycache__/Hello.cpython-37.pyc differ diff --git a/__pycache__/app.cpython-37.pyc b/__pycache__/app.cpython-37.pyc new file mode 100644 index 0000000..02b00fd Binary files /dev/null and b/__pycache__/app.cpython-37.pyc differ diff --git a/__pycache__/config.cpython-37.pyc b/__pycache__/config.cpython-37.pyc new file mode 100644 index 0000000..5d53ca4 Binary files /dev/null and b/__pycache__/config.cpython-37.pyc differ diff --git a/__pycache__/db.cpython-37.pyc b/__pycache__/db.cpython-37.pyc new file mode 100644 index 0000000..105c6e4 Binary files /dev/null and b/__pycache__/db.cpython-37.pyc differ diff --git a/__pycache__/run.cpython-37.pyc b/__pycache__/run.cpython-37.pyc new file mode 100644 index 0000000..82c821c Binary files /dev/null and b/__pycache__/run.cpython-37.pyc differ diff --git a/app.py b/app.py new file mode 100644 index 0000000..b870466 --- /dev/null +++ b/app.py @@ -0,0 +1,9 @@ +from flask import Blueprint +from flask_restful import Api +from resources.Sales import Popularity + +api_bp = Blueprint('api', __name__) +api = Api(api_bp) + +# Route +api.add_resource(Popularity, '/sales/popularity') \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4a9e575 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +flask +flask_restful +flask_mysql \ No newline at end of file diff --git a/resources/Sales.py b/resources/Sales.py new file mode 100644 index 0000000..e7644cf --- /dev/null +++ b/resources/Sales.py @@ -0,0 +1,72 @@ +from flask import jsonify, render_template, make_response +from flask_restful import Resource +import pandas as pd +import numpy as np +import time +import turicreate as tc +import sqlalchemy as sql +from sklearn.model_selection import train_test_split +import json + +def load_data(): + try: + connect_string = 'mysql://root:123456@127.0.0.1:3306/example_science' + sql_engine = sql.create_engine(connect_string) + query = "select * from Transactions" + return pd.read_sql_query(query, sql_engine) + except Exception as e: + print(e) + +def model(train_data, name, user_id, item_id, target, n_rec): + if name == 'popularity': + model = tc.popularity_recommender.create(tc.SFrame(train_data), + user_id=user_id, + item_id=item_id, + target=target) + elif name == 'cosine': + model = tc.item_similarity_recommender.create(train_data, + user_id=user_id, + item_id=item_id, + target=target, + similarity_type='cosine') + elif name == 'pearson': + model = tc.item_similarity_recommender.create(train_data, + user_id=user_id, + item_id=item_id, + target=target, + similarity_type='pearson') + + recom = model.recommend(k=n_rec) + return recom + +def split_data(data): + train, test = train_test_split(data, test_size = .2) + train_data = train + return train_data + +class Popularity(Resource): + def get(self): + try: + transactions = load_data() + data = pd.melt(transactions.set_index('customerId')['product_name'].apply(pd.Series).reset_index(), + id_vars=['customerId'], + value_name='product_name').groupby(['customerId', 'product_name']) \ + .agg({'product_name' : 'count'}) \ + .rename(columns={'product_name' : 'product_count'}) \ + .reset_index() \ + .sort_values(by='product_count', ascending=False) + + name = 'popularity' + user_id = 'customerId' + item_id = 'product_name' + target = 'product_count' + n_rec = 10 # number of items to recommend + train_data = split_data(data) + result = model(train_data, name, user_id, item_id, target, n_rec) + df_result = pd.DataFrame(result) + headers = {'Content-Type': 'text/html'} + return make_response(render_template('view.html', tables=[df_result.to_html(classes='data', header="true")]),200,headers) + except Exception as e: + print(e) + return { "success" : False, "message" : "Internal server error" } + \ No newline at end of file diff --git a/resources/__init__.py b/resources/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/resources/__pycache__/Hello.cpython-37.pyc b/resources/__pycache__/Hello.cpython-37.pyc new file mode 100644 index 0000000..d09e95c Binary files /dev/null and b/resources/__pycache__/Hello.cpython-37.pyc differ diff --git a/resources/__pycache__/Sales.cpython-37.pyc b/resources/__pycache__/Sales.cpython-37.pyc new file mode 100644 index 0000000..dd762f8 Binary files /dev/null and b/resources/__pycache__/Sales.cpython-37.pyc differ diff --git a/resources/__pycache__/__init__.cpython-37.pyc b/resources/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..6793c01 Binary files /dev/null and b/resources/__pycache__/__init__.cpython-37.pyc differ diff --git a/run.py b/run.py new file mode 100644 index 0000000..7d9322d --- /dev/null +++ b/run.py @@ -0,0 +1,14 @@ +from flask import Flask + +def create_app(config_filename): + app = Flask(__name__) + + from app import api_bp + app.register_blueprint(api_bp, url_prefix='/api') + + return app + + +if __name__ == "__main__": + app = create_app("config") + app.run(debug=True) \ No newline at end of file diff --git a/templates/view.html b/templates/view.html new file mode 100644 index 0000000..c3f2f0b --- /dev/null +++ b/templates/view.html @@ -0,0 +1,13 @@ + + + + + Popularity + + + +{% for table in tables %} + {{ table|safe }} +{% endfor %} + + \ No newline at end of file