-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_visualization
More file actions
216 lines (148 loc) · 5.53 KB
/
data_visualization
File metadata and controls
216 lines (148 loc) · 5.53 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
# This is program is only for reference for various plotting methods
# Import Standard Libraries
import numpy as np
import pandas as pd
from numpy.random import randn
import warnings
warnings.filterwarnings('ignore')
# stats libraries
from scipy import stats
# Plotting libraries
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
url = 'http://en.wikipedia.org/wiki/Histogram'
dataset1 = randn(100)
# check dataset
plt.hist(dataset1)
dataset2 = randn(80)
plt.hist(dataset2,color='indianred')
plt.hist(dataset1,normed=True,color='indianred',alpha=0.5,bins=20)
plt.hist(dataset2,normed=True,alpha=0.5,bins=20)
# Try Joint plot
data1 = randn(1000)
data2 = randn(1000)
sns.jointplot(data1,data2)
sns.jointplot(data1,data2,kind='hex') # with hex
#Kernel Density Estimation Plots (Kde) For more details refer KDE wikipedia (Gaussian basis function)
dataset = randn(25)
sns.rugplot(dataset)
plt.ylim(0,1)
plt.hist(dataset,alpha=0.3)
sns.rugplot(dataset)
sns.rugplot(dataset)
x_min = dataset.min() - 2
x_max = dataset.max() + 2
x_axis = np.linspace(x_min,x_max,100)
bandwidth = ( (4*dataset.std()**5) / (3*len(dataset))) ** 0.2
kernel_list = []
for data_point in dataset:
# Create a kernel for each point and append it to kernel_list
kernel = stats.norm(data_point,bandwidth).pdf(x_axis)
kernel_list.append(kernel)
# Scale for plotting
kernel = kernel / kernel.max()
kernel = kernel * 0.4
plt.plot(x_axis,kernel,color='grey',alpha=0.5)
plt.ylim(0,1)
sum_of_kde = np.sum(kernel_list,axis=0)
fig = plt.plot(x_axis,sum_of_kde,color='indianred')
sns.rugplot(dataset)
plt.yticks([])
plt.suptitle("Sum of basis functions")
sns.rugplot(dataset,color='black')
for bw in np.arange(0.5,2,0.25):
sns.kdeplot(dataset,bw=bw,lw=1.8,label=bw)
# Read url
url = 'http://en.wikipedia.org/wiki/Kernel_(statistics)'
kernel_options = ['biw','cos','epa','gau','tri','triw']
for kern in kernel_options:
sns.kdeplot(dataset,kernel=kern,label=kern,shade=True)
sns.kdeplot(dataset,vertical=True)
url = 'http://en.wikipedia.org/wiki/Cumulative_distribution_function'
sns.kdeplot(dataset,cumulative = True)
mean = [0,0]
cov = [[1,0],[0,100]]
dataset2 = np.random.multivariate_normal(mean,cov,1000)
dframe = pd.DataFrame(dataset2,columns=['X','Y'])
sns.kdeplot(dframe)
sns.kdeplot(dframe.X,dframe.Y,shade=True)
sns.kdeplot(dframe,bw=1)
sns.kdeplot(dframe,bw='silverman')
sns.jointplot('X','Y',dframe,kind='kde')
# Combining Plot Styles
dataset = randn(100)
sns.distplot(dataset,bins=25)
sns.distplot(dataset,bins=25,rug=True,hist=False)
sns.distplot(dataset,bins=25,
kde_kws={'color':'indianred','label':'KDE PLOT'},
hist_kws={'color':'blue','label':'HIST'})
from pandas import Series
ser1 = Series(dataset,name='My_data')
ser1
sns.distplot(ser1,bins=25)
# Box and Violin Plots
url = 'http://en.wikipedia.org/wiki/Box_plot#mediaviewer/File:Boxplot_vs_PDF.svg'
data1 = randn(100)
data2 = randn(100)
sns.boxplot([data1,data2])
sns.boxplot([data1,data2],whis=np.inf)
# Normal Dist
data1 = stats.norm(0,5).rvs(100)
# Two gamma dist. Concatenated together
data2 = np.concatenate([stats.gamma(5).rvs(50)-1,
-1*stats.gamma(5).rvs(50)])
# Box Plot both data1 and data2
sns.boxplot([data1,data2],whis=np.inf)
sns.violinplot(data1,data2)
sns.violinplot(data2,bw=0.01)
sns.violinplot(data1,inner='stick')
# Regression Plots
tips = sns.load_dataset('tips')
tips.head()
sns.lmplot('total_bill','tip',tips)
sns.lmplot('total_bill','tip',tips,
scatter_kws={'marker':'o','color':'indianred'},
line_kws={'linewidth':1,'color':'blue'})
sns.lmplot('total_bill','tip',tips,order=4,
scatter_kws={'marker':'o','color':'indianred'},
line_kws={'linewidth':1,'color':'blue'})
sns.lmplot('total_bill','tip',tips,fit_reg=False)
tips['tip_pect']=100*(tips['tip']/tips['total_bill'])
tips.head()
sns.lmplot('size','tip_pect',tips)
url = 'http://en.wikipedia.org/wiki/jitter'
sns.lmplot('size','tip_pect',tips,x_jitter=.1)
sns.lmplot('size','tip_pect',tips,x_estimator=np.mean)
sns.lmplot('total_bill','tip_pect',tips,hue='sex',markers=['x','o'])
sns.lmplot('total_bill','tip_pect',tips,hue='day')
url = 'http://en.wikipedia.org/wiki/Local_regression'
sns.lmplot('total_bill','tip_pect',tips,lowess=True,line_kws={'color':'black'})
sns.regplot('total_bill','tip_pect',tips)
fig, (axis1,axis2) = plt.subplots(1,2,sharey=True)
sns.regplot('total_bill','tip_pect',tips,ax=axis1)
sns.violinplot(tips['tip_pect'],tips['size'],color='indianred',ax=axis2)
# Heatmap and clustered Matrics
flight_dframe = sns.load_dataset('flights')
flight_dframe.head()
flight_dframe = flight_dframe.pivot('month','year','passengers')
flight_dframe
sns.heatmap(flight_dframe)
sns.heatmap(flight_dframe,annot=True,fmt='d')
sns.heatmap(flight_dframe,center=flight_dframe.loc['January',1955])
f,(axis1,axis2) = plt.subplots(2,1)
yearly_flights = flight_dframe.sum()
years = pd.Series(yearly_flights.index.values)
years = pd.DataFrame(years)
flights = pd.Series(yearly_flights.values)
flights = pd.DataFrame(flights)
year_dframe = pd.concat((years,flights),axis=1)
year_dframe.columns = ['Year','Flights']
sns.barplot('Year',y='Flights',data=year_dframe,ax=axis1)
sns.heatmap(flight_dframe,cmap='Blues',ax=axis2,cbar_kws={'orientation':'horizontal'})
sns.clustermap(flight_dframe)
sns.clustermap(flight_dframe,col_cluster=False)
sns.clustermap(flight_dframe,standard_scale=1)
sns.clustermap(flight_dframe,standard_scale=0)
sns.clustermap(flight_dframe,z_score=1)