Numpy example

Numpy hands on examples to be familiar with numpy behavior.

Creating numpy.ndarray

numpy.ndarray can be created by following methods

  • array
  • asarray
  • empty
  • zeros
  • ones

Attributes

numpy has following attributes (showing only major attributes)

  • dtype
  • shape
  • ndim
  • size

In [23]:
import numpy as np  # numpy is often imported as np for short terminology


def print_numpy_attributes(a):
    """dumps ``numpy.ndarray`` attribute information 
    
    :param a: 
    :type a: np.ndarray 
    :return: 
    """
    print('----- numpy attributes info -----')
    print('', a)  # data will be printed. this is same with a.data
    print('type', type(a))  # should be <class 'numpy.ndarray'>
    print('data', a.data)   # actual array data
    print('dtype', a.dtype)  # type of data (int, float32 etc)
    print('shape', a.shape)  # dimensional information of data (2, 3) etc.
    print('ndim', a.ndim)  # total dimension of shape. 0 means scalar, 1 is vector, 2 is matrix... 
    print('size', a.size)  # total size of data, which is product sum of shape
    print('---------------------------------')

In [24]:
# 1. creating scalar
a1 = np.array(3)  # note that np.array([3]) will create vector with 1 element
print_numpy_attributes(a1)


----- numpy attributes info -----
 3
type <class 'numpy.ndarray'>
data <memory at 0x0000019ABAB15D68>
dtype int32
shape ()
ndim 0
size 1
---------------------------------

In [25]:
# 2. creating vector
l2 = [1, 2, 3]
a2 = np.array(l2)
print(l2, type(l2))         # l2 is list
print_numpy_attributes(a2)  # a2 is numpy.ndarray


[1, 2, 3] <class 'list'>
----- numpy attributes info -----
 [1 2 3]
type <class 'numpy.ndarray'>
data <memory at 0x0000019ABAFD1AC8>
dtype int32
shape (3,)
ndim 1
size 3
---------------------------------

In [26]:
# 3. creating matrix
l3 = [[1, 2, 3], [4, 5, 6]]
a3 = np.array(l3)
# print(l3, type(l3))
print_numpy_attributes(a3)


----- numpy attributes info -----
 [[1 2 3]
 [4 5 6]]
type <class 'numpy.ndarray'>
data <memory at 0x0000019ABAB2B1F8>
dtype int32
shape (2, 3)
ndim 2
size 6
---------------------------------

In [27]:
# 4. creating general multi-dimensional array (tensor)
a4 = np.array([
    [[1, 2, 3], [4, 5, 6]],
    [[11, 22, 33], [44, 55, 66]]
    ]
)
print_numpy_attributes(a4)


----- numpy attributes info -----
 [[[ 1  2  3]
  [ 4  5  6]]

 [[11 22 33]
  [44 55 66]]]
type <class 'numpy.ndarray'>
data <memory at 0x0000019ABAFE2138>
dtype int32
shape (2, 2, 3)
ndim 3
size 12
---------------------------------

In [ ]: