1
1
"""
2
- Show statistical information
2
+ Show statistical information
3
3
4
4
Le, Phong D. - le.duc.phong@gmail.com
5
5
Date: Sep 19, 2020
@@ -36,6 +36,40 @@ def corshow(X, Y):
36
36
print ('Kendall tau Correlation between X and Y is \n {}' .format (kendalltau (X , Y )[0 ]))
37
37
38
38
39
+ def visualize2D (X ):
40
+ '''
41
+ Show histogram of the variable X
42
+ '''
43
+
44
+ # Calculate the number of bins using Sturges' rule
45
+ # Formula: bins = ceil(1 + log2(n))
46
+ bins = np .ceil (1 + np .log2 (len (X ))).astype ('int' )
47
+ plt .hist (X , bins = bins , density = True )
48
+
49
+ def visualize3D (X , Y ):
50
+ '''
51
+ Show 3D histogram of joint probabily of X and Y
52
+ '''
53
+ # Determine the number of bins using Sturges' rule
54
+ binX = np .ceil (1 + np .log2 (len (X ))).astype ('int' )
55
+ binY = np .ceil (1 + np .log2 (len (Y ))).astype ('int' )
56
+
57
+ # Create 2d histogram
58
+ Hist , _ , _ = np .histogram2d (X , Y , bins = [binX , binY ])
59
+
60
+ # Define 3d axes
61
+ fig = plt .figure ()
62
+ ax = fig .add_subplot (111 , projection = '3d' )
63
+
64
+ # Create an X-Y mesh of the 2D data
65
+ x_data , y_data = np .meshgrid (np .arange (Hist .shape [1 ]), np .arange (Hist .shape [0 ]))
66
+
67
+ # Flatten out the arrays to pass to bar3d
68
+ x_data = x_data .flatten ()
69
+ y_data = y_data .flatten ()
70
+ z_data = Hist .flatten ()
71
+ ax .bar3d (x_data , y_data , np .zeros (len (z_data )), 1 , 1 , z_data )
72
+
39
73
40
74
if __name__ == '__main__' :
41
75
# X, a random Gaussian distribution
@@ -52,7 +86,11 @@ def corshow(X, Y):
52
86
# Show correlation information between X and Y
53
87
corshow (X , Y )
54
88
55
- # Plot histogram of the variable
56
- plt .hist (X )
57
- plt .hist (Y )
89
+ # Plot 2D histogram of the variable
90
+ visualize2D (X )
91
+ visualize2D (Y )
92
+ plt .show ()
93
+
94
+ # Plot 3D histogram of the two variables
95
+ visualize3D (X , Y )
58
96
plt .show ()
0 commit comments