Skip to content

Commit 76cc760

Browse files
committed
2 parents a89457b + f2e0d7a commit 76cc760

File tree

13 files changed

+285
-21222
lines changed

13 files changed

+285
-21222
lines changed

__pycache__/app.cpython-38.pyc

3.98 KB
Binary file not shown.

__pycache__/app.cpython-39.pyc

1.52 KB
Binary file not shown.

__pycache__/forms.cpython-39.pyc

125 Bytes
Binary file not shown.

app.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
from authlib.integrations.flask_client import OAuth
44
import os
55
from datetime import timedelta
6+
#from forms import ProfileForm
67
import pandas as pd
8+
#from forms import ProfileForm
9+
#from kmeans import K_cluster
10+
import io
11+
import base64
12+
13+
from werkzeug.utils import send_file
14+
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
715
import numpy as np
816
import matplotlib.pyplot as plt
917
from sklearn.metrics import silhouette_score
@@ -60,9 +68,19 @@ def cluster(df, col):
6068
plt.xlabel(col[0])
6169
plt.ylabel(col[1])
6270
ax.set_zlabel(col[2])
63-
plt.show()
64-
image_path = os.path('image'+'.png')
65-
plt.savefig(image_path)
71+
canvas = FigureCanvas(fig)
72+
img = io.BytesIO()
73+
plt.savefig('static/img.png')
74+
return 'static/img.png'
75+
76+
'''
77+
plt.savefig(img)
78+
img.seek(0)
79+
return send_file(img, mimetype='img/png')
80+
81+
'''
82+
83+
6684

6785
def silhoutte(data):
6886
km_silhouette = []
@@ -75,6 +93,7 @@ def silhoutte(data):
7593
cluster_n = int(km_silhouette.index(max(km_silhouette)))+1
7694
return cluster_n
7795

96+
7897
@app.route('/')
7998
def index():
8099
return render_template('login.html')
@@ -86,10 +105,20 @@ def home():
86105
email = dict(session)['profile']['email']
87106
if request.method == 'POST':
88107
file = pd.read_csv(request.files['file'])
89-
col =['CustomerID','Age','Income']
90-
cluster(file,col)
108+
col =['Age','Income','Score']
109+
image = cluster(file,col)
110+
return render_template('image.html', image = image)
91111
return render_template('index.html')
92112

113+
114+
'''
115+
@app.route('/profile')
116+
@login_required
117+
def profile():
118+
form = ProfileForm()
119+
return render_template('profile.html',title = 'Register',form=form)
120+
'''
121+
93122
@app.route('/login')
94123
def login():
95124
google = oauth.create_client('google') # create the google oauth client
@@ -99,18 +128,15 @@ def login():
99128

100129
@app.route('/authorize')
101130
def authorize():
102-
google = oauth.create_client('google') # create the google oauth client
103-
token = google.authorize_access_token() # Access token from google (needed to get user info)
104-
resp = google.get('userinfo') # userinfo contains stuff u specificed in the scrope
131+
google = oauth.create_client('google')
132+
token = google.authorize_access_token()
133+
resp = google.get('userinfo')
105134
user_info = resp.json()
106-
user = oauth.google.userinfo() # uses openid endpoint to fetch user info
107-
# Here you use the profile/user data that you got and query your database find/register the user
108-
# and set ur own data in the session not the profile from google
135+
user = oauth.google.userinfo()
109136
session['profile'] = user_info
110-
session.permanent = True # make the session permanant so it keeps existing after broweser gets closed
137+
session.permanent = True
111138
return redirect('/home')
112139

113-
114140
@app.route('/logout')
115141
def logout():
116142
for key in list(session.keys()):

forms.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from flask_wtf import FlaskForm
2+
from wtforms import StringField, SubmitField
3+
from wtforms.validators import DataRequired, Length, Email
4+
5+
class ProfileForm(FlaskForm):
6+
name = StringField('Name', validators=[DataRequired(),Length(min =2,max=20)])
7+
email = StringField('Email',validators =[DataRequired(),Email()])
8+
company = StringField('Company',validators =[DataRequired()])
9+
location = StringField('Location')
10+
submit = SubmitField('Sign Up')

kmeans.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from app import cluster
12
import numpy as np
23
import pandas as pd
34
import matplotlib.pyplot as plt
@@ -8,35 +9,42 @@
89
import numpy as np
910
import pandas as pd
1011

11-
def cluster(df, col):
12-
data = pd.DataFrame()
13-
for j in col:
14-
data[j]=df[j]
15-
cluster_n =silhoutte(data)
16-
km = KMeans(n_clusters=cluster_n)
17-
clusters = km.fit_predict(data)
18-
df["label"] = clusters
19-
fig = plt.figure(figsize=(20,10))
20-
ax = fig.add_subplot(111, projection='3d')
21-
colors = ['blue','red','yellow','orange','green','purple']
22-
count = 0
23-
for i in range(cluster_n):
24-
ax.scatter(df[col[0]][df.label == i], df[col[1]][df.label == i], df[col[2]][df.label == i], c=colors[count], s=60)
25-
count+=1
26-
ax.view_init(30, 185)
27-
plt.xlabel(col[0])
28-
plt.ylabel(col[1])
29-
ax.set_zlabel(col[2])
30-
plt.show()
12+
class K_cluster():
13+
14+
def __init__(self, df, cluster_n, col):
15+
self.df = df
16+
self.col = col
17+
self.cluster_n = cluster_n
3118

32-
def silhoutte(data):
33-
km_silhouette = []
34-
for k in range(2,11):
35-
kmeans = KMeans(n_clusters=k, init="k-means++")
36-
kmeans.fit(data)
37-
preds = kmeans.predict(data)
38-
silhouette = silhouette_score(data,preds)
39-
km_silhouette.append(silhouette)
40-
cluster_n = int(km_silhouette.index(max(km_silhouette)))+1
41-
return cluster_n
19+
def cluster(df, col):
20+
data = pd.DataFrame()
21+
for j in col:
22+
data[j]=df[j]
23+
cluster_n = silhoutte(data)
24+
km = KMeans(n_clusters=cluster_n)
25+
clusters = km.fit_predict(data)
26+
df["label"] = clusters
27+
fig = plt.figure(figsize=(20,10))
28+
ax = fig.add_subplot(111, projection='3d')
29+
colors = ['blue','red','yellow','orange','green','purple']
30+
count = 0
31+
for i in range(cluster_n):
32+
ax.scatter(df[col[0]][df.label == i], df[col[1]][df.label == i], df[col[2]][df.label == i], c=colors[count], s=60)
33+
count+=1
34+
ax.view_init(30, 185)
35+
plt.xlabel(col[0])
36+
plt.ylabel(col[1])
37+
ax.set_zlabel(col[2])
38+
plt.show()
39+
40+
def silhoutte(data):
41+
km_silhouette = []
42+
for k in range(2,11):
43+
kmeans = KMeans(n_clusters=k, init="k-means++")
44+
kmeans.fit(data)
45+
preds = kmeans.predict(data)
46+
silhouette = silhouette_score(data,preds)
47+
km_silhouette.append(silhouette)
48+
cluster_n = int(km_silhouette.index(max(km_silhouette)))+1
49+
return cluster_n
4250

0 commit comments

Comments
 (0)