sourav11295 commited on
Commit
9afdcaf
·
1 Parent(s): f94a14e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -0
app.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+
5
+ from sklearn.preprocessing import LabelEncoder
6
+ from sklearn.pipeline import Pipeline
7
+ from sklearn.model_selection import GridSearchCV
8
+ from sklearn.model_selection import train_test_split
9
+ from sklearn.preprocessing import StandardScaler
10
+
11
+ from sklearn.linear_model import LinearRegression
12
+ from sklearn.svm import SVR
13
+ from sklearn.tree import DecisionTreeRegressor
14
+ from sklearn.ensemble import RandomForestRegressor
15
+
16
+ from sklearn.linear_model import LogisticRegression
17
+ from sklearn.neighbors import KNeighborsClassifier
18
+ from sklearn.svm import SVC
19
+ from sklearn.tree import DecisionTreeClassifier
20
+ from sklearn.ensemble import RandomForestClassifier
21
+
22
+ def read(file,dep,ord):
23
+ df = pd.read_csv(file.name)
24
+ cat = list()
25
+ dep_type = str(df.dtypes[dep])
26
+ for col in df.columns.values:
27
+ if str(df.dtypes[col]) == 'bool' or str(df.dtypes[col]) == 'object':
28
+ cat.append(col)
29
+ nom = list(set(cat).difference(set(ord)))
30
+ new_df = df.dropna(axis=0)
31
+ if str(ord) != "":
32
+ ord = ord.split(',')
33
+ le = LabelEncoder()
34
+ new_df[ord] = new_df[ord].apply(lambda col: le.fit_transform(col))
35
+ else:
36
+ pass
37
+ ohe_df = pd.get_dummies(new_df[nom], drop_first=True)
38
+ new_df.drop(columns=nom, axis=1,inplace=True)
39
+ new_df = pd.concat([new_df,ohe_df],axis=1)
40
+ if dep_type == 'bool' or dep_type == 'object':
41
+ text = "classification"
42
+ result = classification(new_df,dep)
43
+ else:
44
+ text = "regression"
45
+ result = regression(new_df,dep)
46
+ return df.sample(5),new_df.sample(5),result, text, cat, ord, nom
47
+
48
+ def classification(df,dep):
49
+ X = df.drop(dep,axis=1)
50
+ y = df[dep]
51
+
52
+ X_train, X_test, y_train, y_test = train_test_split(X, y)
53
+
54
+ scale = StandardScaler()
55
+
56
+ pipe = Pipeline(steps=[('scale',scale),('classification','pass')])
57
+
58
+ parameters = [
59
+ {
60
+ 'classification':[LogisticRegression()],
61
+ },
62
+ {
63
+ 'classification':[RandomForestClassifier()],
64
+ },
65
+ {
66
+ 'classification':[DecisionTreeClassifier()],
67
+ },
68
+ {
69
+ 'classification':[SVC()],
70
+ },
71
+ {
72
+ 'classification':[KNeighborsClassifier(n_neighbors=5)],
73
+ },
74
+ ]
75
+
76
+ search = GridSearchCV(pipe, param_grid=parameters, n_jobs=-1, scoring='accuracy')
77
+ search.fit(X_train,y_train)
78
+
79
+ result = pd.DataFrame(search.cv_results_)[['params','rank_test_score','mean_test_score']]
80
+
81
+ result['mean_test_score']= (result['mean_test_score'])*100
82
+ result = result.astype({'params': str})
83
+
84
+ result.sort_values('rank_test_score',inplace=True)
85
+ return result
86
+
87
+ def regression(df,dep):
88
+ X = df.drop(dep,axis=1)
89
+ y =df[dep]
90
+
91
+ X_train, X_test, y_train, y_test = train_test_split(X, y)
92
+
93
+ scale = StandardScaler()
94
+
95
+ pipe = Pipeline(steps=[('scale',scale),('regression','pass')])
96
+
97
+ parameters = [
98
+ {
99
+ 'regression':[LinearRegression()]
100
+ },
101
+ {
102
+ 'regression':[RandomForestRegressor()],
103
+ },
104
+ {
105
+ 'regression':[DecisionTreeRegressor()],
106
+ },
107
+ {
108
+ 'regression':[SVR()],
109
+ },
110
+ ]
111
+
112
+ search = GridSearchCV(pipe, param_grid=parameters, cv=5, n_jobs=-1, scoring='neg_mean_absolute_percentage_error')
113
+ search.fit(X_train,y_train)
114
+
115
+ result = pd.DataFrame(search.cv_results_)[['params','rank_test_score','mean_test_score']]
116
+
117
+ result['mean_test_score']= (result['mean_test_score']+1)*100
118
+ result = result.astype({'params': str})
119
+
120
+ result.sort_values('rank_test_score',inplace=True)
121
+ return result
122
+
123
+ with gr.Blocks() as demo:
124
+ gr.Markdown("Start typing below and then click **Run** to see the output.")
125
+ with gr.Column():
126
+ with gr.Row():
127
+ file = gr.File(label="Upload File(Comma Separated)")
128
+ dep = gr.Textbox(label="Dependent Variable(Variable as in the file)")
129
+ ord = gr.Textbox(label="Ordinal Variables(Seperate with a comma)")
130
+ submit = gr.Button("Submit")
131
+ text = gr.Text(label="Suitable Algorithm")
132
+ other1 = gr.Text(label="Categorical Variables")
133
+ other2 = gr.Text(label="Ordinal Vairables")
134
+ other3 = gr.Text(label="Nominal Variables")
135
+ with gr.Row():
136
+ org = gr.DataFrame(overflow_row_behaviour="paginate", label="Original Data")
137
+ converted = gr.DataFrame(overflow_row_behaviour="paginate", label="Transformed Data")
138
+ result = gr.DataFrame(label="Result")
139
+ submit.click(fn=read, inputs=[file,dep,ord], outputs=[org,converted,result,text,other1,other2,other3])
140
+ demo.launch(share=True)