-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 956ce8f
Showing
18 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.vscode | ||
.idea | ||
env | ||
science |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# example-datascience |
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
flask | ||
flask_restful | ||
flask_mysql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |