Try matrix operation with NumPy

Iwate Prefectural University Advent Calendar Day 17! Thank you for your hard work, Jack (@kazuhikoyamashita). My recent boom is Python. This time, I will try matrix operation using numpy.

What is NumPy

NumPy is an extension module for performing numerical calculations. Please see the link below for details. http://www.numpy.org/

Let's install pip

You need the pip command to install numpy. To install the pip command, use easy_install. Please enter the following command.

% easy_install pip 

Install numpy with pip command

To install numpy, run the following command. The version of numpy installed this time is 1.10.2.

% pip install numpy
Collecting numpy
  Downloading numpy-1.10.2-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.7MB)
    100% |████████████████████████████████| 3.7MB 133kB/s 
Installing collected packages: numpy
Successfully installed numpy-1.10.2

Try using

Prepare the matrices m1 and m2, and find the sum-difference product quotient and the inverse matrix of m1. The program is as follows.

# -*- coding: utf-8 -*-
import numpy as np
from numpy import linalg as la

m1 = np.matrix('1 2; 3 4')
m2 = np.matrix('5 6; 7 8')

#sum
print("####sum")
print(m1 + m2)

#difference
print("####difference")
print(m1 - m2)

#Product of elements
print("####Product of elements")
print(m1 * m2)

#Merchant between elements
print("####Merchant between elements")
print(m1 / m2)

#Inverse matrix of m1
print("####Inverse matrix of m1")
print(la.inv(m1))

The output result is as follows. It's convenient because the inverse matrix can be calculated easily.

% python sample.py
####sum
[[ 6  8]
 [10 12]]
####difference
[[-4 -4]
 [-4 -4]]
####Product of elements
[[19 22]
 [43 50]]
####Merchant between elements
[[ 0.2         0.33333333]
 [ 0.42857143  0.5       ]]
####Inverse matrix of m1
[[-2.   1. ]
 [ 1.5 -0.5]]

that's all!

Recommended Posts

Try matrix operation with NumPy
Matrix concatenation with Numpy
[Python] Matrix operation
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Numpy leave? !! Partial differential of matrix with Sympy
Try scraping with Python.
Moving average with numpy
Try SNN with BindsNET
Getting Started with Numpy
Learn with Cheminformatics NumPy
Hamming code with numpy
Regression analysis with NumPy
Try regression with TensorFlow
Extend NumPy with Rust
Let's transpose the matrix with numpy and multiply the matrices.
I tried Smith standardizing an integer matrix with Numpy
Kernel regression with Numpy only
Try to factorial with recursion
Try function optimization with Optuna
I wrote GP with numpy
CNN implementation with just numpy
Artificial data generation with numpy
Try using PythonTex with Texpad.
I made an N-dimensional matrix operation library Matft with Swift
Try edge detection with OpenCV
Try implementing RBM with chainer.
Try Google Mock with C
Try programming with a shell!
Try an autoencoder with Pytorch
[Python] Calculation method with numpy
Try Python output with Haxe 3.2
S3 operation with python boto3
Diffusion equation animation with NumPy
Debt repayment simulation with numpy
Try implementing XOR with PyTorch
Try running CNN with ChainerRL
Implemented SMO with Python + NumPy
Try various things with PhantomJS
Stick strings together with Numpy
Try Deep Learning with FPGA
Matrix multiplication in python numpy
Try to automate the operation of network devices with Python
Try running Python with Try Jupyter
Try implementing perfume with Go
Handle numpy arrays with f2py
Use OpenBLAS with numpy, scipy
Try Selenium Grid with Docker
Try face recognition with Python
Try OpenCV with Google Colaboratory
Try machine learning with Kaggle
Python3 | Getting Started with numpy
Try TensorFlow MNIST with RNN
Try building JupyterHub with Docker
Try using folium with anaconda
Implementing logistic regression with NumPy
Performance comparison between 2D matrix calculation and for with numpy
Cosine similarity matrix? You can get it right away with NumPy
Visualization of matrix created by numpy
Python-Mouse and keyboard operation with pyautogui
Try scraping with Python + Beautiful Soup
Perform least squares fitting with numpy.