-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSupervised_ML_Classifier_python.py
1115 lines (934 loc) · 55.5 KB
/
Supervised_ML_Classifier_python.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from collections import Counter
import re
# Tkinter
from tkinter import *
from tkinter import messagebox, filedialog, messagebox, ttk
from PIL import ImageTk, Image
#Metrics
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
#Misc
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.impute import SimpleImputer
#Feature Transformations
from sklearn.preprocessing import StandardScaler
#Classifiers
from sklearn import linear_model
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import Ridge, Lasso
from sklearn.model_selection import GridSearchCV
#Global Variables & Frames that need to be placed
root = Tk()
file_path = 'change this in function below' #will be reassigned in csv_opener()
df_original = pd.DataFrame() # this df will not be transformed at all, it will keep original data
df = pd.DataFrame() #will ve reassigned in csv_opener() / this df will be transformed
x, y = None,None
x_train, x_test, y_train, y_test = None,None,None,None
# I keep all frames here incase I want to delete them later
choose_csv_frame = LabelFrame(root, pady= 6, padx=15)
treevew_data_frame = LabelFrame(root, padx=40, text="Data Display")
treevew_has_NA_data_frame = LabelFrame(root, padx=40, text="Does Column have NA")
transoform_data_frame = LabelFrame(root, padx=20, text="Transform Data")
split_data_frame = LabelFrame(root, padx=20, text="Split Data")
EDA_frame = LabelFrame(root, padx=20, text="EDA")
def create_root(project_name):
#create root
root.title(project_name)
#title at top
title_label = Label(root, bg="gray", padx = 185, text = project_name)
title_label.place(anchor=CENTER, relx= 0.5, y=10)
#size
# width= root.winfo_screenwidth()
# height= root.winfo_screenheight()
# root.geometry("%dx%d" % (width, height))
root.geometry("1100x680+300+100")
# root.resizable(False,False)
def root_loop() -> int:
#start root loop
root.mainloop()
return 0
#get file path
def run_code():
choose_csv_frame.place_forget()
treeview_of_df()
EDA()
transform_data()
split_data()
def csv_opener():
#choose CSV File
csv_file_name = Label(choose_csv_frame ,text="No File Selected")
choose_csv_frame.filename = filedialog.askopenfilename(initialdir="/C", title="Select CSV File", filetypes=(("CSV Files", "*.csv"),))
csv_file_name["text"] = choose_csv_frame.filename
global file_path
file_path = csv_file_name["text"]
#make df
global df, df_original
df = pd.read_csv(file_path)
df_original = df
#delete choose_csv_frame / Starts Actual Apps
submission_successful = messagebox.showinfo("Submission Successful", "The data has been submitted successfully")
if submission_successful == "ok":
run_code()
def choose_csv():
#label frame to put things in
choose_csv_frame.place(anchor=CENTER, relx= 0.5, y=50)
choose_csv_button = Button(choose_csv_frame, text="Choose Select CSV File", command= csv_opener)
choose_csv_button.grid(row= 0, column=0)
def treeview_of_df():
#~~ treeview ~~#
# create tree view frame & treeview
tv = ttk.Treeview(treevew_data_frame,height=15)
y_scrollbar = ttk.Scrollbar(treevew_data_frame, orient="vertical", command= tv.yview)
x_scrollbar = ttk.Scrollbar(treevew_data_frame, orient="horizontal", command=tv.xview)
#put frame on grid
treevew_data_frame.pack(fill=None, expand=False)
#put scroll bars
tv.configure(yscrollcommand=y_scrollbar.set, xscrollcommand=x_scrollbar.set)
y_scrollbar.pack(side=RIGHT, fill="y")
x_scrollbar.pack(side=BOTTOM, fill="x")
#show columns and list
tv["column"] = list(df.columns)
tv["show"] = "headings"
for column in tv["column"]:
tv.heading(column, text = column)
tv.column(column, width=100, minwidth=100)
df_rows = df.to_numpy().tolist()
for row in df_rows:
tv.insert("", "end", values= row)
tv.pack()
#check is_na of all columns
#create dataframe with data
data = []
for column in df.columns:
column_has_na_list = [column, df[column].isna().any(), df[column].isna().sum(), f"{round(df[column].isna().sum() * 100 / len(df[column]), 4)} %"]
data.append(column_has_na_list)
df_has_NA = pd.DataFrame(data, columns=["Column Name", "Column Has NA", "Count of NA Cells", "% of NA Cells"])
# create tree view to show each column and if it has NA values
tv_has_NA = ttk.Treeview(treevew_has_NA_data_frame)
y_scrollbar_has_NA = ttk.Scrollbar(treevew_has_NA_data_frame, orient="vertical", command=tv_has_NA.yview)
x_scrollbar_has_NA = ttk.Scrollbar(treevew_has_NA_data_frame, orient="horizontal", command=tv_has_NA.xview)
#put frame on grid
treevew_has_NA_data_frame.pack(side=LEFT, anchor=NW)
#put scroll bars
tv_has_NA.configure(yscrollcommand=y_scrollbar_has_NA.set, xscrollcommand=x_scrollbar_has_NA.set)
y_scrollbar_has_NA.pack(side=RIGHT, fill="y")
x_scrollbar_has_NA.pack(side=BOTTOM, fill="x")
# #show columns and list
tv_has_NA["column"] = list(df_has_NA.columns)
tv_has_NA["show"] = "headings"
for column in tv_has_NA["column"]:
tv_has_NA.heading(column, text = column)
tv_has_NA.column(column, width=100, minwidth=100)
df_has_NA_rows = df_has_NA.to_numpy().tolist()
for row in df_has_NA_rows:
tv_has_NA.insert("", "end", values= row)
tv_has_NA.pack()
def detailed_EDA():
detailed_EDA_window = Toplevel(root, padx=30)
detailed_EDA_window.title("Detailed EDA")
detailed_EDA_window.geometry("470x500")
detailed_EDA_frame = LabelFrame(detailed_EDA_window) #will be used in display_detailed_EDA()
Label(detailed_EDA_window, bg="gray", padx = 105, text = "Detailed Column EDA").grid(row=0, column=0, columnspan=3)
choices = []
for i, column in enumerate(df.columns):
choices.append(column)
Label(detailed_EDA_window, text = "Choose Column to view more about").grid(row=1, column=0)
column_combobox_get = ttk.Combobox(detailed_EDA_window, values= choices)
column_combobox_get.grid(row=1, column=1)
Button(detailed_EDA_window, text= "Display More", command= lambda: display_detailed_EDA(column_combobox_get.get())).grid(row=1, column=2)
Label(detailed_EDA_window, text=" ").grid(row=2, column=0)
def display_detailed_EDA(column):
def nav_bar():
Label(detailed_EDA_frame, bg="lightgray", text= column, padx=105).grid(row=0, column=0, columnspan=5)
Button(detailed_EDA_frame, text="Statistics", command=lambda: statistics_detailed_EDA(column)).grid(row=1, column=0)
#show Histogram Button if column.dtype == float || int // show bar Button if column.dtype == Bool
if df[column].dtype == np.dtype('bool') or (len(df[column].unique()) == 2 and (1 in df[column].unique() and 0 in df[column].unique())):
Button(detailed_EDA_frame, text="Stacked Bar", command=lambda: stacked_bar_detailed_EDA(column)).grid(row=1, column=1)
elif (df[column].dtype == np.dtype('O') or df[column].dtype == np.dtype('float64') or df[column].dtype == np.dtype('int64') or df[column].dtype == np.dtype('float32') or df[column].dtype == np.dtype('int32') and (len(df[column].unique()) == 2 and (1 in df[column].unique() and 0 in df[column].unique()))):
Button(detailed_EDA_frame, text="Histogram", command=lambda: histogram_detailed_EDA(column, df[column].dtype)).grid(row=1, column=1)
#only show Common Value button if its a float or int
if (df[column].dtype == np.dtype('float64') or df[column].dtype == np.dtype('int64') or df[column].dtype == np.dtype('float32') or df[column].dtype == np.dtype('int32')) and not (len(df[column].unique()) == 2 and (1 in df[column].unique() and 0 in df[column].unique())):
Button(detailed_EDA_frame, text="Common Values", command=lambda: common_values_detailed_EDA(column)).grid(row=1, column=2)
Button(detailed_EDA_frame, text="Extreme Values", command=lambda: extreme_values_detailed_EDA(column)).grid(row=1, column=3)
def rebuild_everything_in_detailed_EDA_frame():
#deletes everything in frame
for widgets in detailed_EDA_frame.winfo_children():
widgets.destroy()
detailed_EDA_frame.grid(row=3, column=0, columnspan=3)
#put navbar again
nav_bar()
rebuild_everything_in_detailed_EDA_frame()
def prepare_categorical_data_for_analysis():
all_sentences = ' '.join(df[column])
cleaned_text = re.sub(r'[^\w\s]', '', all_sentences)
words = cleaned_text.lower().split()
word_counts = Counter(words)
top_10_words_with_count = word_counts.most_common(10)
top_10_words = []
#get top 10 words
for word in top_10_words_with_count:
for i in range(word[1]):
top_10_words.append(word[0])
return top_10_words, word_counts
def statistics_detailed_EDA(column):
#deletes everything in frame
rebuild_everything_in_detailed_EDA_frame()
#change geometry
detailed_EDA_window.geometry("470x500")
Label(detailed_EDA_frame, text = " ").grid(row=2, column=0)
#if column.dtype == int or bool
if (df[column].dtype == np.dtype('float64') or df[column].dtype == np.dtype('int64') or df[column].dtype == np.dtype('float32') or df[column].dtype == np.dtype('int32')) and not (len(df[column].unique()) == 2 and (1 in df[column].unique() and 0 in df[column].unique())):
#min
Label(detailed_EDA_frame, text="Minimum: ").grid(row=3, column=0, columnspan=2)
Label(detailed_EDA_frame, text= min(df[column])).grid(row=3, column=1, columnspan=2)
#Q1
Q1 = df[column].quantile(0.25)
Label(detailed_EDA_frame, text="Q1: ").grid(row=4, column=0, columnspan=2)
Label(detailed_EDA_frame, text= Q1).grid(row=4, column=1, columnspan=2)
#median value
Label(detailed_EDA_frame, text="Median: ").grid(row=5, column=0, columnspan=2)
Label(detailed_EDA_frame, text= round(df[column].median(), 3)).grid(row=5, column=1, columnspan=2)
#Q3
Q3 = df[column].quantile(0.75)
Label(detailed_EDA_frame, text="Q3: ").grid(row=6, column=0, columnspan=2)
Label(detailed_EDA_frame, text= Q1).grid(row=6, column=1, columnspan=2)
#max
Label(detailed_EDA_frame, text="Maximum: ").grid(row=8, column=0, columnspan=2)
Label(detailed_EDA_frame, text= max(df[column])).grid(row=8, column=1, columnspan=2)
#IQR
column_iqr = Q3 - Q1
Label(detailed_EDA_frame, text="IQR: ").grid(row=7, column=0, columnspan=2)
Label(detailed_EDA_frame, text= column_iqr).grid(row=7, column=1, columnspan=2)
#mean
Label(detailed_EDA_frame, text="Mean: ").grid(row=9, column=0, columnspan=2)
Label(detailed_EDA_frame, text= round(df[column].mean(), 3)).grid(row=9, column=1, columnspan=2)
#distance value
Label(detailed_EDA_frame, text="Distinct: ").grid(row=3, column=2, columnspan=2)
Label(detailed_EDA_frame, text= len(pd.unique(df[column]))).grid(row=3, column=3, columnspan=2)
#distance percentage
Label(detailed_EDA_frame, text="Distinct (%): ").grid(row=4, column=2, columnspan=2)
distinct_percentage = round((len(pd.unique(df[column])) / len(df[column])) * 100, 1)
Label(detailed_EDA_frame, text= f"{distinct_percentage}%").grid(row=4, column=3, columnspan=2)
#missing value
Label(detailed_EDA_frame, text="Missing: ").grid(row=5, column=2, columnspan=2)
Label(detailed_EDA_frame, text= df[column].isna().sum()).grid(row=5, column=3, columnspan=2)
#missing percentage
Label(detailed_EDA_frame, text="Missing(%): ").grid(row=6, column=2, columnspan=2)
missing_percentage = round((df[column].isna().sum() / len(df[column])) * 100, 1)
Label(detailed_EDA_frame, text= f"{missing_percentage}%").grid(row=6, column=3, columnspan=2)
#standard diviation
Label(detailed_EDA_frame, text="STD: ").grid(row=7, column=2, columnspan=2)
Label(detailed_EDA_frame, text= round(df[column].std(), 3)).grid(row=7, column=3, columnspan=2)
#Range
range = df[column].max() - df[column].min()
Label(detailed_EDA_frame, text="Range: ").grid(row=8, column=2, columnspan=2)
Label(detailed_EDA_frame, text= range).grid(row=8, column=3, columnspan=2)
#sum
Label(detailed_EDA_frame, text="Sum: ").grid(row=9, column=2, columnspan=2)
Label(detailed_EDA_frame, text= round(df[column].sum(), 3)).grid(row=9, column=3, columnspan=2)
#if column.dtype == category
elif df[column].dtype == np.dtype('O'):
top_10_words,word_counts = prepare_categorical_data_for_analysis()
#Least Used word
Label(detailed_EDA_frame, text="Least Used Word: ").grid(row=3, column=0, columnspan=2)
Label(detailed_EDA_frame, text= f"{word_counts.most_common()[-1][0]}").grid(row=3, column=1, columnspan=2)
Label(detailed_EDA_frame, text=f"Count: {word_counts.most_common()[-1][1]}").grid(row=3, column=2, columnspan=2)
#Most Used word
Label(detailed_EDA_frame, text="Most Used Word: ").grid(row=4, column=0, columnspan=2)
Label(detailed_EDA_frame, text= f"{word_counts.most_common()[0][0]}").grid(row=4, column=1, columnspan=2)
Label(detailed_EDA_frame, text=f"Count: {word_counts.most_common()[0][1]}").grid(row=4, column=2, columnspan=2)
#if bool
else:
#deletes everything in frame
rebuild_everything_in_detailed_EDA_frame()
#change geometry
detailed_EDA_window.geometry("470x500")
Label(detailed_EDA_frame, text = " ").grid(row=2, column=0)
#missing value
Label(detailed_EDA_frame, text="Missing: ").grid(row=3, column=0, columnspan=2)
Label(detailed_EDA_frame, text= df[column].isna().sum()).grid(row=3, column=1, columnspan=5)
#missing percentage
Label(detailed_EDA_frame, text="Missing(%): ").grid(row=4, column=0, columnspan=2)
missing_percentage = round((df[column].isna().sum() / len(df[column])) * 100, 1)
Label(detailed_EDA_frame, text= f"{missing_percentage}%").grid(row=4, column=1, columnspan=5)
def histogram_detailed_EDA(column, column_dtype):
#deletes everything in frame
rebuild_everything_in_detailed_EDA_frame()
#change geometry
detailed_EDA_window.geometry("700x705")
#change bin size
bin_size_get = StringVar()
Label(detailed_EDA_frame, text = " ").grid(row=2, column=0)
Label(detailed_EDA_frame, text="Enter New Bin Size").grid(row=3, column=0)
Entry(detailed_EDA_frame, textvariable= bin_size_get).grid(row=3, column=1)
Button(detailed_EDA_frame, text="Confirm", command=lambda: change_bin_size()).grid(row=3, column=2)
Button(detailed_EDA_frame, text="Or use freedman diaconis", command=lambda: graph_using_freedman_diaconis()).grid(row=3, column=3)
def graph_using_freedman_diaconis():
global bin_size
bin_size = True
fig, ax = plt.subplots()
canvas = FigureCanvasTkAgg(fig, master=detailed_EDA_frame)
canvas.get_tk_widget().grid(row= 4, column= 0, columnspan= 5)
# Plot data on Matplotlib Figure
try:
#if dtype == Object
if column_dtype == np.dtype('O'):
top_10_words,word_counts = prepare_categorical_data_for_analysis()
top_10_words_numpy_array = np.array(top_10_words)
bin_length = len(np.unique(top_10_words_numpy_array))
ax.hist(top_10_words, bins=bin_length)
else:
#calculate bins using
Q75, Q25 = np.percentile(df[column], [75 ,25])
IQR = Q75 - Q25
bin_width = 2 * IQR * np.power(len(df[column]), -1/3)
num_bins = int((max(df[column]) - min(df[column])) / bin_width)
ax.hist(df[column], bins=num_bins, rwidth=0.7)
# Add labels and title
plt.xlabel(column)
plt.ylabel('Frequency')
plt.title(f'{column} Histogram')
canvas.draw()
except ValueError:
messagebox.showerror('Could not complete freedman diaconis calculation', 'Calculating freedman diaconis was unsuccessful, \nIt seems that there are some NaN values, please remove them before graphing. \nOR \nInput bin size mangually.')
bin_size = True #used to detect if custom bin sized is used (so if graph_externally() is run we graph the new bin graph)
def change_bin_size():
try:
global bin_size
bin_size = int(bin_size_get.get())
fig, ax = plt.subplots()
canvas = FigureCanvasTkAgg(fig, master=detailed_EDA_frame)
canvas.get_tk_widget().grid(row= 4, column= 0, columnspan= 5)
#if dtype != Object
if column_dtype == np.dtype('O'):
top_10_words,word_counts = prepare_categorical_data_for_analysis()
ax.hist(top_10_words, bins=bin_size, rwidth=0.7)
else:
ax.hist(df[column], bins=bin_size, rwidth=0.7)
# Add labels and title
plt.xlabel(column)
plt.ylabel('Frequency')
plt.title(f'{column} Histogram')
canvas.draw()
except ValueError:
messagebox.showerror('Input Valid Number', 'Please Input an integer & make sure it is not a float')
graph_using_freedman_diaconis()
#extra features to edit graph
def graph_externally(column):
#if bin_size isnt custom made
global bin_size
if bin_size != True:
fig, ax = plt.subplots()
#if dtype != Object
if column_dtype == np.dtype('O'):
top_10_words,word_counts = prepare_categorical_data_for_analysis()
ax.hist(top_10_words, bins=bin_size, rwidth=0.7)
else:
ax.hist(df[column], bins=bin_size, rwidth=0.7)
fig.show()
else:
fig, ax = plt.subplots()
#if dtype != Object
if column_dtype == np.dtype('O'):
top_10_words,word_counts = prepare_categorical_data_for_analysis()
top_10_words_numpy_array = np.array(top_10_words)
bin_length = len(np.unique(top_10_words_numpy_array))
ax.hist(top_10_words, bins=bin_length)
else:
#calculate bins using
Q75, Q25 = np.percentile(df[column], [75 ,25])
IQR = Q75 - Q25
bin_width = 2 * IQR * np.power(len(df[column]), -1/3)
num_bins = int((max(df[column]) - min(df[column])) / bin_width)
ax.hist(df[column], bins=num_bins, rwidth=0.7)
fig.show()
def display_count_graph(column):
#rebuild Canvas
fig, ax = plt.subplots()
canvas = FigureCanvasTkAgg(fig, master=detailed_EDA_frame)
canvas.get_tk_widget().grid(row= 4, column= 0, columnspan= 5)
#if dtype != Object
if column_dtype == np.dtype('O'):
#prepares words for analysis
top_10_words,word_counts = prepare_categorical_data_for_analysis()
top_10_words_numpy_array = np.array(top_10_words)
bin_length = len(np.unique(top_10_words_numpy_array))
ax.hist(top_10_words, bins=bin_length)
counts, edges, bars = ax.hist(top_10_words, bins=bin_length)
ax.bar_label(bars)
else:
#if bin_size isnt custom made
global bin_size
if bin_size != True:
ax.hist(df[column], bins=bin_size, rwidth=0.7)
counts, edges, bars = ax.hist(df[column], bins=bin_size, rwidth=0.7)
ax.bar_label(bars)
else:
#calculate bins using
Q75, Q25 = np.percentile(df[column], [75 ,25])
IQR = Q75 - Q25
bin_width = 2 * IQR * np.power(len(df[column]), -1/3)
num_bins = int((max(df[column]) - min(df[column])) / bin_width)
ax.hist(df[column], bins=num_bins, rwidth=0.7)
counts, edges, bars = ax.hist(df[column], bins=num_bins, rwidth=0.7)
ax.bar_label(bars)
# Add labels and title
plt.xlabel(column)
plt.ylabel('Frequency')
plt.title(f'{column} Histogram')
canvas.draw()
def not_display_count_graph(column):
# Initialize Tkinter and Matplotlib Figure
fig, ax = plt.subplots()
canvas = FigureCanvasTkAgg(fig, master=detailed_EDA_frame)
# canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
canvas.get_tk_widget().grid(row= 4, column= 0, columnspan= 5)
# Plot data on Matplotlib Figure
#if dtype != Object
if column_dtype == np.dtype('O'):
top_10_words,word_counts = prepare_categorical_data_for_analysis()
top_10_words_numpy_array = np.array(top_10_words)
bin_length = len(np.unique(top_10_words_numpy_array))
ax.hist(top_10_words, bins=bin_length)
else:
global bin_size
if bin_size != True:
ax.hist(df[column], bins=bin_size, rwidth=0.7)
else:
#calculate bins using
Q75, Q25 = np.percentile(df[column], [75 ,25])
IQR = Q75 - Q25
bin_width = 2 * IQR * np.power(len(df[column]), -1/3)
num_bins = int((max(df[column]) - min(df[column])) / bin_width)
ax.hist(df[column], bins=num_bins, rwidth=0.7)
# Add labels and title
plt.xlabel(column)
plt.ylabel('Frequency')
plt.title(f'{column} Histogram')
canvas.draw()
Label(detailed_EDA_frame, text = " ").grid(row=5, column=0)
Button(detailed_EDA_frame, text="Open Graph Externally", command=lambda: graph_externally(column)).grid(row=6, column=0)
Button(detailed_EDA_frame, text="Display Count Over Bar", command=lambda: display_count_graph(column)).grid(row=6, column=1)
Button(detailed_EDA_frame, text="Dont Display Count Over Bar", command=lambda: not_display_count_graph(column)).grid(row=6, column=2)
def stacked_bar_detailed_EDA(column):
#deletes everything in frame
rebuild_everything_in_detailed_EDA_frame()
#change geometry
detailed_EDA_window.geometry("700x705")
fig, ax = plt.subplots()
canvas = FigureCanvasTkAgg(fig, master=detailed_EDA_frame)
canvas.get_tk_widget().grid(row= 4, column= 0, columnspan= 5)
groups = ['%']
values1 = [len(df[df[column] == 0]) or len(df[df.Boolea == False])]
values2 = [len(df[df[column] == 1]) or len(df[df.Boolea == True])]
total = values1[0] + values2[0]
# Stacked bar chart
ax.bar(groups, values1, label = "0 or False")
ax.bar(groups, values2, bottom = values1, label = "1 or True")
for bar in ax.patches:
ax.text(bar.get_x() + bar.get_width() / 2,
bar.get_height() / 2 + bar.get_y(),
f"{round(bar.get_height())}({round((round(bar.get_height()) / total) * 100, 3)} %)", ha = 'center',
color = 'w', weight = 'bold', size = 10)
ax.legend()
canvas.draw()
#extra features to edit graph
def graph_externally(column):
fig, ax = plt.subplots()
groups = ['%']
values1 = [len(df[df[column] == 0]) or len(df[df.Boolea == False])]
values2 = [len(df[df[column] == 1]) or len(df[df.Boolea == True])]
total = values1[0] + values2[0]
# Stacked bar chart
ax.bar(groups, values1, label = "0 or False")
ax.bar(groups, values2, bottom = values1, label = "1 or True")
for bar in ax.patches:
ax.text(bar.get_x() + bar.get_width() / 2,
bar.get_height() / 2 + bar.get_y(),
f"{round(bar.get_height())}({round((round(bar.get_height()) / total) * 100, 3)} %)", ha = 'center',
color = 'w', weight = 'bold', size = 10)
ax.legend()
fig.show()
Label(detailed_EDA_frame, text = " ").grid(row=5, column=0)
Button(detailed_EDA_frame, text="Open Graph Externally", command=lambda: graph_externally(column)).grid(row=6, column=0, columnspan=5)
def common_values_detailed_EDA(column):
#deletes everything in frame
rebuild_everything_in_detailed_EDA_frame()
#change geometry
detailed_EDA_window.geometry("700x705")
#calculate top 10
column_name = df[column].value_counts().nlargest(10).index
column_count = df[column].value_counts().nlargest(10).values
#rebuild Canvas
def display_graph():
for i in range(len(column_name)):
fig, ax = plt.subplots(figsize=(6, 0.4))
canvas = FigureCanvasTkAgg(fig, master=detailed_EDA_frame)
canvas.get_tk_widget().grid(row= 4+i, column= 0, columnspan= 5)
ax.barh(column_name[i], column_count[i])
plt.yticks([column_name[i]])
plt.xticks([min(column_count), max(column_count)])
for i in ax.patches:
plt.text(i.get_width()+0.2, i.get_y()+0.5, str(round((i.get_width()), 2)), fontsize = 10, fontweight ='bold', color ='grey')
display_graph()
def extreme_values_detailed_EDA(column):
#deletes everything in frame
rebuild_everything_in_detailed_EDA_frame()
#change geometry
detailed_EDA_window.geometry("700x705")
#change new threshold
threshold_get = StringVar()
Label(detailed_EDA_frame, text = " ").grid(row=2, column=0)
#calculations
def IQR_calculations(threshold = 1.5):
Q1 = df[column].quantile(0.25)
Q3 = df[column].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - threshold * IQR
upper_bound = Q3 + threshold * IQR
outliers = df[column][(df[column] < lower_bound) | (df[column] > upper_bound)]
outliers_name = outliers.value_counts().index
outliers_count = outliers.value_counts().values
return outliers_name, outliers_count
outliers_name, outliers_count = IQR_calculations() #default threshold
def display_graph():
for i in range(len(outliers_name)):
fig, ax = plt.subplots(figsize=(6, 0.4))
canvas = FigureCanvasTkAgg(fig, master=detailed_EDA_frame)
canvas.get_tk_widget().grid(row= 4+i, column= 0, columnspan= 5)
ax.barh(outliers_name[i], outliers_count[i])
plt.yticks([outliers_name[i]])
plt.xticks([min(outliers_count), max(outliers_count)])
for i in ax.patches:
plt.text(i.get_width()+0.2, i.get_y()+0.5, str(round((i.get_width()), 2)), fontsize = 10, fontweight ='bold', color ='grey')
display_graph()
#build statistics_detailed_EDA() automatically
statistics_detailed_EDA(column)
def interaction_graph():
interaction_window = Toplevel(root, padx=30)
interaction_window.title("Interaction")
interaction_window.geometry("700x705")
interaction_frame = LabelFrame(interaction_window) #will be used in display_interaction()
Label(interaction_window, bg="gray", padx = 105, text = "Interaction Between Column").grid(row=0, column=0, columnspan=5)
choices = []
for i, column in enumerate(df.columns):
if (df[column].dtype == np.dtype('O')) or (len(df[column].unique()) == 2 and (1 in df[column].unique() and 0 in df[column].unique())) or (df[column].dtype == np.dtype('bool')):
continue
else:
choices.append(column)
Label(interaction_window, text = "Choose x and y columns to view interaction").grid(row=1, column=0, columnspan=5)
Label(interaction_window, text = "x").grid(row=2, column=0)
x_column_combobox_get = ttk.Combobox(interaction_window, values= choices)
x_column_combobox_get.grid(row=3, column=0)
Label(interaction_window, text = "y").grid(row=2, column=2)
y_column_combobox_get = ttk.Combobox(interaction_window, values= choices)
y_column_combobox_get.grid(row=3, column=2)
Button(interaction_window, text= "Display Chosen Columns", command= lambda: display_interaction(x_column_combobox_get.get(), y_column_combobox_get.get())).grid(row=4, column=0, columnspan=5)
empty_space_label = Label(interaction_window, text = " ")
empty_space_label.grid(row=5, column=0)
def display_interaction(x_column, y_column):
def rebuild_everything_in_display_interaction_frame():
#deletes everything in frame
for widgets in interaction_frame.winfo_children():
widgets.destroy()
interaction_frame.grid(row=6, column=0, columnspan=3)
rebuild_everything_in_display_interaction_frame()
fig, ax = plt.subplots()
canvas = FigureCanvasTkAgg(fig, master=interaction_frame)
canvas.get_tk_widget().grid(row= 0, column= 0, columnspan= 5)
ax.scatter(df[x_column], df[y_column])
# Add labels and title
plt.xlabel(x_column)
plt.ylabel(y_column)
plt.title(f'{x_column} - {y_column} Scatter Plot Interaction')
canvas.draw()
def correlation():
correlation_window = Toplevel(root, padx=30)
correlation_window.title("Correlation")
correlation_window.geometry("690x605")
correlation_frame = LabelFrame(correlation_window) #will be used in display_correlation()
Label(correlation_window, bg="gray", padx = 105, text = "Correlation Between Columns").grid(row=0, column=0, columnspan=5)
def display_correlation():
def rebuild_everything_in_correlation_frame():
#deletes everything in frame
for widgets in correlation_frame.winfo_children():
widgets.destroy()
correlation_frame.grid(row=1, column=0)
rebuild_everything_in_correlation_frame()
fig, ax = plt.subplots()
canvas = FigureCanvasTkAgg(fig, master=correlation_frame)
canvas.get_tk_widget().grid(row= 0, column= 0)
sns.heatmap(df.corr(), cmap='YlGnBu')
canvas.draw()
display_correlation()
def EDA():
EDA_frame.pack(side=TOP, anchor=NW)
empty_space_label = Label(EDA_frame, text = " ")
#check if column is categorical or numaric or bool
column_is_categorical = []
column_is_numaric = []
column_is_bool = []
for column in df.columns:
#check if bool this checks if the unique() outcome is actually 0 & 1 or if it this column just happens to contain only 2 unique rows that could be not a bool
if df[column].dtype == np.dtype('bool') or (len(df[column].unique()) == 2 and (1 in df[column].unique() and 0 in df[column].unique()) ):
column_is_bool.append(column)
#check if object
elif df[column].dtype == np.dtype('O'):
column_is_categorical.append(column)
#check if float or int
elif df[column].dtype == np.dtype('float64') or df[column].dtype == np.dtype('int64') or df[column].dtype == np.dtype('float32') or df[column].dtype == np.dtype('int32'):
column_is_numaric.append(column)
Label(EDA_frame, text = f"Number of Features: {len(df.columns)}").grid(row=0, column=0)
Label(EDA_frame, text = f"Number of Rows: {len(df)}").grid(row=1, column=0)
Label(EDA_frame, text = f"Number of Missing Cells: {df.isnull().sum().sum()}").grid(row=2, column=0)
empty_space_label.grid(row=0, column=1)
Label(EDA_frame, text = f"Number of Numaric Columns: {len(column_is_numaric)}").grid(row=0, column=1)
Label(EDA_frame, text = f"Number of Boolean Columns: {len(column_is_bool)}").grid(row=1, column=1)
Label(EDA_frame, text = f"Number of Categorical Columns: {len(column_is_categorical)}").grid(row=2, column=1)
Button(EDA_frame, text="More Detailed EDA", command=detailed_EDA).grid(row=0, column=2)
Button(EDA_frame, text="Interactions", command=interaction_graph).grid(row=1, column=2)
Button(EDA_frame, text="Correlations", command=correlation).grid(row=2, column=2)
def close_whatever_transform_window(transform_window):
transform_window.destroy()
#delete old treeview and repack it
for widgets in treevew_data_frame.winfo_children():
widgets.destroy()
#deletes old NA treeview
for widgets in treevew_has_NA_data_frame.winfo_children():
widgets.destroy()
treeview_of_df()
#deltes and repacks EDA EDA_frame
for widgets in EDA_frame.winfo_children():
widgets.destroy()
EDA()
def handle_NA():
#check if any columns have NA
if df.isnull().any().any() == False:
messagebox.showinfo("No NA values found", "All columns don't contain any NA")
#open window to handle NA values
elif df.isnull().any().any() == True:
handle_NA_window = Toplevel(root, padx=30)
handle_NA_window.title("Handle NA")
handle_NA_window.geometry("420x500")
# #make window behind this one unclickable
# handle_NA_window.grab_set()
# handle_NA_window.transient(root)
#title at top
title_label = Label(handle_NA_window, bg="gray", padx = 105, text = "Columns Containing NA")
title_label.grid(row=0, column=0, columnspan=3)
choices = []
for column in df.columns:
if df[column].isna().any() == False:
continue
else:
choices.append(column)
Label(handle_NA_window, bg="lightgray", text ="Choose Column Name to Handle").grid(row=1, column=0)
column_to_handle_na = ttk.Combobox(handle_NA_window, values= choices)
column_to_handle_na.grid(row=1, column=1)
Label(handle_NA_window, bg="lightgray", text ="Choose How to Handle Column").grid(row=2, column=0, columnspan=2)
def replace_NA_with_mean():
column_to_handle = column_to_handle_na.get()
global df
mean = df[column_to_handle].mean()
df[column_to_handle].fillna(value=mean, inplace=True)
removal_successful = messagebox.showinfo("Filling Successful", "NA cells have been filled with mean successfully in the column")
if removal_successful == "ok":
close_whatever_transform_window(handle_NA_window)
def replace_NA_with_median():
column_to_handle = column_to_handle_na.get()
global df
median = df[column_to_handle].median()
df[column_to_handle].fillna(value=median, inplace=True)
removal_successful = messagebox.showinfo("Filling Successful", "NA cells have been filled with median successfully in the column")
if removal_successful == "ok":
close_whatever_transform_window(handle_NA_window)
def remove_NA_row():
column_to_handle = column_to_handle_na.get()
global df
df.dropna(subset = [column_to_handle], inplace=True)
removal_successful = messagebox.showinfo("Removal Successful", "The rows has been removed successfully")
if removal_successful == "ok":
close_whatever_transform_window(handle_NA_window)
def replace_all_NA_with_imputer(strategy):
global df
imputer = SimpleImputer(strategy=strategy)
df_array_imputed = imputer.fit_transform(df)
df = pd.DataFrame(df_array_imputed, columns=df.columns)
removal_successful = messagebox.showinfo("Filling Successful", f"NA cells have been filled with {strategy} successfully in all DataFrame")
if removal_successful == "ok":
close_whatever_transform_window(handle_NA_window)
def remove_all_NA_row():
global df
for column in df.columns:
df.dropna(subset = [column], inplace=True)
removal_successful = messagebox.showinfo("Removal Successful", "The rows has been removed successfully in all DataFrame")
if removal_successful == "ok":
close_whatever_transform_window(handle_NA_window)
Button(handle_NA_window, text= "Replace with Mean",command= lambda: replace_NA_with_mean()).grid(row=3, column=0)
Button(handle_NA_window, text= "Replace with Median",command= lambda: replace_NA_with_median()).grid(row=3, column=1)
Button(handle_NA_window, text= "Remove Entire Row",command= lambda: remove_NA_row()).grid(row=4, column=0, columnspan=2)
Label(handle_NA_window, text= "or").grid(row=5, column=0, columnspan=2)
Button(handle_NA_window, text= "Replace all NA with Mean",command= lambda: replace_all_NA_with_imputer("mean")).grid(row=6, column=0, columnspan=2)
Button(handle_NA_window, text= "Replace all NA with Median",command= lambda: replace_all_NA_with_imputer("median")).grid(row=7, column=0, columnspan=2)
Button(handle_NA_window, text= "Remove all NA rows Entirely",command= lambda: remove_all_NA_row()).grid(row=8, column=0, columnspan=2)
#Closing & Saving button
Label(handle_NA_window, text= " ").grid(row=1009, column=0)
Button(handle_NA_window, text= "Close & Save", command=lambda: close_whatever_transform_window(handle_NA_window)).grid(row=1010, column=0, columnspan=2)
def encode_columns():
def actually_encode_column():
column_to_encode = column_combobox_get.get()
label_encoder = LabelEncoder()
global df
df[column_to_encode] = label_encoder.fit_transform(df[column_to_encode])
messagebox.showinfo("Encoding Successful", "The data has been encodded successfully")
#making window
encode_columns_window = Toplevel(root, padx=30)
encode_columns_window.title("Encoding Columns")
encode_columns_window.geometry("440x500")
# #make window behind this one unclickable
# encode_columns_window.grab_set()
# encode_columns_window.transient(root)
choices = []
for i, column in enumerate(df.columns):
choices.append(column)
column_has_NA = df[column].isna().any()
Label(encode_columns_window, text = column).grid(row=i+2, column=0)
Label(encode_columns_window, text = column_has_NA).grid(row=i+2, column=2)
Label(encode_columns_window, text= "Enter Column Name to Encode: ").grid(row=0, column=0)
column_combobox_get = ttk.Combobox(encode_columns_window, values= choices)
column_combobox_get.grid(row=0, column=1)
Button(encode_columns_window, text= "encode", command= lambda: actually_encode_column()).grid(row=0, column=2)
#each column
Label(encode_columns_window,bg="lightgray", text ="Columns").grid(row=1, column=0)
Label(encode_columns_window,bg="lightgray", text ="Column has NA").grid(row=1, column=2)
#Closing & Saving button
Button(encode_columns_window, text= "Close & Save", command=lambda: close_whatever_transform_window(encode_columns_window)).grid(row=1010, column=1, columnspan=1)
def remove_column():
def actually_remove_column():
column_to_remove = column_combobox_get.get()
global df
df = df.drop(column_to_remove, axis=1)
messagebox.showinfo("Removal Successful", "The column has been removed successfully")
def actually_remove_all_non_int_columns():
global df
for column in df.columns:
if df[column].dtype == 'int64' or df[column].dtype == 'float64':
continue
else:
df = df.drop(column, axis=1)
removal_successful = messagebox.showinfo("Removal Successful", "All non float/int columns have been removed successfully")
if removal_successful == "ok":
close_whatever_transform_window(remove_column_window)
remove_column_window = Toplevel(root, padx=30)
remove_column_window.title("Remove Column")
remove_column_window.geometry("460x500")
# #make window behind this one unclickable
# remove_column_window.grab_set()
# remove_column_window.transient(root)
choices = []
for i, column in enumerate(df.columns):
choices.append(column)
column_dtype = df[column].dtype
Label(remove_column_window, text = column).grid(row=i+2, column=0)
Label(remove_column_window, text = column_dtype).grid(row=i+2, column=1)
Label(remove_column_window, text= "Enter Column Name to Remove: ").grid(row=0, column=0)
column_combobox_get = ttk.Combobox(remove_column_window, values= choices)
column_combobox_get.grid(row=0, column=1)
Button(remove_column_window, text= "Remove Column", command= actually_remove_column).grid(row=0, column=2)
#each column
Label(remove_column_window,bg="lightgray", text ="Columns").grid(row=1, column=0)
Label(remove_column_window,bg="lightgray", text ="Column dtype").grid(row=1, column=1)
Label(remove_column_window,bg="lightgray", text ="or").grid(row=1, column=2)
Button(remove_column_window, text= "Remove all non \n float/int columns", command= actually_remove_all_non_int_columns).grid(row=2, column=2)
#Closing & Saving button
Button(remove_column_window, text= "Close & Save", command=lambda: close_whatever_transform_window(remove_column_window)).grid(row=1010, column=1)
def rename_column():
new_column_name_get = StringVar()
#making window
rename_column_window = Toplevel(root, padx=30)
rename_column_window.title("Rename Column")
rename_column_window.geometry("430x500")
def actually_rename_column():
new_column_name = new_column_name_get.get()
column_to_rename = column_combobox_get.get()
global df
df.rename(columns = {f'{column_to_rename}':f'{new_column_name}'}, inplace = True)
removal_successful = messagebox.showinfo("Removed Duplicates Successful", "The duplicated data has been removed successfully")
if removal_successful == "ok":
# close_whatever_transform_window(remove_duplicates_window)
rename_column_window.destroy()
rename_column()
def save_and_close():
close_whatever_transform_window(rename_column_window)
choices = []
for i, column in enumerate(df.columns):
choices.append(column)
Label(rename_column_window, text= "Choose Column to Rename: ").grid(row=0, column=0)
Label(rename_column_window, text= "Rename Column to: ").grid(row=1, column=0)
column_combobox_get = ttk.Combobox(rename_column_window, values= choices)
column_combobox_get.grid(row=0, column=1)
Entry(rename_column_window, textvariable= new_column_name_get).grid(row=1, column=1)
Button(rename_column_window, text= "Rename Column", command= actually_rename_column).grid(row=2, column=0, columnspan=2)
Button(rename_column_window, text= "Save & Close", command= save_and_close).grid(row=3, column=0, columnspan=2)
def rename_column():
new_column_name_get = StringVar()
#making window
rename_column_window = Toplevel(root, padx=30)
rename_column_window.title("Rename Column")
rename_column_window.geometry("430x500")
def actually_rename_column():
new_column_name = new_column_name_get.get()
column_to_rename = column_combobox_get.get()
global df
df.rename(columns = {f'{column_to_rename}':f'{new_column_name}'}, inplace = True)
removal_successful = messagebox.showinfo("Removed Duplicates Successful", "The duplicated data has been removed successfully")
if removal_successful == "ok":
# close_whatever_transform_window(remove_duplicates_window)
rename_column_window.destroy()
rename_column()
def save_and_close():
close_whatever_transform_window(rename_column_window)
choices = []
for i, column in enumerate(df.columns):
choices.append(column)
Label(rename_column_window, text= "Choose Column to Rename: ").grid(row=0, column=0)
Label(rename_column_window, text= "Rename Column to: ").grid(row=1, column=0)
column_combobox_get = ttk.Combobox(rename_column_window, values= choices)
column_combobox_get.grid(row=0, column=1)
Entry(rename_column_window, textvariable= new_column_name_get).grid(row=1, column=1)
Button(rename_column_window, text= "Rename Column", command= actually_rename_column).grid(row=2, column=0, columnspan=2)
Button(rename_column_window, text= "Save & Close", command= save_and_close).grid(row=3, column=0, columnspan=2)
def remove_duplicates():
#making window
remove_duplicates_window = Toplevel(root, padx=30)
remove_duplicates_window.title("Remove Duplicates")
remove_duplicates_window.geometry("450x500")
def actually_remove_duplicates():
column_entry = column_combobox_get.get()
global df
df = df.drop_duplicates(subset=[column_entry])
removal_successful = messagebox.showinfo("Removed Duplicates Successful", "The duplicated data has been removed successfully")
if removal_successful == "ok":
close_whatever_transform_window(remove_duplicates_window)
choices = []
for i, column in enumerate(df.columns):
choices.append(column)
Label(remove_duplicates_window, text = column).grid(row=i+2, column=0, columnspan=2)
Label(remove_duplicates_window, text= "Enter Column to Remove Duplicates From: ").grid(row=0, column=0)
column_combobox_get = ttk.Combobox(remove_duplicates_window, values= choices)
column_combobox_get.grid(row=0, column=1)
Label(remove_duplicates_window, bg="lightgray", text= "Columns: ").grid(row=1, column=0, columnspan=2)
Button(remove_duplicates_window, text= "Remove", command= lambda: actually_remove_duplicates()).grid(row=1, column=1)
Label(remove_duplicates_window, text= "").grid(row=2, column=1)
Button(remove_duplicates_window, text= "Create Index Column", command= lambda: create_index_column(remove_duplicates_window)).grid(row=3, column=1)
Button(remove_duplicates_window, text= "Rename Column", command= lambda: actually_remove_duplicates()).grid(row=1, column=1)
Button(remove_duplicates_window, text= "Rename Column", command= lambda: actually_remove_duplicates()).grid(row=1, column=1)
def create_index_column(remove_duplicates_window):
#create index column
global df
if "index" in df.columns:
messagebox.showerror("Index already exists", "A column with the name 'index' already exists, please rename that column and try again")
else: