Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rizalkun committed Nov 25, 2019
0 parents commit 956ce8f
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vscode
.idea
env
science
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# example-datascience
Empty file added __init__.py
Empty file.
Binary file added __pycache__/Hello.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/app.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/config.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/db.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/run.cpython-37.pyc
Binary file not shown.
9 changes: 9 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -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')
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
flask
flask_restful
flask_mysql
72 changes: 72 additions & 0 deletions resources/Sales.py
Original file line number Diff line number Diff line change
@@ -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:[email protected]: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" }

Empty file added resources/__init__.py
Empty file.
Binary file added resources/__pycache__/Hello.cpython-37.pyc
Binary file not shown.
Binary file added resources/__pycache__/Sales.cpython-37.pyc
Binary file not shown.
Binary file added resources/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
14 changes: 14 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions templates/view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Popularity</title>
</head>
<body>

{% for table in tables %}
{{ table|safe }}
{% endfor %}
</body>
</html>

0 comments on commit 956ce8f

Please sign in to comment.