-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
122 lines (87 loc) · 3.22 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from flask import Flask, render_template, jsonify, request, session
from waitress import serve
import pandas as pd
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import numpy as np
from sklearn.impute import SimpleImputer
from sklearn.cluster import KMeans
app = Flask(__name__)
data = pd.read_csv("static/data/diabetes.csv")
data.drop(columns=['Outcome'], inplace=True)
imputer = SimpleImputer(missing_values=np.nan, strategy='median')
processed_data = imputer.fit_transform(data)
scaler = StandardScaler()
processed_data = scaler.fit_transform(processed_data)
k=3
di=4
@app.route('/')
def index():
return render_template('index.html')
@app.route('/pca_data')
def get_pca_data():
pca = PCA()
pca.fit(processed_data)
explained_variance_ratio = pca.explained_variance_ratio_
explained_variance_cumsum = explained_variance_ratio.cumsum()
chart_data = {
"explained_variance_ratio": explained_variance_ratio.tolist(),
"explained_variance_cumsum": explained_variance_cumsum.tolist()
}
return jsonify(chart_data)
@app.route('/biplot_data')
def biplot_data():
global di, k
pca = PCA()
pca.fit(processed_data)
pc_scores = pca.transform(processed_data)
loadings = pca.components_
kmeans = KMeans(n_clusters=k, random_state=0)
clusters = kmeans.fit_predict(processed_data)
biplot_data = {
'scores': pc_scores.tolist(),
'loadings': loadings.tolist(),
'feature_names': data.columns.tolist(),
'observation_names': data.index.tolist(),
'clusters': clusters.tolist()
}
return jsonify(biplot_data)
@app.route('/elbow_plot_data')
def get_elbow_plot_data():
distortions = []
K_range = range(1, 11)
for k in K_range:
kmeans = KMeans(n_clusters=k, random_state=0)
kmeans.fit(processed_data)
distortions.append(kmeans.inertia_)
return jsonify({'K_range': list(K_range), 'distortions': distortions})
@app.route('/set_di', methods=['POST'])
def set_dimensionality_index():
request_data = request.get_json()
global di
di = request_data['di']
return jsonify({'message': 'Dimensionality index set successfully'})
@app.route('/get_top_attributes')
def get_top_attributes():
global di, k
pca = PCA(n_components=di)
pca.fit(processed_data)
loadings = pca.components_
squared_sum_loadings = np.sum(loadings ** 2, axis=0)
top_indices = np.argsort(squared_sum_loadings)[::-1][:4]
top_attributes = [data.columns[i] for i in top_indices]
top_loadings = list(squared_sum_loadings[top_indices])
top_attribute_data = data[top_attributes]
kmeans = KMeans(n_clusters=k, random_state=0)
clusters = kmeans.fit_predict(top_attribute_data)
top_attribute_data['cluster'] = clusters
data_list = top_attribute_data.to_dict(orient='records')
return jsonify({'top_attributes': top_attributes, 'top_loadings':top_loadings, 'data': data_list})
@app.route('/update_k', methods=['POST'])
def update_k():
print(request.get_json())
global k
k = request.json.get('k')
return jsonify({'success': True}), 200
if __name__ == '__main__':
serve(app, host='0.0.0.0', port=8080)