[PYTHON] Seaborn basics for beginners ④ pairplot

What is seaborn

A library for drawing Python graphs. .. Positioned as a wrapper function (inclusive program) of matplotlib, the most famous libra. In addition to being able to easily draw beautiful-looking graphs, it also has a certain amount of functions such as batch processing. Matplotlib is for detailed specification and drawing, and seaborn is for easy and beautiful.

Pair plot (jointplot)

The theme this time is pair plot. Isn't it the most famous function in seaborn? Use .pairplot to create it. It is used to grasp the correlation of data.

Preparation

First, install the seaborn library with pip. For pip ?, click here ('https://qiita.com/Yanagawa_Yoshihisa/items/35e6f70a8411277282ce').

Import the library. Name seaborn`` sns and ʻimport`.

python


import seaborn as sns

I will try the sample with Titanic data. If you don't know Titanic, please check "kaggle Titanic". Create a dataframe with pandas.

python


dataframe = pd.read_csv('train.csv')

Basic creation

Use .jointplot to create a scatter plot. Basically, what you set is the original data and the axis you want to plot. Here, select Age (age), Fare (fare), and Pclass (grade) as the axes to be plotted, and set them to vars.

python


sns.pairplot(dataframe, vars = ['Age','Fare','Pclass'])

I was able to draw a graph like that.

image.png

As for how to read the graph, an n × n matrix of items with axes is created. The intersections of the same axes (areas in the blue frame) are a matrix of individual items. The other area is a scatter plot of the axes of interest. The scatter plots at diagonal positions are related to the scatter plots with their axes swapped. (The red frame is the same scatter plot, but the X-axis and Y-axis are interchanged.) With this function, you can get a rough idea of the overall picture of the item. (Looking at the fare and age in the red frame, there seems to be no easy-to-understand correlation between age and fare.)

The hue option allows you to set the Z axis. Add Sex as an example.

python


sns.pairplot(dataframe, vars = ['Age','Fare','Pclass'], hue = 'Sex')

image.png

You can also change to a histogram with diag_kind =" hist ".

python


sns.pairplot(dataframe, vars = ['Age','Fare','Pclass'], hue = 'Sex' ,diag_kind="hist")

image.png

It's a very simple syntax, but it's recommended for those who aren't familiar with it because it looks like it and gives a feeling of doing it.

You can specify various other options, so if you want to dig deeper, please see the Official Document.

Finally

As a beginner can understand, we have summarized the necessary knowledge when implementing machine learning with Python as a simple article. The table of contents is here, so I hope you can refer to other articles as well.

Recommended Posts

Seaborn basics for beginners ④ pairplot
Seaborn basics for beginners ② Histogram (distplot)
Seaborn basics for beginners ③ Scatter plot (jointplot) * With histogram
Pandas basics for beginners ① Reading & processing
Pandas basics for beginners ⑧ Digit processing
Pandas basics summary link for beginners
Roadmap for beginners
Seaborn basics for beginners ① Aggregate graph of the number of data (Countplot)
Pandas basics for beginners ③ Histogram creation with matplotlib
Basics of pandas for beginners ② Understanding data overview
Spacemacs settings (for beginners)
Python basics ② for statement
python textbook for beginners
Dijkstra algorithm for beginners
OpenCV for Python beginners
[Translation] NumPy Official Tutorial "NumPy: the absolute basics for beginners"
[Linux] Basics of authority setting by chmod for beginners
[For beginners] Basics of Python explained by Java Gold Part 2
Learning flow for Python beginners
[For beginners] kaggle exercise (merucari)
Linux distribution recommended for beginners
CNN (1) for image classification (for beginners)
Python3 environment construction (for beginners)
Overview of Docker (for beginners)
Python #function 2 for super beginners
Basic Python grammar for beginners
Pandas basics for beginners ④ Handling of date and time items
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Python #list for super beginners
~ Tips for beginners to Python ③ ~
[For Kaggle beginners] Titanic (LightGBM)
Reference resource summary (for beginners)
[For beginners] Basics of Python explained by Java Gold Part 1
Linux command memorandum [for beginners]
Convenient Linux shortcuts (for beginners)
[Explanation for beginners] TensorFlow tutorial MNIST (for beginners)
Decision tree (for beginners) -Code edition-
Python Exercise for Beginners # 2 [for Statement / While Statement]
XPath Basics (3) -Functions often used for XPath
Python for super beginners Python # dictionary type 1 for super beginners
[For beginners] Django -Development environment construction-
[For beginners] Script within 10 lines (1.folium)
Logistic Regression (for beginners) -Code Edition-
What is scraping? [Summary for beginners]
Python #index for super beginners, slices
<For beginners> python library <For machine learning>
TensorFlow Tutorial MNIST For ML Beginners
Frequently used Linux commands (for beginners)
Python #len function for super beginners
Beginners use Python for web scraping (1)
Run unittests in Python (for beginners)
What is xg boost (1) (for beginners)
Beginners use Python for web scraping (4) ―― 1
Python #Hello World for super beginners
Linear regression (for beginners) -Code edition-
Python for super beginners Python # dictionary type 2 for super beginners
[For beginners] Process monitoring using cron
LSTM (1) for time series forecasting (for beginners)
[Deprecated] Chainer v1.24.0 Tutorial for beginners
TensorFlow Tutorial -MNIST For ML Beginners