Seaborn: Statistical Data Visualization
Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics.
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
dv = cd.diagnosis # dependent variable # M or B
list = ['id','diagnosis']
iv = cd.drop(list,axis = 1 ) # independent variable
iv.head()
Data Visualization : Cancer Data
Count plot ->
A count plot can be thought of as a histogram across a categorical, instead of quantitative, variable. The basic API and options are identical to those for barplot(), so you can compare counts across nested variables.
Violin Plot ->
A violin plot is a method of plotting numeric data. It is similar to a box plot, with the addition of a rotated kernel density plot on each side. Violin plots are similar to box plots, except that they also show the probability density of the data at different values, usually smoothed by a kernel density estimator.
y=dv
x=iv
data_n_2 = (iv - iv.mean()) / (iv.std()) # standardization
data = pd.concat([y,data_n_2.iloc[:,0:10]],axis=1)
data = pd.melt(data,id_vars="diagnosis",
var_name="features",
value_name='value')
plt.figure(figsize=(10,10))
sns.violinplot(x="features", y="value", hue="diagnosis", data=data,split=True, inner="quart", aspect=9)
plt.grid(True,color='G')
plt.xticks(rotation=90)
Cat Plot ->
Factor Plot ->
g = sns.factorplot(x="features", y="value", hue="diagnosis",
data=data, kind="box", aspect=3)
Box Plot ->
The box plot is a standardized way of displaying the distribution of data based on the five number summary: minimum, first quartile, median, third quartile, and maximum.
Joint Plot ->
Joint Plot ->
sns.jointplot(x.loc[:,'concavity_worst'], x.loc[:,'concave points_worst'], kind="regg", color="#ce1414")
Scatter Plot ->
A scatter plot is a type of plot or mathematical diagram using Cartesian coordinates to display values for typically two variables for a set of data. If the points are coded, one additional variable can be displayed.
Pair Grid Plot ->
Subplot grid for plotting pairwise relationships in a dataset.
This class maps each variable in a dataset onto a column and row in a grid of multiple axes. Different axes-level plotting functions can be used to draw bivariate plots in the upper and lower triangles, and the the marginal distribution of each variable can be shown on the diagonal.
sns.set(style="white")
df = x.loc[:,['radius_worst','perimeter_worst','area_worst']]
g = sns.PairGrid(df, diag_sharey=False)
g.map_lower(sns.kdeplot, cmap="Blues_d")
g.map_upper(plt.scatter)
g.map_diag(sns.kdeplot, lw=3)
g = g.add_legend()
Color the points using a categorical variable
Swarm Plot ->
import time
from subprocess import check_output
sns.set(style="whitegrid", palette="muted")
data_dia = y
data = x
data_n_2 = (data - data.mean()) / (data.std()) # standardization
data = pd.concat([y,data_n_2.iloc[:,0:10]],axis=1)
data = pd.melt(data,id_vars="diagnosis",
var_name="features",
value_name='value')
plt.figure(figsize=(10,10))
tic = time.time()
sns.swarmplot(x="features", y="value", hue="diagnosis", data=data)
plt.xticks(rotation=90)
Correlation Map
f,ax = plt.subplots(figsize=(18, 18))
sns.heatmap(x.corr(), annot=True, linewidths=.5, fmt= '.1f',ax=ax)
fig, (axis1,axis2,axis3) = plt.subplots(1,3,figsize=(14,12))
sns.boxplot(x="features", y="value", hue="diagnosis", data = data, ax = axis1)
sns.violinplot(x="features", y="value", hue="diagnosis", data = data, split = True, ax = axis2)
sns.boxplot(x="features", y="value", hue="diagnosis", data = data, ax = axis3)
fig, saxis = plt.subplots(2, 3,figsize=(16,12))
sns.barplot(x="features", y="value", data=data, ax = saxis[0,0])
sns.barplot(x="features", y="value", order=[1,2,3], data=data, ax = saxis[0,1])
sns.barplot(x="features", y="value", order=[1,0], data=data, ax = saxis[0,2])
sns.pointplot(x="features", y="value", data=data, ax = saxis[1,0])
sns.pointplot(x="features", y="value", data=data, ax = saxis[1,1])
sns.pointplot(x="features", y="value", data=data, ax = saxis[1,2])
Comments
Post a Comment