Numpy(2) : Make Array(2)

One dimensional array : Scala

import numpy as np

a = np.array([1,2,3], dtype=int)
b = np.array([1.1,2.2,3.3], dtype=float)
c = np.array([1,1,0], dtype=bool)

print(a)
print(a.dtype)

print(b)
print(b.dtype)

print(c)
print(c.dtype)
[1 2 3]
int64
[1.1 2.2 3.3]
float64
[ True  True False]
bool

You can create an array using np.array({list}, dtype={int/float/bool}). And it can be returned by .dype property. If you omit dtype={} parameter, it automatically recognize the type of list.


Two dimensional array : Matrix

import numpy as np

a = np.array([[1,2,3],[4,5,6],[7,8,9]])

print(a)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

The list for the multi dimensional array is composed of overlapped lists such as [[1,2,3],[4,5,6],[7,8,9]]. From that the


Multi dimensional array

import numpy as np

a = np.array([[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]])

print(a)
[[[ 1  2]
  [ 3  4]]

 [[ 5  6]
  [ 7  8]]

 [[ 9 10]
  [11 12]]]

The list for the multi dimensional array is composed of multi overlapped lists such as [[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]]. It is printed being seperated by Matrix .


Attribute of array

import numpy as np

a = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = np.array([[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]])

print(a.ndim)
print(a.shape)
print(a.dtype)
print()

print(b.ndim)
print(b.shape)
print(b.dtype)
2
(3, 3)
int64

3
(3, 2, 2)
int64

.ndim returns the number of dimension of array.
.shape returns the shape of array.
.dtype returns the data type of array.


좋은 웹페이지 즐겨찾기