[PYTHON] Studying Machine Learning ~ matplotlib ~

Studying Machine Learning ~ matplotlib ~

This time I studied matplotlib, so I will output it.

1. What is matplotlib?

An external library that visualizes data. Like numpy and pandas, it is not included in Python at first, but it is installed in Anaconda from the beginning.

2. What is good for visualizing data?

You can quickly find outliers.

For example, let's say you have statistics on the price of sweets. It would be strange if there was data on sweets for 10,000 yen each among sweets for around 100 yen each, right? It is very difficult to find the strange data instantly, but you can find it instantly by visualizing the data with a graph or the like.

Is there only one strange point here? (Laughs)

3. Basic usage

import

test.ipynb


#Import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

Let's do the above. Use% matplotlib inline only when using it with Jupyter Notebook. This time, only the function of pyplot is used, so only pyplot is imported.

Drawing a graph

test.ipynb


#Graph size setting(4×4)
plt.figure(figsize=(4,4))
#Creating a graph
plt.plot([1,2,3,4,5],[6,7,8,9,10],label='test')
#x-axis name
plt.xlabel('x axis')
#y-axis name
plt.ylabel('y axis')
#Graph title
plt.title('title')
#Reflect the above legend in the graph
plt.legend()
#Show graph
plt.show()

The basic function is above. Even if you do not describe plt.show (), the graph will be displayed, so please feel free to use it.

If you want to describe the graph of x squared, ...

test.ipynb


import numpy as np

#1~Substitute an array of 10 into x
x = np.arange(0,11,1)
#Make the value of y the square of x
y = x ** 2
plt.plot(x,y,label='y = x^2')
#x-axis name
plt.xlabel('x axis')
#y-axis name
plt.ylabel('y axis')
#Graph title
plt.title('y = x^2')
#Reflect the above legend in the graph
plt.legend()
#Show graph
plt.show()

You can do it with this.

Display multiple graphs.

The following describes how to display multiple graphs.

test.ipynb


#Create a graph with a total size of 8 x 8 in 2 rows and 2 columns
fig,ax = plt.subplots(2,2,figsize=(8,8))
#x,y,Set the z range
x = np.arange(0,11,1)
y = x ** 2
z = x ** 3
#Creating a graph
ax[0,0].plot(x,x,label='x = x',color='red')
ax[0,1].plot(x,y,label='y = x^2'),color='green')
ax[1,0].plot(x,z,label='z = x^3'),color='blue')

#Graph settings
for i in range(2):
  for j in range(2):
    ax[i,j].set_xlabel('x axis')
    ax[i,j].set_ylabel('y axis')
    ax[i,j].legend()

plt.tight_layout()

Create a graph of 8 sizes vertically and horizontally in the entire size in fig Four graphs of two sizes, vertical and horizontal, are created in it.

The tight_layout function is a function that prevents graphs from overlapping each other.

Creating other graphs

Until now, it was only a bar graph, but of course you can create other graphs.

test.ipynb


#pie chart
plt.pie()
#histogram
plt.hist()
#Scatter plot
plt.scatter()

Arguments are omitted, but pie charts and histograms show the ratio and transition of each data. The scatter plot is used to check the set data, so if you are interested, please check it out!

4. Summary

Machine learning I've been here so far, but it's fun! I haven't spoken a language that allows me to do various things like this, so it's nice to be able to do many things. I'll post it in the output tomorrow and beyond, but don't miss it ...

that's all

Recommended Posts

Studying Machine Learning ~ matplotlib ~
Machine learning
[Machine learning] Try studying decision trees
[Machine learning] Try studying random forest
[Memo] Machine learning
Machine learning classification
Machine Learning sample
Machine learning tutorial summary
About machine learning overfitting
Machine Learning: Supervised --AdaBoost
Machine learning logistic regression
Machine learning support vector machine
Studying Machine Learning-Pandas Edition-
Machine learning linear regression
Machine learning course memo
Machine learning library dlib
Machine learning (TensorFlow) + Lotto 6
Somehow learn machine learning
Machine learning library Shogun
Machine learning rabbit challenge
Introduction to machine learning
Machine Learning: k-Nearest Neighbors
What is machine learning?
Machine learning model considering maintainability
Machine learning learned with Pokemon
Data set for machine learning
Machine learning in Delemas (practice)
An introduction to machine learning
Machine learning / classification related techniques
Machine Learning: Supervised --Linear Regression
Basics of Machine Learning (Notes)
Machine learning beginners tried RBM
[Machine learning] Understanding random forest
Machine learning with Python! Preparation
Machine Learning Study Resource Notepad
Machine learning ② Naive Bayes Summary
Understand machine learning ~ ridge regression ~.
Machine learning article summary (self-authored)
About machine learning mixed matrices
Machine Learning: Supervised --Random Forest
Practical machine learning system memo
Machine learning Minesweeper with PyTorch
Machine learning environment construction macbook 2021
Build a machine learning environment
Python Machine Learning Programming> Keywords
Machine learning algorithm (simple perceptron)
Used in machine learning EDA
Importance of machine learning datasets
Machine learning and mathematical optimization
Machine Learning: Supervised --Support Vector Machine
Supervised machine learning (classification / regression)
I implemented Extreme learning machine
Beginning with Python machine learning
Machine learning algorithm (support vector machine)
Super introduction to machine learning
4 [/] Four Arithmetic by Machine Learning
Machine learning ④ K-nearest neighbor Summary
Pokemon machine learning Nth decoction
Try machine learning with Kaggle
Machine learning stacking template (regression)
Machine Learning: Supervised --Decision Tree