import numpy as np
import pandas as pd
from numpy.random import randn
from scipy import stats
import matplotlib as mpl
import matplotlib.pyplot as plt
/opt/conda/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
/opt/conda/lib/python3.5/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
import seaborn as sns
%matplotlib inline 
data1 = randn(100)
plt.hist(data1)
(array([  1.,   3.,   7.,  15.,  31.,  18.,  11.,  11.,   2.,   1.]),
 array([-3.14066754, -2.50559038, -1.87051322, -1.23543606, -0.6003589 ,
         0.03471825,  0.66979541,  1.30487257,  1.93994973,  2.57502689,
         3.21010405]),
 <a list of 10 Patch objects>)
png

png

data2= randn(80)
plt.hist(data2, color ='indianred')
(array([  1.,   6.,   9.,  12.,  12.,  19.,   9.,   3.,   4.,   5.]),
 array([-2.45946671, -1.97723166, -1.49499661, -1.01276156, -0.5305265 ,
        -0.04829145,  0.4339436 ,  0.91617865,  1.39841371,  1.88064876,
         2.36288381]),
 <a list of 10 Patch objects>)
png

png

plt.hist(data1, normed = True, color ='indianred', alpha = 0.5, bins = 20)
plt.hist(data2, normed = True, alpha =0.5, bins = 20)
(array([ 0.05184194,  0.        ,  0.15552582,  0.15552582,  0.15552582,
         0.31105163,  0.15552582,  0.46657745,  0.31105163,  0.31105163,
         0.46657745,  0.51841939,  0.31105163,  0.15552582,  0.        ,
         0.15552582,  0.10368388,  0.10368388,  0.10368388,  0.15552582]),
 array([-2.45946671, -2.21834919, -1.97723166, -1.73611413, -1.49499661,
        -1.25387908, -1.01276156, -0.77164403, -0.5305265 , -0.28940898,
        -0.04829145,  0.19282607,  0.4339436 ,  0.67506113,  0.91617865,
         1.15729618,  1.39841371,  1.63953123,  1.88064876,  2.12176628,
         2.36288381]),
 <a list of 20 Patch objects>)
png

png

data1 = randn(1000)
data2 = randn(1000)
sns.jointplot(data1, data2)
<seaborn.axisgrid.JointGrid at 0x7fdad01a98d0>
png

png

sns.jointplot(data1, data2, kind = 'hex')
<seaborn.axisgrid.JointGrid at 0x7fdac54955c0>
png

png

dataset = randn(25)
sns.rugplot(dataset)
<matplotlib.axes._subplots.AxesSubplot at 0x7fdac532f8d0>
png

png

plt.hist(dataset,alpha = 0.3)
sns.rugplot(dataset)
<matplotlib.axes._subplots.AxesSubplot at 0x7fdac51ad2e8>
png

png

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 the 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)
(0, 1)
png

png

sum_of_kde = np.sum(kernel_list, axis =0)
fig = plt.plot(x_axis, sum_of_kde, color = 'indianred')
sns.rugplot(dataset)
plt.suptitle("Sum of the basis functions")
<matplotlib.text.Text at 0x7fdac4d9c400>
png

png

sns.kdeplot(dataset)
<matplotlib.axes._subplots.AxesSubplot at 0x7fdac4e40080>
png

png

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)
    
    
png

png

kernel_options = ['biw','cos','epa','gau','tri','triw']
for kern in kernel_options:
    sns.kdeplot(dataset, kernel = kern, label = kern, shade = True)
png

png

sns.kdeplot(dataset, vertical = True)
<matplotlib.axes._subplots.AxesSubplot at 0x7fdac4fdf588>
png

png

sns.kdeplot(dataset, cumulative = True)
<matplotlib.axes._subplots.AxesSubplot at 0x7fdac4eb5a58>
png

png

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)
<matplotlib.axes._subplots.AxesSubplot at 0x7fdac4fe8cf8>
png

png

sns.kdeplot(dframe.X, dframe.Y, shade = True)
<matplotlib.axes._subplots.AxesSubplot at 0x7fdac4bbc0f0>
png

png

sns.kdeplot(dframe, bw ='silverman')
<matplotlib.axes._subplots.AxesSubplot at 0x7fdac4b80470>
png

png

sns.jointplot('X','Y', dframe, kind = 'kde')
<seaborn.axisgrid.JointGrid at 0x7fdac4ac2cc0>
png

png