From 956ce8fe696b03f8ccf7aaef49e8043084c9cd8e Mon Sep 17 00:00:00 2001 From: Rizal Hidayat Date: Mon, 25 Nov 2019 12:55:37 +0700 Subject: [PATCH] Initial commit --- .DS_Store | Bin 0 -> 6148 bytes .gitignore | 4 + README.md | 1 + __init__.py | 0 __pycache__/Hello.cpython-37.pyc | Bin 0 -> 745 bytes __pycache__/app.cpython-37.pyc | Bin 0 -> 370 bytes __pycache__/config.cpython-37.pyc | Bin 0 -> 375 bytes __pycache__/db.cpython-37.pyc | Bin 0 -> 1469 bytes __pycache__/run.cpython-37.pyc | Bin 0 -> 784 bytes app.py | 9 +++ requirements.txt | 3 + resources/Sales.py | 72 ++++++++++++++++++ resources/__init__.py | 0 resources/__pycache__/Hello.cpython-37.pyc | Bin 0 -> 3101 bytes resources/__pycache__/Sales.cpython-37.pyc | Bin 0 -> 2468 bytes resources/__pycache__/__init__.cpython-37.pyc | Bin 0 -> 163 bytes run.py | 14 ++++ templates/view.html | 13 ++++ 18 files changed, 116 insertions(+) create mode 100644 .DS_Store create mode 100644 .gitignore create mode 100644 README.md create mode 100644 __init__.py create mode 100644 __pycache__/Hello.cpython-37.pyc create mode 100644 __pycache__/app.cpython-37.pyc create mode 100644 __pycache__/config.cpython-37.pyc create mode 100644 __pycache__/db.cpython-37.pyc create mode 100644 __pycache__/run.cpython-37.pyc create mode 100644 app.py create mode 100644 requirements.txt create mode 100644 resources/Sales.py create mode 100644 resources/__init__.py create mode 100644 resources/__pycache__/Hello.cpython-37.pyc create mode 100644 resources/__pycache__/Sales.cpython-37.pyc create mode 100644 resources/__pycache__/__init__.cpython-37.pyc create mode 100644 run.py create mode 100644 templates/view.html diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..2ef5e2851f403ce40d63a2076cf8fd3f50e709b7 GIT binary patch literal 6148 zcmeHK%Wl&^6upzAsgtTA3naS38zdGHiVG4{s8SjNQg=v3umF_Wanx9HJSKLUmJk#P z@dqq`H5)#G9f%bx*s-xHUgY)Qn+4FCPonfnT@(PbLFN`~VNe+V!LhjrO zLOq-gI3A>((D)6Wqp(Dr1GgVXJl=;_FpCgvfN8O8s)=d&bZT1S(b(}6(0_`xcD!&C zWfU+9{6`9i{lP{OSl2jFD7Ow|@(KVfpjsNr;$uN@Y>jn|6NT`=n4$s|mFX)6Q*_kZ zIB|SxZ)W<2!sOf0zAeLv)fJlDC}0$rRUoe(E8_gW`t$q$EXh0>1&jj! zlme{KbejzfNuR9?gA-@1jkJd(OvFtTN(wT49Ls_@iZ_s?!DovCu&!~U5H&FKAs}Ti KnNi@6D)1G4Jk62- literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..2052a388c7dd8ce2435c390a6cf897def20a1c2b GIT binary patch literal 745 zcmYjPO>fgc5ZwX5ZWO^KQ3IFfR9hfA~%b`Qryy5g^&cG>;Jk5mb_b zCX{;4BrABrYtCg*gh@!rOCkahUJwxiU-P6P8Y9wcUSc)rQP=!rN;RDmem8G_$;oYuv91?yY=dBQ(3>U->b4 zPZT@G5gywZCfyun#+I|x>P(sCz0uy`UVj`ti1v?O528^f!9<4#gM)GOX#45@^6r7M zpj9R#1Nt-Q2(&JBk0ni0;{W1flTShA z5Z9OZaK3Q-1Z>0K-3y7eN zH;h8L>g80TGJw+k>$h1t#(Sf%_7Mf;>j@Vgu-o^>i=dqlv%N0?H@jf>;UQyE6s>BY zD8@NO7f;T|VR@yr4))3L!TgfMsU6ScJ%UGxRXbRDZOguawnO=BeT`00yey%{&ejsk f3fdB+a&F^>F*$Fn?hRaU3d8WhD^4d$c_MxRWr}1` literal 0 HcmV?d00001 diff --git a/__pycache__/config.cpython-37.pyc b/__pycache__/config.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5d53ca4b4fe68635837e165c1051fda0661efec0 GIT binary patch literal 375 zcmYk2&q~8U5XN`g)I_0p5xjZxP*4}IMWkuk(qR8!lL{`#vfbU<*d*I_QlvhOJ^Df( za`ogZcyb#NI>Ua<&dditrdh2XArtk7&!=;Y(AVzs9~DzH%!3Of5=k5*iWGXm@1;V^ z%G%;BrWIMD2eM3UX;DYw0M#nr#(@T%@a;O;z%s5AvIezEd7|nI4)Cg1vJq>+m2z+WMw!k#;fO`}*bM(v%&wCQnh-*bCjx7~kaZLd3| zXM2rsTY;xx&(Q+NDTXR?hjuFQ?!IsHi-wJG7 literal 0 HcmV?d00001 diff --git a/__pycache__/db.cpython-37.pyc b/__pycache__/db.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..105c6e4871f58ddcad1c56728d04ba2749a02f81 GIT binary patch literal 1469 zcmb7^&u`l{6vs)5l4Z|H)AiSW07Xv&1QapaZS5r}lGN#XXx%ny(Nx$Vs3M&xmL-Rj zkvNchN$&j@;$#1X{UZZ--D&^APJ53|0wX<*2C2vQW z#>qubcJa zpJ#oIHBzxNh|efRi_gfE)vPA5D+o&J8LrB*if+9800QFf#@mg}jc_Y|7jA7w-h+of z|MKgj=+2ek>y0pc|7yD%UB43OZklFZrb)h6>aTM?e-*wBJmVyzk@3>9=x4je9ZvK? zZv3pssu%+e2%vV;IyV49;25rD-ZxBqR7(Kh3sEcZ`1f(=ot8>>bXH8tPPa_QLs?Y1 zb6Bc@9wn*Nom38VRSr5j&18|vj`+AbnixNh?TT>|aW2C^XlwpsE#|Z1@2;G~>ct;= zDV_p07y38Q?TP#jMosp~8J&^O&!^7hX6>A@n$~Ov^kowU-sU_YOhky+c(NyUzm8 zA_1afrAuWR@<^uRN=ANPR_R`n=cc)Jl*$p7lW|8XE2_X%sM)v&W2q*Fi=;}_4fIfH z$#-p7WhE)l(k2_pKuKBbd(O%W9ZnZ#Q`jA0So&CK>jBdKF8vwc?mUw3e;^F0>?_XLG?X)v62?_{s7av&~+OI z0!mS@ReS3zwU|1fJt%4@s3IB4I5w>~9+qO9quq|>O4Qk(o$2ABn@X-f9ZH3J)$?whnnzt=f-N!3|5)k2v7M3a?(fpv|zb+@&1jugy_B!EI~&!wPM~ K=jE~GyZ-^u%17-0 literal 0 HcmV?d00001 diff --git a/__pycache__/run.cpython-37.pyc b/__pycache__/run.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..82c821cf1944dcbf0eef2de2f850d91a966154fe GIT binary patch literal 784 zcmZWn%Z}496t(kenrUZvXqa#4g60PbqNRgCng>ieLKGn@jop$qiG%HkDQZ_`JKw;t zWX0dG@OD=73#_%%t-p=*=;9X5bx@JbUXqBui2 z9%F2SS}1W zUPf<@5^Q)F&h7ggzH$yhA%!=1jaPVXUD+%2-AG|r+R5Fug%R-H$}$EnAybVFTHDk$ z)zs^uThsvL*IW}&P@|n5TG>~?8pRa`bFC9$Y=9{*E>kiozc?WP$b(8`q!etLUT8;f zuJps>cVL?g`=kD#A03kO=lhFF#D$;2sVSom>`p^Vx>q6 zCCoW$KP^&4pswTSy{!hBrWrG`;vQ6N=ezk*e#(9zu@BVmC*iqdLWVLemOLEtq{>;L zWcZnjg)HNQ$uMCHsrVwKv5FtdBxOayLQxgna;bwvuvjs(sxc=YK|t8YEvtohEzb&Y z{aAq`4&aaJGLO@O5U~sC$XpT1Ce`eu<7;ng7?PwcJ e)Lr}&i~;TUa!#v^JvApT&Gm1ABY)oqXMX_Tn8`N) literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..d09e95c6f9a76dc2f64f74ff48228d4b4f3cc1d2 GIT binary patch literal 3101 zcmcIm&668P6`!6DeT<}CMTza$364ndVFi%@LjZbZTm9!_MK7xORU)$G1EL$Rhb>kQEfdC zs+!4E?4i-uGRewV3#DUK=b1is$K#X+(L-A&$NUUM?Lo}1a<+7|vtkS9DO)n`E?w=e zyoI}9Pu+bC(Ed><-(}7cPC4%CVBtN%CVoxMg6>oAL+3Y6>3)b7mgnsq-z?_pn{VB|cjM*{$Jg=RxO3~)_1hEiI4SB}#4621nTq2pO5`Hd!#9QpvMPq} z%cN9Es_~V(7bJPC$<5bi%$^^B{NFQ{*S@}e6T(2hYiaf8zHI|WASQbxo(jzbLKsd~0J_}L0tj{Fr(qGc5|LG;|`TLPw3X zf{Vn+m$W&#OQH`E?W(NE))T~fUW=$%i$nsb046)aQMx*fE}s4+mLjbRIGGEHS?vi& zuh`$6ndxk&_O^l5(iz9H`b3S|CS2p5+;03bDa84;1ttA0YL_w35E#ka1VzO~9tCzv zi-5K0(ta*LTtrdVARN}h>*5W!wdd~*km=dy90oSNy+(Vegw!0NSkyCZY)@TMA zI=oh+Z=%Nc(RfF~BcrX|RvrjFlcm}!PaQbCrql8MxiOYe))SwzPXF{rdcOGah(+x+ z@npwha3aQ?$R$66RXxj^z6pP|h4%J*XwEGe{sWEq@3r&9Su#y=$5tN4*>Lx= zv+|($-iA!|~23(#9CHH}Xv@wr-AN6UH$}XNJ@l zn{JG}mE^R6IF>h`|0(4oX^H4EYHy<`f_aNcYX9Hx-+AziXcNkh+Yq1#-G;zl(3n5` zIzvEr7Va5vcLDA{Y;G0<*zC;$;BND91Mc(pi_V_Bf>p{_AV#jF!;s$~LG&tLBQ29d ze1KRBPz%qmM5g=}4f{3?>zyJ}euuQ*B|+pbMwf^rp0hv?MKorC#LfW8-GFHD{CRDFByZ8wZ<2V6#8+bGHmn@w8D^+Gx?+NZJW)rI zfMj~Mj+ChM!7P^^bWvlSF>F{yK{+exdE}NgXgVuI6e3+@>m0o%_1-4IWYrkAD&uCA z3}_m^rVAV0w30l1D2jP>Ngd_52$W-+apU!E4>+_L0{6SQGJc)tho*th2f?h*f>N+$ tdy9EVn>Z7e+XykOx7RUsmlCfcBy@4lqKr-N0EJpzx7BW4YVG!~{TB(Q^85e* literal 0 HcmV?d00001 diff --git a/resources/__pycache__/Sales.cpython-37.pyc b/resources/__pycache__/Sales.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..dd762f857004fbb381a05bbf6e34d769417b2bc1 GIT binary patch literal 2468 zcmZWr%Z}W}6=m_!WOKUbrH5n3HfCW2Xv3b?BT)h&a0FX%Y#10AmV-Ej1O&yZX>pn_ zRTX9Rfb?d($Ywskh8Hu-{6jt<`2}W~m4K|g^vdT}OJhl9ql#6xZWZtAoa*j8~{zsE%kB`9}H2n{B!V#=+O2%2~a<}xj$4KTDei`tv4DHwEh*)+hE zbN$9|_pjl1{r1h9*KW<_;}nj_L}$6IGI{)(mW9mB-rIYJsxJ3FS81iw%;a!xHX1yf z$ETT+@H~;#BCq7&HiB&vqWmr4T|z6I)}N;XEEA@Z8Nn6Nj4%mo(L^Ovnf#+bWE_NtKKrs z5j%2`8jn(ggm|4V+*7|FRqsK^gU(&(_(yK%JYDsKe;Q!@O{@o4zyH#DuwL&*M?r^* zJY{{@g-Ba?FvLyWv_-0Nv!cpt5r`xgLztuHXfQIVT1dl3S&i_>nXh(WtJ70wCgWFs`U4|E2xxb0OTitaL+{uMfhMQjrZbCIwJ zKN`I_j@${edoo7)cnDY2Y!ZKgfpI#}3DV{M&FxtaH1%Fb;Pws{-+n{SfBrCIe7weJ zehdsNs)(6C_~Z)@tY3pq3hF9WUb1j~B6Gxa*=7m&x;anbIeX>w9J zoC$}!WF9!kvz+4(@uwFBaN!oA;({)|$7J;{GpNjtoq2+tYHV-)0!s*kjfVsIl^@f5@(#yw8#QRRIgP{$(fL$lGj zC#;Xmq>BgvUC@oX@MP4F4Lh55V~|!%9GbI!!)%%@G40}`ZLuk~#P+EROLk6OvD*{<2`CPZfV{v~nwJJ1~{ZSvLR1xmYez~ih1=uui93qEO7Em{x? z)!NMEVJ?zKsB<1aN{d!n@epKnTNyrmrfa9bL73G=TUPqRnLD`mSA3l63;+Nzscl5} z3B~$&@4T`7@)xrdvTBKS_J&YcJ5T!z0 z0j>8zM^FAu2VVIY=cy_!_5kWfQte5lYDKo|HUp{YAuQ3S3pu-Fxk(Wxale#>;bSdL zk^`iVd5|_uu>wY=%BAKT;J#;TL%2|N+rT9+T`YzX?kj@vhOU)Ccn}zEtJ0!|kME+w zEzlK#MtStP(A0_bZA>DH#B4hFxEK!k1X#GZ5UlU&6;gVUffV7+LWRDsTQXi8(j|%E z5UyjR75)SK3`-uE?Hc5okVIrXVy^>8yI_EAH+FrD*T2ZhBDM#hMeKOWmx_co zRyJzG)Jn-ebJ!Ltz$za#X(dvzq$4)1%GXS*u8=z2hG~&KkmX9z z9mHSJ%L1%j?OV1?*4HvFRLf0+Pya{O6+(=;&M4XV95-D!Geo~%yZ;`Mq^9It#`Q;x Oy8dD8M&sye6#NL2lzX%Q literal 0 HcmV?d00001 diff --git a/resources/__pycache__/__init__.cpython-37.pyc b/resources/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6793c0199175f714dd61795d6e38d0e691c35026 GIT binary patch literal 163 zcmZ?b<>g`kf-t^Iu^{>}h=2h`Aj1KOi&=m~3PUi1CZpd2kU0jl%t)G%ulBippoSB-JoT^`xTAW{6 i1d@)A&& + + + + Popularity + + + +{% for table in tables %} + {{ table|safe }} +{% endfor %} + + \ No newline at end of file