File size: 5,050 Bytes
0ab7b0c |
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 |
#import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import os
plt.style.use('seaborn-colorblind')
# 2018.11.07 Created by Eamon.Zhang
def get_dtypes(data,drop_col=[]):
"""Return the dtypes for each column of a pandas Dataframe
Parameters
----------
data : pandas Dataframe
drop_col : columns to omit in a list
Returns
-------
str_var_list, num_var_list, all_var_list
"""
name_of_col = list(data.columns)
num_var_list = []
str_var_list = []
all_var_list = []
str_var_list = name_of_col.copy()
for var in name_of_col:
# check if column belongs to numeric type
if (data[var].dtypes in (np.int, np.int64, np.uint, np.int32, np.float,
np.float64, np.float32, np.double)):
str_var_list.remove(var)
num_var_list.append(var)
# drop the omit column from list
for var in drop_col:
if var in str_var_list:
str_var_list.remove(var)
if var in num_var_list:
num_var_list.remove(var)
all_var_list.extend(str_var_list)
all_var_list.extend(num_var_list)
return str_var_list, num_var_list, all_var_list
def describe(data,output_path=None):
"""output the general description of a pandas Dataframe
into a csv file
"""
result = data.describe(include='all')
if output_path is not None:
output = os.path.join(output_path,'describe.csv')
result.to_csv(output)
print('result saved at:', str(output))
return result
def discrete_var_barplot(x,y,data,output_path=None):
"""draw the barplot of a discrete variable x against y(target variable).
By default the bar shows the mean value of y.
Parameters
----------
Returns
-------
figure save as PNG
"""
plt.figure(figsize=(15,10))
sns.barplot(x=x,y=y,data=data)
if output_path is not None:
output = os.path.join(output_path,'Barplot_'+str(x)+'_'+str(y)+'.png')
plt.savefig(output)
print('Image saved at', str(output))
def discrete_var_countplot(x,data,output_path=None):
"""draw the countplot of a discrete variable x.
Parameters
----------
Returns
-------
figure save as PNG
"""
plt.figure(figsize=(15,10))
sns.countplot(x=x,data=data)
if output_path is not None:
output = os.path.join(output_path,'Countplot_'+str(x)+'.png')
plt.savefig(output)
print('Image saved at',str(output))
def discrete_var_boxplot(x,y,data,output_path=None):
"""draw the boxplot of a discrete variable x against y.
Parameters
----------
Returns
-------
figure save as PNG
"""
plt.figure(figsize=(15,10))
sns.boxplot(x=x,y=y,data=data)
if output_path is not None:
output = os.path.join(output_path,'Boxplot_'+str(x)+'_'+str(y)+'.png')
plt.savefig(output)
print('Image saved at',str(output))
def continuous_var_distplot(x,output_path=None,bins=None):
"""draw the distplot of a continuous variable x.
Parameters
----------
Returns
-------
figure save as PNG
"""
plt.figure(figsize=(15,10))
sns.distplot(a=x,kde=False,bins=bins)
if output_path is not None:
output=os.path.join(output_path,'Distplot_'+str(x.name)+'.png')
plt.savefig(output)
print('Image saved at',str(output))
# 2018.11.28 Created by Eamon.Zhang
def scatter_plot(x,y,data,output_path=None):
"""draw the scatter-plot of two variables.
Parameters
----------
Returns
-------
figure save as PNG
"""
plt.figure(figsize=(15,10))
sns.scatterplot(x=x,y=y,data=data)
if output_path is not None:
output = os.path.join(output_path,'Scatter_plot_'+str(x.name)+'_'+str(y.name)+'.png')
plt.savefig(output)
print('Image saved at',str(output))
def correlation_plot(data,output_path=None):
"""draw the correlation plot between variables.
Parameters
----------
Returns
-------
figure save as PNG
"""
corrmat = data.corr()
fig, ax = plt.subplots()
fig.set_size_inches(11,11)
sns.heatmap(corrmat,cmap="YlGnBu",linewidths=.5,annot=True)
if output_path is not None:
output = os.path.join(output_path,'Corr_plot'+'.png')
plt.savefig(output)
print('Image saved at',str(output))
def heatmap(data,output_path=None,fmt='d'):
"""draw the heatmap between 2 variables.
Parameters
----------
Returns
-------
figure save as PNG
"""
fig, ax = plt.subplots()
fig.set_size_inches(11,11)
sns.heatmap(data,cmap="YlGnBu",linewidths=.5,annot=True,fmt=fmt)
if output_path is not None:
output = os.path.join(output_path,'Heatmap'+'.png')
plt.savefig(output)
print('Image saved at',str(output)) |