Analyze Data/Python Libraries

numpy-array, arange, reshape, slicing, newaxis, ...(Ellipsis)

Naranjito 2021. 5. 25. 16:20
  • array

 

Create an array.


  • dtype="object"
a_object = np.array([1, 0.1, 'one'], dtype=object)
print(a_object)
print(a_object.dtype)

>>> [1 0.1 'one']
>>> object

print(type(a_object[0]))
print(type(a_object[1]))
print(type(a_object[2]))
>>> <class 'int'>
>>> <class 'float'>
>>> <class 'str'>

 

It stores pointers to Python objects. Since the data of each element itself is stored in each memory area, it can have multiple data types (pointers to) in one array.

 

  • arange

Return evenly spaced values within a given interval with array. Values are generated within the half-open interval (start, stop) in other words, the interval including start but excluding stop.

np.arange(9)
>>>array([0, 1, 2, 3, 4, 5, 6, 7, 8])

np.arange(0,10,3)
>>>array([0, 3, 6, 9])

 

  • reshape

Give a new shape to an array without changing its data.

arange(number of multiply reshape)

np.array(np.arange(20)).reshape((2,10))
>>>array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]])

 

reshape(row:column) -->reshape(-1:num) 

If -1 is put in the row position and specify the column, the number of rows to be converted automatically.

x = np.arange(12).reshape(-1,1)

>>>x

array([[ 0],
       [ 1],
       [ 2],
       [ 3],
       [ 4],
       [ 5],
       [ 6],
       [ 7],
       [ 8],
       [ 9],
       [10],
       [11]])
       
y = np.arange(12).reshape(-1,2)

>>>y

array([[ 0,  1],
       [ 2,  3],
       [ 4,  5],
       [ 6,  7],
       [ 8,  9],
       [10, 11]])

 

reshape(-1) returns 1-d.

np.array([[1,2,3],[4,5,6]]).reshape(-1)

>>>

array([1, 2, 3, 4, 5, 6])

 

It is equivalent to below.

np.array([[1,2,3],[4,5,6]]).ravel()

>>>

array([1, 2, 3, 4, 5, 6])

 

reshape(overall rows, inside rows, columns)

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

>>>

array([[[ 1,  2],
        [ 3,  4],
        [ 5,  6]],

       [[ 7,  8],
        [ 9, 10],
        [11, 12]]])

 

  • slicing

a=np.array([[1,2,3],[4,5,6]])
b=a[0:2,1:2]
b
>>>array([[2],
       [5]])
       
b=a[1,:2]
b
>>>array([4, 5])

b=a[:,1]
b
>>>array([2, 5])

 

[start:stop:step]

py_str='python'
print(py_str[1:6:2])
>>>
yhn

string = "홀짝홀짝홀짝"
print(string[::2])
>>>
홀홀홀

string = "PYTHON"
print(string[::-1])
>>>
NOHTYP

 

In a 3D array,

 

[:2, 1:, :2]

This selects :

- planes :2 (the first 2 planes)

- rows :1 (the last 2 rows)

- columns :2 (the first 2 columns)

 

As shown here below.

https://www.pythoninformer.com/python-libraries/numpy/index-and-slice/

 

 

Reassign the column.

0	1	2	3	4	mean
0	0.621096	0.904698	0.629040	0.109003	0.648523	0.325452

cols=df.columns.tolist()
cols
>>>
[0, 1, 2, 3, 4, 'mean']

cols=cols[-1:]+cols[:-1]

df=df[cols]
df
>>>
mean	0	1	2	3	4
0	0.325452	0.621096	0.904698	0.629040	0.109003	0.648523

 

  • newaxis

Increase the dimension of one of the arrays.

Inside [] adds a new dimension of size 1 at that position.

x1=np.array([1,2,3,4,5])
x1.shape

>>>
(5,)

x2=np.array([5,4,3])
x2.shape

>>>
(3,)

x1_new=x1[:,np.newaxis]
x1_new.shape

>>>
(5, 1)

x1_new+x2

>>>
array([[ 6,  5,  4],
       [ 7,  6,  5],
       [ 8,  7,  6],
       [ 9,  8,  7],
       [10,  9,  8]])
a

>>>
array([[0, 1, 2],
       [3, 4, 5]])
       
a[:,:,np.newaxis]

>>>
array([[[0],
        [1],
        [2]],

       [[3],
        [4],
        [5]]])

 

  • ...(Ellipsis)

Add a new dimension to the last dimension, which has many dimensions, it is easier to use ....

a

>>>
array([[0, 1, 2],
       [3, 4, 5]])
       
a[...,np.newaxis]

>>>
array([[[0],
        [1],
        [2]],

       [[3],
        [4],
        [5]]])

 

newaxis, Ellipsis : https://note.nkmk.me/en/python-numpy-newaxis/