Python #Numpy basics

Learning memo / memorandum

About Numpy

A typical library that has an abundant mathematical calculation library and can perform efficient numerical calculations.

ndarray type

The basic data type used by Numpy in a typical numerical calculation package.

Description example
# Creating ndarray type
import numpy

a = [0, 1, 2, 3]
 b = numpy.array (a) # Create ndarray type from list
print('b = ', b)

c = [1, 2]
d = [2, 3]
e = [3, 4]
 f = numpy.array ([c, d, e]) # Create 2D ndarray type
print('f = ', f)

 g = numpy.array (a, dtype = numpy.float16) #Create ndarray by specifying data type
print('g = ', g)

Execution result

b = [0 1 2 3] f = [[1 2] [2 3] [3 4]] g = [0. 1. 2. 3.]

Methods for performing basic numerical operations

Various numerical operations can be performed on ndarray type data and lists by using the methods provided by Numpy. The representative ones are introduced below.

import numpy

h = numpy.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

 i = numpy.median (h) # median
 j = numpy.mean (h) #mean
 k = numpy.std (h) # standard deviation
 l = numpy.var (h) # Distributed

print(i)
print(j)
print(k)
print(l)

Execution result

4.5 4.5 2.8722813232690143 8.25

Innumerable and infinite

Numpy allows you to use the non-numeric nan to indicate non-numeric and the ʻinf` to indicate infinity.

Non-numeric nan

nan is the value that appears when you divide 0 by 0. Since it is a special value that returns False when compared to any value, it also returns False when compared to itself. Therefore, the identity with nan can be confirmed by using ʻis`.

from numpy import nan

m = float32(0) / float32(0)
n = m == nan
o = nan == nan
p = m is nan

print(m)
print(n)
print(o)
print(p)

Execution result

nan False False True

Infinity value ʻinf`

ʻInf is a value that appears by dividing a value with an absolute value by 0. Both comparisons using == and comparisons using ʻis return True.

from numpy import inf

q = float(10) / float(0)
r = q == inf
s = q is inf

print(q)
print(r)
print(s)

Execution result

inf True True

Difference from the list you want to pay attention to

Note that lists and ndarray types are both typical data types that handle arrays, and although their usage is similar, there are some differences.

Use of operators

The + operator means joining lists between lists and adding values between ndarrays. The + operation with an integer value will result in an error in the case of a list, but in ndarray it represents the addition of integer values. The * operator causes an error between lists, but represents multiplication of values between ndarrays. The * operation with an integer value represents the repetition of the list in the case of a list, and represents the multiplication of an integer value in the ndarray.

Operators in lists
t = [0, 1, 2]
u = [3, 4, 5]
print(t + u)
# print (t + 2) error
# print (t * u) error
print(t * 2)

Execution result

[0, 1, 2, 3, 4, 5] [0, 1, 2, 0, 1, 2]

Operators in ndarray
import numpy

v = numpy.array([0, 1, 2])
w = numpy.array([3, 4, 5])
print(v + w)
print(v + 10)
print(v * w)
print(v * 2)

Execution result

[3 5 7] [10 11 12] [ 0 4 10] [0 2 4]

Multidimensional array

Unlike a list, ndarray cannot create data with a different number of elements in each dimension. That is, all the data created by the ndarray type must be a multidimensional array with the same number of rows and columns. If you try to create a two-dimensional array from a list of different lengths, the list and ndarray behave differently as follows.

import numpy

x = [[0, 1], [2, 3, 4], [5, 6]]
y = numpy.array([[0, 1], [2, 3, 4], [5, 6]])

print(x)
print(y)

Execution result

[[0, 1], [2, 3, 4], [5, 6]] [list([0, 1]) list([2, 3, 4]) list([5, 6])]

Index specification / slice specification

You can use , and : to extract elements of a list or ndarray.

import numpy

z = numpy.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])

 print (z [0, 2]) # Index 2 in Index 0
 print (z [2, 1:]) # Index 1 and later in Index 2
 print (z [:, 2]) # Index 2 of all indexes

Execution result

2 [7 8] [2 5 8]

Recommended Posts

#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
Python #Numpy basics
Python basics 8 numpy test
Python basics ⑤
Python basics
NumPy basics
Python basics ④
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
My Numpy (Python)
Python basics: list
Python basics memorandum
Python CGI basics
Python basics: dictionary
Basics of Python ①
Basics of python ①
Python slice basics
#Python basics (scope)
#Python basics (functions)
Python basics: functions
#Python basics (class)
Python basics summary
[Python] Numpy memo
Python and numpy tips
Python basics ② for statement
Python: Unsupervised Learning: Basics
Errbot: Python chatbot basics
[Python] Search (NumPy) ABC165C
python numpy array calculation
#Python DeepLearning Basics (Mathematics 1/4)
Python basics: Socket, Dnspython
# 4 [python] Basics of functions
[Python] Sorting Numpy data
Basics of python: Output
Python Basic --Pandas, Numpy-
Python / Numpy np.newaxis thinking tips
Convert numpy int64 to python int
Python
Implemented SMO with Python + NumPy
Matrix multiplication in python numpy
Create a python numpy array
Python basics: conditions and iterations
Paiza Python Primer 4: List Basics
[Python] Numpy reference, extraction, combination
Python3 | Getting Started with numpy
Basics of Python × GIS (Part 1)
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
SNS Python basics made with Flask
Subscript access to python numpy array
Introduction to Python Numerical Library NumPy
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
Put python, numpy, opencv3 in ubuntu14
Getting Started with Python Basics of Python
Python application: Numpy Part 3: Double array