Seaborn Barplot

Seaborn Barplot

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
In [2]:
#help(sns.set_context)
In [3]:
titanic = sns.load_dataset('titanic')
print(titanic.head())
   survived  pclass     sex   age  sibsp  parch     fare embarked  class  \
0         0       3    male  22.0      1      0   7.2500        S  Third   
1         1       1  female  38.0      1      0  71.2833        C  First   
2         1       3  female  26.0      0      0   7.9250        S  Third   
3         1       1  female  35.0      1      0  53.1000        S  First   
4         0       3    male  35.0      0      0   8.0500        S  Third   

     who  adult_male deck  embark_town alive  alone  
0    man        True  NaN  Southampton    no  False  
1  woman       False    C    Cherbourg   yes  False  
2  woman       False  NaN  Southampton   yes   True  
3  woman       False    C  Southampton   yes  False  
4    man        True  NaN  Southampton    no   True  
In [4]:
sns.barplot(x = titanic['sex'], y = titanic['survived'] )
plt.show()
In [5]:
sns.barplot(x = 'sex', y = 'survived', data = titanic )
plt.show()
In [6]:
sns.barplot(x = titanic.sex, y = titanic.survived )
plt.show()
In [7]:
sns.barplot(x = 'sex', y = 'survived', hue = 'class', data = titanic)
plt.show()
In [8]:
sns.barplot(x = 'sex', y = 'survived', hue = 'class', data = titanic,
            palette = 'hls',
            order = ['male', 'female'],  
            capsize = 0.05,             
            saturation = 8,             
            errcolor = 'gray', errwidth = 2
            )
plt.show()
In [9]:
kwargs = {'alpha':0.9, 'linestyle':':', 'linewidth':5, 'edgecolor':'k'}
 
sns.barplot(x = 'sex', y = 'survived', hue = 'class', data = titanic, **kwargs)
plt.show()
In [10]:
sns.set()
plt.figure(figsize = (8,6))
 
sns.barplot(x = 'sex', y = 'survived', hue = 'class', data = titanic, alpha =1, linestyle = "-.", linewidth = 3, edgecolor = "k")
 
plt.title("Barplot of Titanic Gender and Survived", fontsize = 20)
plt.xlabel("Gender", fontsize = 15)
plt.ylabel("Survived", fontsize = 15)
plt.savefig("output/barplot.png")
plt.show()
In [11]:
sns.set_context('paper')
crashes = sns.load_dataset('car_crashes').sort_values('total', ascending = False)
crashes.head()
Out[11]:
total speeding alcohol not_distracted no_previous ins_premium ins_losses abbrev
40 23.9 9.082 9.799 22.944 19.359 858.97 116.29 SC
34 23.9 5.497 10.038 23.661 20.554 688.75 109.72 ND
48 23.8 8.092 6.664 23.086 20.706 992.61 152.56 WV
3 22.4 4.032 5.824 21.056 21.280 827.34 142.39 AR
17 21.4 4.066 4.922 16.692 16.264 872.51 137.13 KY
In [12]:
f, ax = plt.subplots(figsize = (6,15))
sns.set_color_codes('pastel')
sns.barplot(x = 'total', y = 'abbrev', data = crashes,
            label = 'Total', color = 'b', edgecolor = 'w')
plt.show()
In [13]:
sns.set_color_codes('muted')
sns.barplot(x = 'alcohol', y = 'abbrev', data = crashes,
            label = 'Alcohol-involved', color = 'b', edgecolor = 'w')
ax.legend(ncol = 2, loc = 'lower right')
sns.despine(left = True, bottom = True)
plt.show()
In [14]:
sns.set_context('paper')
tips = sns.load_dataset('tips')
sns.barplot(x = 'day', y = 'total_bill', hue = 'sex', data = tips,
            palette = 'Blues', edgecolor = 'w')
tips.groupby(['day','sex']).mean()
plt.show()
In [15]:
sns.set_context('paper')
# load dataset
titanic = sns.load_dataset('titanic')
print(titanic.head())
   survived  pclass     sex   age  sibsp  parch     fare embarked  class  \
0         0       3    male  22.0      1      0   7.2500        S  Third   
1         1       1  female  38.0      1      0  71.2833        C  First   
2         1       3  female  26.0      0      0   7.9250        S  Third   
3         1       1  female  35.0      1      0  53.1000        S  First   
4         0       3    male  35.0      0      0   8.0500        S  Third   

     who  adult_male deck  embark_town alive  alone  
0    man        True  NaN  Southampton    no  False  
1  woman       False    C    Cherbourg   yes  False  
2  woman       False  NaN  Southampton   yes   True  
3  woman       False    C  Southampton   yes  False  
4    man        True  NaN  Southampton    no   True  
In [16]:
# create plot
sns.countplot(x = 'class', hue = 'who', data = titanic, palette = 'magma')
plt.title('Survivors')
plt.show()

Machine Learning

  1. Deal Banking Marketing Campaign Dataset With Machine Learning

TensorFlow

  1. Difference Between Scalar, Vector, Matrix and Tensor
  2. TensorFlow Deep Learning Model With IRIS Dataset
  3. Sequence to Sequence Learning With Neural Networks To Perform Number Addition
  4. Image Classification Model MobileNet V2 from TensorFlow Hub
  5. Step by Step Intent Recognition With BERT
  6. Sentiment Analysis for Hotel Reviews With NLTK and Keras
  7. Simple Sequence Prediction With LSTM
  8. Image Classification With ResNet50 Model
  9. Predict Amazon Inc Stock Price with Machine Learning
  10. Predict Diabetes With Machine Learning Algorithms
  11. TensorFlow Build Custom Convolutional Neural Network With MNIST Dataset
  12. Deal Banking Marketing Campaign Dataset With Machine Learning

PySpark

  1. How to Parallelize and Distribute Collection in PySpark
  2. Role of StringIndexer and Pipelines in PySpark ML Feature - Part 1
  3. Role of OneHotEncoder and Pipelines in PySpark ML Feature - Part 2
  4. Feature Transformer VectorAssembler in PySpark ML Feature - Part 3
  5. Logistic Regression in PySpark (ML Feature) with Breast Cancer Data Set

PyTorch

  1. Build the Neural Network with PyTorch
  2. Image Classification with PyTorch
  3. Twitter Sentiment Classification In PyTorch
  4. Training an Image Classifier in Pytorch

Natural Language Processing

  1. Spelling Correction Of The Text Data In Natural Language Processing
  2. Handling Text For Machine Learning
  3. Extracting Text From PDF File in Python Using PyPDF2
  4. How to Collect Data Using Twitter API V2 For Natural Language Processing
  5. Converting Text to Features in Natural Language Processing
  6. Extract A Noun Phrase For A Sentence In Natural Language Processing