Learning memo / memorandum
A typical library that has an abundant mathematical calculation library and can perform efficient numerical calculations.
The basic data type used by Numpy
in a typical numerical calculation package.
# 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.]
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
Numpy allows you to use the non-numeric nan
to indicate non-numeric and the ʻinf` to indicate infinity.
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
ʻ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
Note that lists and ndarray
types are both typical data types that handle arrays, and although their usage is similar, there are some differences.
The +
operator means joining lists between lists and adding values between ndarray
s. 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 ndarray
s. 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
.
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]
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]
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])]
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