-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_tab.py
More file actions
1567 lines (1328 loc) · 59 KB
/
test_tab.py
File metadata and controls
1567 lines (1328 loc) · 59 KB
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 tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
from tkinter import Menu
from tkinter import Text
from tkinter import filedialog as fd
from tkinter import messagebox as msg
import os
import math
import copy
from tkinter.messagebox import askquestion, showinfo, showwarning, showerror
from tkinter.simpledialog import askstring, askinteger
import matplotlib
from arima.moving_average import MovingAverage
from arima.runner import init_ar, check_seasonality, do_diff, predict_using_arima
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from tabulate import tabulate # for table
import pandas as pd
import numpy as np
from scipy import stats
import seaborn as sns
import stats_team3 as st
#import Partial_Correlation_Coeff_XYZ as pcc_xyz
import multilinear_regression as mlr
import data_load as load
import file_upload as upload
from ac_classes import IndivModel as imodel
from ac_classes import BiDataModel as bdmodel
from ac_classes import MultiDataModel as mdmodel
from nonlinear_regression import NonLinearRegression as nlr
from anova import main as anova_main
from anova_CI import AnovaConfidenceInterval
from anova_class import Anova as Anova_class
# Create instance
win = tk.Tk()
# Add a title
win.title("AnalyticsCal")
global csvList,x, y,X,Y,data,multi_data,Y_predicted,is_simple_linear_equations
global csvHeader
global file_name
is_simple_linear_equations=False
#-------------------------------------------------------------------------Plots
def reg_plot(x_plot,y_plot,y_predicted, equation_str, title, x_label, y_label, color = None):
plt.clf()
raw_plot = plt.scatter(x_plot, y_plot, color = 'b')
predict_plot, = plt.plot(x_plot,y_predicted , '-',color = color)
plt.title(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.legend((raw_plot, predict_plot),('Observed Data', 'Prediction equation = ' + equation_str),loc=(-0.05,-0.20), scatterpoints=1, ncol=3, fontsize=8)
plt.tight_layout()
plt.show()
#--------------------------------------------------------------------------
csvList = []
#--------------------------------------------------------------------------
# Menu
# Open file
def open_file():
global csvList
global file_name
global csvHeader
global multi_df
file = fd.askopenfile(mode='r', filetypes=[('CSV Files', '*.csv')]) # gets the filename as string
if file:
file_name = file.name
print(file_name)
click_clear()
csvHeader, csvList = upload.preprocess_csv(file_name)
if len(csvHeader) > 2: # Multinomial
multi_df = pd.read_csv(file_name)
print(multi_df[~multi_df.applymap(np.isreal).all(1)])
null_columns=multi_df.columns[multi_df.isnull().any()]
print(multi_df[multi_df.isnull().any(axis=1)][null_columns].head())
create_data_list() # creates a separate
"""
database function has to be called here
"""
#csvList = load.load_csv_file(file_name)
# Exit GUI cleanly
def _quit():
win.quit()
win.destroy()
exit()
# Creating a Menu Bar
menu_bar = Menu(win)
win.config(menu=menu_bar)
# Add menu items
file_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(label="New", command = open_file)
#file_menu.add_separator()
file_menu.add_command(label="Recent Files")
file_menu.add_command(label="Exit", command=_quit)
menu_bar.add_cascade(label="File", menu=file_menu)
# Add another Menu to the Menu Bar and an item
help_menu = Menu(menu_bar, tearoff=0)
help_menu.add_command(label="About")
menu_bar.add_cascade(label="Help", menu=help_menu)
#-----------------------------------------------------------------------
#-----------------------------------------------------------------------Team2 -Outlier
def median(a, l, r):
n1 = r - l + 1
n1 = (n1 + 1)/ 2 - 1;
return int(n1 + l)
def get_outLiers(x1):
n=len(x1)
x1.sort()
mid_index = median(x1, 0, n)
Q1 = x1[median(x1, 0, mid_index)]
Q3 = x1[median(x1, mid_index + 1, n)]
IQR= Q3 - Q1
Lower_bound = Q1 -(1.5 * IQR)
Upper_bound = Q3 +(1.5 * IQR)
print(Lower_bound,Upper_bound)
outlier_list = list(filter(lambda i: float(i) >Upper_bound or float(i)<Lower_bound, x1))
return outlier_list
def display_outliers():
global data
#data.x.values = [1,58,639,2,3,100000,5]
#data.y.values = [1,58,69,8,5,899999,84]
outlier_list_x = get_outLiers(copy.deepcopy(data.x.values))
data.outlier_x = outlier_list_x
outlier_list_y = get_outLiers(copy.deepcopy(data.y.values))
data.outlier_y = outlier_list_y
textBox.delete(1.0, tk.END)
#textBox.insert(tk.INSERT, 'Outliers for x'+ str(data.outlier_x)+'\n')
display_list_out = list(zip(data.outlier_x, data.outlier_y))
if display_list_out != []:
textBox.insert(tk.INSERT, 'Outliers are '+ str(display_list_out)+'\n')
else:
"""No outliers"""
...
#-----------------------------------------------------------------------team 3
basic_statistics = []
tabControl = ttk.Notebook(win) # Create Tab Control
tab1 = ttk.Frame(tabControl) # Create a tab
tabControl.add(tab1, text='Normal Analysis') # Add the tab
tab2 = ttk.Frame(tabControl) # Add a second tab
tabControl.add(tab2, text='Time Series Analysis') # Make second tab visible
tabControl.pack(expand=1, fill="both") # Pack to make visible
# LabelFrame using tab1 as the parent - for basic data Analysis
mighty = ttk.LabelFrame(tab1, text=' Basic Data Analysis')
mighty.grid(column=0, row=0, padx=8, pady=4)
# LabelFrame using tab1 as the parent - for output console
mighty1 = ttk.LabelFrame(tab1, text=' Output Console')
mighty1.grid(column=1, row=0,sticky=tk.N+tk.S, padx=8, pady=4, columnspan=3, rowspan = 6)
mighty2 = ttk.LabelFrame(tab1, text=' Non Linear Regression ')
mighty2.grid(column = 0, row=1, padx=10, pady=2)
#mighty2.grid_columnconfigure(0, weight=1)
mighty3 = ttk.LabelFrame(tab1,text='Prediction')
mighty3.grid(column=0,row=2,padx=15,pady=4)
# Add big textbox
text_h= 35
text_w = 95
textBox = tk.Text(mighty1, height = text_h, width = text_w,wrap=tk.WORD)
textBox.grid(column=0, row=5, sticky=tk.N+tk.S)
def create_data_list():
global x,y,X,Y, data, multi_data
if csvList != []:
if len(csvHeader) <= 2:
x = [float(i) for i in csvList[0]]
y = [float(i) for i in csvList[-1]]
# Create classes
X = imodel(x)
Y = imodel(y)
data = bdmodel(X, Y)
create_instance()
elif len(csvHeader) > 2:
X = []
for idx,i in enumerate(csvList):
if idx != 0:
temp = [float(j) for j in i]
X.append(temp)
y = [float(i) for i in csvList[0]]
Y = imodel(y)
multi_data = mdmodel(X, Y.values)
create_instance()
else:
print('No Data :(')
# to create instance immediately after fetching data
def create_instance():
global X, Y, data, multi_data
if len(csvHeader) <=2 :
X.mean()
Y.mean()
X.var()
Y.var()
data.corr_coeff()
data.nlr_coef()
data.anova()
data.models()
data.pred_model()
data.outliers()
else:
multi_data.x_stats()
multi_data.y_stats()
multi_data.linear_regression_coeff()
def round_off_list(my_list, precision):
return [round(_, precision) for _ in my_list]
def chunkstring(string, length):
return (string[0+i:length+i] for i in range(0, len(string), length))
# Modified Button statistics Function
def click_stats(textBox):
global X,Y, data,multi_data
precision = 2
if len(csvHeader) <= 2:
textBox.delete(1.0, tk.END) # clear anything previously present
"""
textBox.insert(tk.INSERT, 'x_bar ='+ str(round(X.mean, precision))+'\n')
textBox.insert(tk.INSERT, 'x_var ='+ str(round(X.var,precision))+'\n')
textBox.insert(tk.INSERT, 'x_standard_dev ='+ str(round(math.sqrt(X.var), precision))+'\n')
textBox.insert(tk.INSERT, 'y_bar ='+ str(round(Y.mean, precision))+'\n')
textBox.insert(tk.INSERT, 'y_var ='+ str(round(Y.var, precision))+'\n')
textBox.insert(tk.INSERT, 'y_standard_dev ='+ str(round(math.sqrt(Y.var),precision))+'\n')
#textBox.insert(tk.INSERT, 'Cov(x, y) ='+ str(data.cov())+'\n')
textBox.insert(tk.INSERT, 'Correlation coeeficient ='+ str(round(data.corr_coeff, precision))+'\n')
"""
display_outliers()
table=[["Mean",round(X.mean, precision),round(Y.mean, precision)],["Variance",round(X.var,precision),round(Y.var, precision)],["Std.Deviation",round(math.sqrt(X.var), precision),round(math.sqrt(Y.var),precision)],["Corel Coeff(X,Y)",round(data.corr_coeff, precision)]]
headers= ["","X","Y"]
textBox.insert(tk.INSERT,tabulate(table,headers,tablefmt="fancy_grid", floatfmt=".2f")) # decimal precision 2
if data.corr_coeff < data.threshold:
_stats_msgBox()
else:
"""
textBox.delete(1.0, tk.END) # clear anything previously present
textBox.insert(tk.INSERT, 'X_mean = '+ str(round_off_list(multi_data.x_mean,precision))+'\n')
textBox.insert(tk.INSERT, 'x_var ='+ str(round_off_list(multi_data.x_var, precision))+'\n')
textBox.insert(tk.INSERT, 'x_standard_dev ='+ str(round_off_list(multi_data.x_std_dev, precision))+'\n')
textBox.insert(tk.INSERT, 'y_bar ='+ str(round(multi_data.y_mean,precision))+'\n')
textBox.insert(tk.INSERT, 'y_var ='+ str(round(multi_data.y_var, precision))+'\n')
textBox.insert(tk.INSERT, 'y_standard_dev ='+ str(round(multi_data.y_std_dev,precision))+'\n')
"""
# Create list for table
mean_table = ["Mean"]
mean_table.extend(round_off_list(multi_data.x_mean,precision))
mean_table.append(round(multi_data.y_mean,precision))
variance_table = ["Variance"]
variance_table.extend(round_off_list(multi_data.x_var, precision))
variance_table.append(round(multi_data.y_var, precision))
std_dev_table = ["Std.Deviation"]
std_dev_table.extend(round_off_list(multi_data.x_std_dev, precision))
std_dev_table.append(round(multi_data.y_std_dev,precision))
#pcc_table = ["Partial \n Correlation\n Coeff ryx,"]
#pcc_table.append(pcc_xyz.PartialcorrelationCoefficientXY_Z(multi_data.x[0]))
#correlation_coeff_table = ["Correlation Coeffecient"]
headers = [""]
headers.extend(csvHeader[1:])
headers.append(csvHeader[0])
table = []
table.append(mean_table)
table.append(variance_table)
table.append(std_dev_table)
textBox.delete(1.0, tk.END) # clear anything previously present
textBox.insert(tk.INSERT,tabulate(table,headers,tablefmt="fancy_grid", floatfmt=".2f"))
#textBox.insert(tk.INSERT, 'Cov(x, y) ='+ str(data.cov())+'\n')
#textBox.insert(tk.INSERT, 'Correlation coeeficient ='+ str(round(data.corr_coeff, precision))+'\n')
#if data.corr_coeff < data.threshold:
# _stats_msgBox()
# stats_msgBox: Alert the user when corr_coeff < threshold
def _stats_msgBox():
answer = msg.askyesno('AnalyticsCal Alert'," It appears that the data is not linear. \n Do you wish to take log transforms?")
if answer == True:
print("Yes take Log")
log_plot()
else:
print("No don't!")
def log_plot():
global X, Y, data
X_log = [math.log(i) for i in X.values]
Y_log = [math.log(i) for i in Y.values]
print(X_log)
print(Y_log)
fig, (ax1, ax2) = plt.subplots(2)
#fig.suptitle('Raw plot vs Log plots')
ax1.scatter(X_log, Y_log,color = 'r')
ax1.set_title('Scatter plot of log_y & log_x')
ax1.set(xlabel='Log(X)', ylabel='Log(Y)')
ax2.scatter(X.values, Y.values)
#plt.scatter(X_log, Y_log,)
ax2.set_title('Scatter plot of Y & X')
ax2.set(xlabel='X', ylabel='Y')
#plt.xlabel('x')
#plt.ylabel('y')
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
plt.show()
"""
fig = Figure(figsize=(12, 8), facecolor = 'white')
axis1 = fig.add_subplot(211)
axis2 = fig.add_subplot(212)
axis1.plot(X_log,Y_log)
axis1.set_xlabel('log(X)')
axis1.set_ylabel('log(Y)')
axis1.grid(linestyle='-.')
axis2.plot(X.values,Y.values)
axis2.set_xlabel('X')
axis2.set_ylabel('Y')
axis2.grid(linestyle='-.')
global root
root = tk.Tk()
root.withdraw()
root.protocol('WM_DELETE_WINDOW', _destroyWindow)
#--------------------------------------------------------------
canvas = FigureCanvasTkAgg(fig, master=root)
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
#--------------------------------------------------------------
root.update()
root.deiconify()
root.mainloop()
def _destroyWindow():
global root
root.quit()
root.destroy()
"""
mighty_width = 26
# Add button to output basic statistics
Statistics = ttk.Button(mighty, text="Statistics", command= lambda : click_stats(textBox), width = mighty_width)
Statistics.grid(column=0, row=1, sticky='W', padx = 10,pady = 2)
#----------------------------------------------------------------------Basic Plot
# Modified Button Click Plot
def click_plot():
global X, Y,multi_data, multi_df
if len(csvHeader) <= 2:
plt.scatter(X.values, Y.values,alpha=1)
plt.title('Scatter plot of x and y')
plt.xlabel('x')
plt.ylabel('y')
plt.tight_layout()
plt.show()
else:
"""
jet=plt.get_cmap('jet')
for i in multi_data.x:
plt.scatter(i, multi_data.y , alpha = 1)
"""
#Piarplot Data Visualization, type this code and see the output
#sns.set(style="white")
colors = ['red','green', 'blue', 'orange','purple']
#labels1 = [0,1,2,3,4]
#labels = []
#for i in range(len(multi_data.y)):
# labels.append(labels1[i % 5])
#g = pd.plotting.scatter_matrix(multi_df, figsize=(10,10), marker = 'o', hist_kwds = {'bins': 10}, s = 60, alpha = 0.8,cmap=matplotlib.colors.ListedColormap(colors),c = labels)
g = pd.plotting.scatter_matrix(multi_df, figsize=(10,10), marker = 'o', hist_kwds = {'bins': 10}, s = 60, alpha = 0.8)
plt.suptitle('Scatter Matrix')
#plt.tight_layout()
#plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=-1.0)
plt.show()
#sns.pairplot(multi_df, diag_kind ='kde')
# Add button for plot
plot = ttk.Button(mighty, text="Plot", command=click_plot, width = mighty_width)
plot.grid(column=0, row=0, sticky='W', padx = 10,pady = 2)
# Multi plot
"""
fig = create_plot()
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack()
"""
#----------------------------------------------------------------------Linear Regression
def click_linear_regression():
global X,Y, data,Y_predicted,is_simple_linear_equations,coeff,coeff_m
is_simple_linear_equations = True
roundoff = 2
precision = roundoff
if len(csvHeader) > 2:
global multi_data
print("This is MultiRegression")
coeff = mlr.multi_linear_regression(copy.deepcopy(multi_data.x), copy.deepcopy(multi_data.y))
equation_str = stats_display(round_off_list(coeff, precision))
Y_predicted = form_eqn_mlr(copy.deepcopy(coeff))
coeff_m = coeff
textBox.delete(1.0, tk.END)
textBox.insert(tk.INSERT,"The Linear Regression Equation is\n " + equation_str)
#textBox.tag_add("one", "1.0", "1.8")
#textBox.tag_config("one", background="yellow"_norm)
else:
global reg_order,is_bivariate
is_bivariate = True
reg_order =1
coeff = mlr.multi_linear_regression([X.values], Y.values)
print('click_linear coeff',coeff)
data.linear['coeff'] = round_off_list(coeff, roundoff)
print("data.lnear['coeff'] in click_linear",data.linear['coeff'])
equation_str = stats_display(round_off_list(coeff, precision))
print("BBBBB eequation_str",equation_str)
Y_predicted = form_eqn_mlr(copy.deepcopy(coeff))
#textBox.delete(1.0, tk.END)
#print("BBBBB eequation_str",equation_str)
#data.linear['eqn'] = equation_str
#print("data.pred_model",data.pred_model)
#textBox.insert(tk.INSERT,"The linear regression Equation is\n " + equation_str)
#textBox.tag_add("one", "1.0", "1.8")
#textBox.tag_config("one", background="yellow"_norm)
"""coeff_list =copy.deepcopy(coeff)
Y_predicted = form_eqn(copy.deepcopy(coeff))
coefficient_str = ''
n = len(coeff)
for i in range(len(coeff)):
if(coeff[n - i -1] > 0):
coefficient_str += '+'
coefficient_str += str(round(coeff[n - i - 1], roundoff))
if(i == (n - 2)):
coefficient_str += 'x '
elif(i != (n - 1)):
coefficient_str += 'x'+ str(n - i - 1).translate(SUP) +' '
else:
coefficient_str += str(n - i - 1)
equation_str = str(coefficient_str)
if(equation_str[0] == '+'):
temp = list(equation_str)
del(temp[0])
equation_str = "".join(temp)
data.poly_coeff = []
data.poly_coeff = [round(coeff[i], roundoff) for i in range(n)]
textBox.delete(1.0, tk.END)
textBox.insert(tk.INSERT, equation_str)
"""
title= "Predicted vs Observed"
x_label= 'X'
y_label= 'Y'
reg_plot(X.values, Y.values, Y_predicted, equation_str, title, x_label, y_label, 'g')
plt.show()
"""
regression = nlr(X.values,Y.values)
coefficient = regression.polynomial(1)
coeff_list =copy.deepcopy(coefficient)
Y_predicted = form_eqn(copy.deepcopy(coefficient))
coefficient_str = ''
n = len(coefficient)
for i in range(len(coefficient)):
if(coefficient[n - i -1] > 0):
coefficient_str += '+'
coefficient_str += str(round(coefficient[n - i - 1], 4))
if(i == (n - 2)):
coefficient_str += 'x '
elif(i != (n - 1)):
coefficient_str += 'x'+ str(n - i - 1).translate(SUP) +' '
else:
coefficient_str += str(n - i - 1)
equation_str = str(coefficient_str)
if(equation_str[0] == '+'):
temp = list(equation_str)
del(temp[0])
equation_str = "".join(temp)
data.poly_coeff = []
data.poly_coeff = [round(coefficient[n - i - 1], 4) for i in range(n)]
textBox.delete(1.0, tk.END)
textBox.insert(tk.INSERT, equation_str)
title= "predicted vs actual"
x_label= 'X'
y_label= 'Y'
reg_plot(X.values, Y.values, Y_predicted, equation_str, title, x_label, y_label, 'g')
plt.show()
"""
def stats_display(coeff):
global csvHeader
global multi_data,textBox
global Y_predicted
global is_bivariate
roundoff = 2
SUB = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉") # For printing subscript
SUP = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")
if len(csvHeader) > 2:
#textBox.delete(1.0, tk.END)
#textBox.insert(tk.INSERT, 'str(coeff)' + 'str(coeff)')
coeff_list =copy.deepcopy(coeff)
Y_predicted = form_eqn_mlr(coeff_list)
coefficient_str = ''
n = len(coeff)
for i in range(len(coeff)):
if i > 0:
if coeff[i] > 0:
coefficient_str += '+'
coefficient_str += str(coeff[i])
# note subscript unicode 3 is printed as 1
if i != 3:
coefficient_str += 'x' + str(i).translate(SUB) + ' '
else:
coefficient_str += 'x' + '3 '
else:
coefficient_str += str(coeff[i]) + ' '
equation_str = coefficient_str
if(equation_str[0] == '+'):
temp = list(equation_str)
del(temp[0])
equation_str = "".join(temp)
multi_data.lin_reg_coeff = []
multi_data.lin_reg_coeff = [round(coeff[i], roundoff) for i in range(n)]
multi_data.lin_reg_eqn = equation_str
print('hi',equation_str)
return equation_str
#textBox.delete(1.0, tk.END)
#textBox.insert(tk.INSERT, equation_str)
else:
is_bivariate = True
Y_predicted = form_eqn(copy.deepcopy(coeff))
coefficient_str = ''
n = len(coeff)
for i in range(len(coeff)):
if(coeff[n - i -1] > 0):
coefficient_str += '+'
coefficient_str += str(round(coeff[n - i - 1], roundoff))
if(i == (n - 2)):
coefficient_str += 'x '
elif(i != (n - 1)):
coefficient_str += 'x'+ str(n - i - 1).translate(SUP) +' '
else:
coefficient_str += str(n - i - 1)
equation_str = coefficient_str
if(equation_str[0] == '+'):# remove '+' sign in the first term
temp = list(equation_str)
del(temp[0])
equation_str = "".join(temp)
data.poly_coeff = []
data.poly_coeff = [round(coeff[i],roundoff ) for i in range(n)]
textBox.delete(1.0, tk.END)
textBox.insert(tk.INSERT,'The linear model is \n' + equation_str)
str_1 = '\n'.join(list(chunkstring(equation_str, 14)))
data.linear['eqn'] = str_1
data.pred_eqn = data.linear['eqn']
data.pred_model = data.linear['coeff']
title= "Predicted vs Observed"
x_label= 'X'
y_label= 'Y'
reg_plot(X.values, Y.values, Y_predicted, equation_str, title, x_label, y_label, 'g')
plt.show()
return equation_str
def form_eqn_mlr(coeff):
global multi_data
temp = []
for i in range(len(multi_data.x[0])):
temp.append(coeff[0]+ coeff[1]*multi_data.x[0][i] +coeff[2]*multi_data.x[1][i] + coeff[3]*multi_data.x[2][i])
print('form_eqn_mlr',temp)
return temp
# Add button to Regression
linear_Regression = ttk.Button(mighty, text="Linear Regression", command=click_linear_regression,width = mighty_width)
linear_Regression.grid(column=0, row=2, sticky='W', padx = 10,pady = 2)
#-----------------------------------------------------------------------Non Linear Regerssion
##Polynomail Regression
# Poly_reg:Modified Button Click Function
def click_nlr_poly():
global Y_predicted,is_simple_linear_equations,is_bivariate
is_simple_linear_equations=False
is_bivariate = True
if len(csvHeader) <= 2:
SUB = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉") # For printing subscript
SUP = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")
global X,Y, data
precision = 2
regression = nlr(X.values,Y.values)
global reg_order
reg_order = int(number_chosen_poly.get())
coefficient = regression.polynomial(reg_order)
coeff_list =copy.deepcopy(coefficient)
Y_predicted = form_eqn(copy.deepcopy(coefficient))
coefficient_str = ''
n = len(coefficient)
for i in range(len(coefficient)):
if(coefficient[n - i -1] > 0):
coefficient_str += '+'
coefficient_str += str(round(coefficient[n - i - 1], precision))
if(i == (n - 2)):
coefficient_str += 'x '
elif(i != (n - 1)):
coefficient_str += 'x'+ str(n - i - 1).translate(SUP) +' '
else:
coefficient_str += str(n - i - 1)
equation_str = str(coefficient_str)
if(equation_str[0] == '+'):
temp = list(equation_str)
del(temp[0])
equation_str = "".join(temp)
data.poly_coeff = []
#data.poly_coeff = [round(coefficient[n - i - 1], precision) for i in range(n)]
data.poly_coeff = [round(coefficient[i], precision) for i in range(n)]
textBox.delete(1.0, tk.END)
textBox.insert(tk.INSERT,"The Polynomial model of order "+str(reg_order) +" :\n"+equation_str)
if reg_order == 2:
str_2 = '\n'.join(list(chunkstring(equation_str, 14)))
data.poly_2['eqn'] = str_2 # 12 harcoded for this textbox width = 90
data.poly_2['coeff'] = [round(coefficient[i], precision) for i in range(n)]
data.pred_model = data.poly_2['coeff']
data.pred_eqn = data.poly_2['eqn']
elif reg_order == 3:
str_3 = '\n'.join(list(chunkstring(equation_str, 14)))
data.poly_3['eqn'] = str_3
data.poly_3['coeff'] = [round(coefficient[i], precision) for i in range(n)]
data.pred_model = data.poly_3['coeff']
data.pred_eqn = data.poly_3['eqn']
elif reg_order == 4:
str_4 = '\n'.join(list(chunkstring(equation_str, 14)))
data.poly_4['eqn'] = str_4
data.poly_4['coeff'] = [round(coefficient[i], precision) for i in range(n)]
data.pred_model = data.poly_4['coeff']
data.pred_eqn = data.poly_4['eqn']
#raw_plot = plt.scatter(X.values, Y.values, color = 'r')
#plt.plot(X.values, round (coeff_list[0] + (coeff_list[1]*X.values) + (coeff_list[2]*(X.values**2))+ (coeff_list[3]*(X.values**3)), 4),'-')
#predict_plot = plt.plot(X.values, Y_predicted, '-')
title= "Predicted vs Observed"
x_label= 'X'
y_label= 'Y'
#plt.legend((raw_plot, predict_plot),('Raw Data', 'Predicted'),scatterpoints=1, ncol=3, fontsize=8)
#plt.legend([red_dot, (red_dot, white_cross)], ["Attr A", "Attr A+B"])
reg_plot(X.values, Y.values, Y_predicted, equation_str, title, x_label, y_label, 'r')
plt.show()
else:
global multi_data
textBox.delete(1.0, tk.END)
textBox.insert(tk.INSERT, "Polynomial Regression works for bivariate data only\n")
# Add button for nonlinear_regression
polynomial_regression = ttk.Button(mighty2, text="Polynomial Regression", command=click_nlr_poly,width = 26)
polynomial_regression.grid(column=0, row=0, sticky='W',padx = 9,pady =3 )
# Order for polynomial_reg
number_poly = tk.IntVar()
number_chosen_poly = ttk.Combobox(mighty2, width=3, textvariable=number_poly, state='readonly')
number_chosen_poly['values'] = (1, 2, 3, 4)
number_chosen_poly.grid(column=1, row=0)
number_chosen_poly.current(0)
# Eqn for poly regression
def form_eqn(coeff_list):
global X
n = len(coeff_list)
len_diff = 4 - n
for _ in range(len_diff):
coeff_list.append(0)
print("coeff_list aft:",coeff_list)
eqn = [round (coeff_list[0] + (coeff_list[1]*x) + (coeff_list[2]*(x**2))+ (coeff_list[3]*(x**3)), 4) for x in X.values]
print(eqn)
return eqn
## Sinusoidal Regression
def click_nlr_sin():
global X,Y, data
if len(csvHeader) <= 2:
regression = nlr(X.values,Y.values)
#print(int(number_chosen_sin.get()))
#coefficient = regression.sinusoidal(int(number_chosen_sin.get()))
coefficient = regression.sinusoidal(4)
textBox.delete(1.0, tk.END)
textBox.insert(tk.INSERT, str(coefficient))
else:
textBox.delete(1.0, tk.END)
textBox.insert(tk.INSERT, "Sinusoidal Regression works for bivariate data only\n")
sinusoidal_regression = ttk.Button(mighty2, text="Sinusoidal Regression", command=click_nlr_sin,width = 26)
sinusoidal_regression.grid(column=0, row=1, sticky='W',padx=10,pady = 2)
"""
# Order for sinusoidal_reg
number_sin = tk.StringVar()
number_chosen_sin = ttk.Combobox(mighty2, width=3, textvariable=number_sin, state='readonly')
number_chosen_sin['values'] = (1, 2, 3, 4)
number_chosen_sin.grid(column=1, row=1)
number_chosen_sin.current(0)
"""
## Exponential Regression
def click_nlr_exp():
global X,Y, data
if len(csvHeader) <=2:
regression = nlr(X.values,Y.values)
#print('regression = ',regression)
coefficient = regression.exponential(2)
if(math.isnan(coefficient[0])):
coefficient_str = "The exponential model is not a right fit for this data"
print(coefficient_str)
#print('coeff = ', coefficient)
else:
coefficient_str = [str(i) for i in coefficient ]
#print(coefficient_str)
textBox.delete(1.0, tk.END)
textBox.insert(tk.INSERT, coefficient_str[0]+'e^'+coefficient_str[1]+'x')
else:
textBox.delete(1.0, tk.END)
textBox.insert(tk.INSERT, "Exponential Regression works for bivariate data only\n")
exponential_regression = ttk.Button(mighty2, text="Exponential Regression", command=click_nlr_exp,width = 26)
exponential_regression.grid(column=0, row=2, sticky='W',padx = 10,pady = 2)
## Exponential Transformation
def click_nlr_exp_trf():
global X,Y, data
if len(csvHeader) <=2:
SUB = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉") # For printing subscript
SUP = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")
regression = nlr(X.values,Y.values)
#print('regression = ',regression)
coefficient = regression.exponentialTransformation(1)
if(math.isnan(coefficient[0])):
coefficient_str = "The exponential model is not a right fit for this data"
print(coefficient_str)
#print('coeff = ', coefficient)
else:
coefficient_str = [str(i) for i in coefficient ]
#print(coefficient_str)
textBox.delete(1.0, tk.END)
textBox.insert(tk.INSERT, coefficient_str[0]+'e^'+coefficient_str[1]+'x')
else:
textBox.delete(1.0, tk.END)
textBox.insert(tk.INSERT, "Exponential Transformation implemented for bivariate data only\n")
exponential_transformation = ttk.Button(mighty2, text="Exponential Transformation", command=click_nlr_exp_trf,width = 26)
exponential_transformation.grid(column=0, row=3, sticky='W', padx = 10,pady = 2)
""" To be implemented
# Order for exponential_reg
number_exp = tk.IntVar()
number_chosen_exp = ttk.Combobox(mighty2, width=3, textvariable=number_exp, state='readonly')
number_chosen_exp['values'] = (1, 2)
number_chosen_exp.grid(column=1, row=2)
number_chosen_exp.current(0)
"""
#--------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------ANOVA
def click_anova():
global X,Y,data,reg_order
global Y_predicted,coeff
precision = 2
if len(csvHeader) <= 2:
anova_dict = anova_main(X.values, Y.values,data.poly_coeff)
anova_class_2 = Anova_class(Y.values, Y_predicted, len(csvHeader))
data.msr = anova_dict['msr']
data.mse = anova_dict['mse']
data.ssr = anova_dict['ssr']
data.sse = anova_dict['sse']
data.f = anova_dict['f']
data.p = anova_dict['p']
data.model_confidence=anova_class_2.model_confidence
#textBox.delete(1.0, tk.END)
textBox.insert(tk.INSERT,"\n")
"""
textBox.insert(tk.INSERT, 'ANOVA'+ '\n')
textBox.insert(tk.INSERT, 'msr = '+ str(round(data.msr, 4)) + '\n')
textBox.insert(tk.INSERT, 'mse = '+ str(round(data.mse, 4))+ '\n')
textBox.insert(tk.INSERT, 'ssr = '+ str(round(data.ssr, 4)) + '\n')
textBox.insert(tk.INSERT, 'sse = '+ str(round(data.sse,4)) + '\n')
textBox.insert(tk.INSERT, 'f = '+ str(round(data.f, 4)) + '\n')
textBox.insert(tk.INSERT, 'p = '+ str(round(data.p,4)) + '\n')
"""
table=[["MSR",data.msr],["MSE",data.mse],["SSR",data.ssr],["SSE",data.sse],["f",data.f],["p",data.p],["Model Confidence",data.model_confidence]]
headers= ["ANOVA","Values"]
textBox.insert(tk.INSERT,tabulate(table,headers,tablefmt="fancy_grid",floatfmt=".2f"))
if(is_simple_linear_equations):
anova_CI=AnovaConfidenceInterval(X.values,Y.values,Y_predicted,len(csvHeader))
ci_rtn=anova_CI.cal_CI_tm_tc(95)
data.t_m=ci_rtn["tm"]
data.t_c=ci_rtn["tc"]
textBox.insert(tk.INSERT, "\n\n Confidence Interval:\n")
ci_table=[
["m",str(round(coeff[1] - data.t_m,4)),str(round(coeff[1] + data.t_m,4))],
["c",str(round(coeff[0] - data.t_c,4)),str(round(coeff[0] + data.t_c,4))]
]
textBox.insert(tk.INSERT,tabulate(ci_table,["Coeffient","min","max"],tablefmt="fancy_grid", floatfmt=".2f"))
if reg_order == 2:
#str_2 = '\n'.join(list(chunkstring(equation_str, 12)))
data.poly_2['f'] = round(data.f, precision)
data.poly_2['p'] = round(data.p, precision)
elif reg_order == 3:
data.poly_3['f'] = round(data.f, precision)
data.poly_3['p'] = round(data.p, precision)
elif reg_order == 4:
#str_4 = '\n'.join(list(chunkstring(equation_str, 14)))
data.poly_4['f'] = round(data.f, precision)
data.poly_4['p'] = round(data.p, precision)
elif reg_order == 1:
data.linear['f'] = round(data.f, precision)
data.linear['p'] = round(data.p, precision)
#if(is_simple_linear_equations):
# textBox.insert(tk.INSERT, "\n\n Confidence Interval:\n")
# ci_table=[
# ["m",str(round(coeff[1] - data.t_m,4)),str(round(coeff[1] + data.t_m,4))],
# ["c",str(round(coeff[0] - data.t_c,4)),str(round(coeff[0] + data.t_c,4))]
# ]
#textBox.insert(tk.INSERT,tabulate(ci_table,["Coeffient","min","max"],tablefmt="fancy_grid", floatfmt=".2f"))
else:
# Add the code for multiple linear regression
anova_class_2 = Anova_class(Y.values, Y_predicted, len(csvHeader))
multi_data.msr = anova_class_2.msr
multi_data.mse = anova_class_2.mse
multi_data.ssr = anova_class_2.ssr
multi_data.sse = anova_class_2.sse
multi_data.f = anova_class_2.f
multi_data.p = anova_class_2.p
multi_data.model_confidence=anova_class_2.model_confidence
textBox.insert(tk.INSERT,"\n\n\n Anova Values: \n")
table=[ ["Regression",anova_class_2.ssr_drg_of_freedom,round(anova_class_2.ssr,4),round(anova_class_2.msr,4),round(anova_class_2.f,4),str(round(anova_class_2.p,4))],
["Error",anova_class_2.sse_dgr_pf_freedom,round(anova_class_2.sse,4),round(anova_class_2.mse,4),None,None],
["Total",anova_class_2.sse_dgr_pf_freedom+anova_class_2.ssr_drg_of_freedom,round(anova_class_2.sse + anova_class_2.ssr,4),None,None,None]
]
table.append([])
textBox.insert(tk.INSERT,tabulate(table,['Source','df','SS','MS','F','P'],tablefmt="fancy_grid", floatfmt=".2f"))
textBox.insert(tk.INSERT, "\n")
# Add button for ANOVA
anova = ttk.Button(mighty, text="ANOVA", command=click_anova,width = mighty_width)
anova.grid(column=0, row=4, sticky='W', padx = 10,pady = 2)
#------------------------------------------------------------------------------Comparison
def table_highlight (is_clear = True,max_f_table = None):
if(is_clear):
bg_color_norm = "white"
bg_color_eqn = bg_color_norm
max_f_table = 0
#.tag_remove(tagName, index1, index2=None) remove tag
textBox.tag_delete("1,1", "2.12", "2.29")
textBox.tag_delete("1,2", "3.12", "3.29")
textBox.tag_delete("1,3", "4.12", "4.29")
textBox.tag_delete("1,4", "5.12", "5.29")
textBox.tag_delete("1,5", "6.12", "6.29")
textBox.tag_delete("1,6", "7.12", "7.29")
textBox.tag_delete("1,7", "8.12", "8.29")
textBox.tag_delete("1,8", "9.12", "9.29")
textBox.tag_delete("1,9", "10.12", "10.29")
textBox.tag_delete("1,10", "11.12", "11.29")
textBox.tag_delete("two,1", "2.30", "2.49")
textBox.tag_delete("two,2", "3.30", "3.49")
textBox.tag_delete("two,3", "4.30", "4.49")
textBox.tag_delete("two,4", "5.30", "5.49")
textBox.tag_delete("two,5", "6.30", "6.49")
textBox.tag_delete("two,6", "7.30", "7.49")
textBox.tag_delete("two,7", "8.30", "8.49")
textBox.tag_delete("two,8", "9.30", "9.49")
textBox.tag_delete("two,9", "10.30", "10.49")
textBox.tag_delete("two,10", "11.30", "11.49")
#textBox.tag_delete("one,11", "13.12", "2.32")
textBox.tag_delete("3,1", "2.50", "2.68")
textBox.tag_delete("3,2", "3.50", "3.68")
textBox.tag_delete("3,3", "4.50", "4.68")
textBox.tag_delete("3,4", "5.50", "5.68")
textBox.tag_delete("3,5", "6.50", "6.68")
textBox.tag_delete("3,6", "7.50", "7.68")
textBox.tag_delete("3,7", "8.50", "8.68")
textBox.tag_delete("3,8", "9.50", "9.68")
textBox.tag_delete("3,9", "10.50", "10.68")
textBox.tag_delete("3,10", "11.50", "11.68")
textBox.tag_delete("4,1", "2.69", "2.89")
textBox.tag_delete("4,2", "3.69", "3.89")
textBox.tag_delete("4,3", "4.69", "4.89")
textBox.tag_delete("4,4", "5.69", "5.89")
textBox.tag_delete("4,5", "6.69", "6.89")
textBox.tag_delete("4,6", "7.69", "7.89")
textBox.tag_delete("4,7", "8.69", "8.89")
textBox.tag_delete("4,8", "9.69", "9.89")
textBox.tag_delete("4,9", "10.69", "10.89")
textBox.tag_delete("4,10", "11.69", "11.89")
print("removed tag")
else:
bg_color_norm = "yellow"
bg_color_eqn = "red"
textBox.tag_add("1,1", "2.12", "2.29")
textBox.tag_add("1,2", "3.12", "3.29")
textBox.tag_add("1,3", "4.12", "4.29")
textBox.tag_add("1,4", "5.12", "5.29")
textBox.tag_add("1,5", "6.12", "6.29")
textBox.tag_add("1,6", "7.12", "7.29")
textBox.tag_add("1,7", "8.12", "8.29")
textBox.tag_add("1,8", "9.12", "9.29")
textBox.tag_add("1,9", "10.12", "10.29")
textBox.tag_add("1,10", "11.12", "11.29")
textBox.tag_add("two,1", "2.30", "2.49")
textBox.tag_add("two,2", "3.30", "3.49")
textBox.tag_add("two,3", "4.30", "4.49")
textBox.tag_add("two,4", "5.30", "5.49")
textBox.tag_add("two,5", "6.30", "6.49")
textBox.tag_add("two,6", "7.30", "7.49")
textBox.tag_add("two,7", "8.30", "8.49")
textBox.tag_add("two,8", "9.30", "9.49")
textBox.tag_add("two,9", "10.30", "10.49")
textBox.tag_add("two,10", "11.30", "11.49")
#textBox.tag_add("one,11", "13.12", "2.32")
textBox.tag_add("3,1", "2.50", "2.68")
textBox.tag_add("3,2", "3.50", "3.68")
textBox.tag_add("3,3", "4.50", "4.68")
textBox.tag_add("3,4", "5.50", "5.68")
textBox.tag_add("3,5", "6.50", "6.68")
textBox.tag_add("3,6", "7.50", "7.68")
textBox.tag_add("3,7", "8.50", "8.68")
textBox.tag_add("3,8", "9.50", "9.68")