NumPy - 1

import numpy as np
mylist = list([1,2,3,4])
mylist
[1, 2, 3, 4]
myarray = np.array([1,2,3,4])
myarray
array([1, 2, 3, 4])
new = myarray/2
new
array([0.5, 1. , 1.5, 2. ])
new_list = mylist/2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-182a7d153636> in <module>
----> 1 new_list = mylist/2

TypeError: unsupported operand type(s) for /: 'list' and 'int'
type(myarray)
numpy.ndarray
type(mylist)
list
# Creating an array
import numpy as np
myarray = np.array([1,2,3,4,5,6])
print(myarray)
[1 2 3 4 5 6]
type(myarray)
numpy.ndarray
# 0-Dimensional array
myarray0 = np.array(40)
print(myarray0)
40
type(myarray0)
numpy.ndarray
# 1- Dimension array
myarray1 = np.array([1,2,3])
print(myarray1)
[1 2 3]
# 2- Dimension array
myarray2 = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(myarray2)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
# 3- Dimensional array
myarray3 = np.array([[[1,2],[4,5]],[[1,2],[2,5]]])
print(myarray3)
[[[1 2]
  [4 5]]

 [[1 2]
  [2 5]]]
type(myarray3)
numpy.ndarray
# Dimension of arrays
print(myarray3.ndim)
3
print(myarray2.ndim)
2
print(myarray1.ndim)
1
print(myarray0.ndim)
0
# Higher dimension

myarray = np.array([1,2,3,4,5],ndmin=5)
print(myarray)
[[[[[1 2 3 4 5]]]]]
## Accessing the elements

print(myarray1)
[1 2 3]
myarray1[1]
2
print(myarray2)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
print(myarray2[2, 1])
8
print(myarray1[-1])
3
print(myarray3)
[[[1 2]
  [4 5]]

 [[1 2]
  [2 5]]]
print(myarray3[1, 0, 1])
2
print(myarray3[0,0,1])
2
# Slicing [start:stop:step]
myarray1
array([1, 2, 3])
print(myarray1[1:])
[2 3]
print(myarray1[::-1])
[3 2 1]
print(myarray1[::2])
[1 3]
myarray2
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
print(myarray2[0:3,1])
[2 5 8]
print(myarray2[1: ,])
[[4 5 6]
 [7 8 9]]
print(myarray2[ : ,1:])
[[2 3]
 [5 6]
 [8 9]]
print(myarray2[1: , 1: ])
[[5 6]
 [8 9]]
## Special arrays
myarray = np.arange(10)
myarray
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
myarray = np.zeros(3)
print(myarray)
[0. 0. 0.]
myarray = np.zeros((2,3))
myarray
array([[0., 0., 0.],
       [0., 0., 0.]])
myarray = np.ones((2,3))
myarray
array([[1., 1., 1.],
       [1., 1., 1.]])
myarray = np.eye(3)
myarray
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
myarray = np.linspace(1,5,10)
myarray
array([1.        , 1.44444444, 1.88888889, 2.33333333, 2.77777778,
       3.22222222, 3.66666667, 4.11111111, 4.55555556, 5.        ])
myarray.ndim
1
myarray = np.linspace(1,10,10)
myarray
array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
myarray = np.linspace(1,10,6)
myarray
array([ 1. ,  2.8,  4.6,  6.4,  8.2, 10. ])
# reshaping
myarray = np.eye(4)
myarray
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
myarray.reshape((2,8))
array([[1., 0., 0., 0., 0., 1., 0., 0.],
       [0., 0., 1., 0., 0., 0., 0., 1.]])
myarray.reshape((2,9))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-116-858813a99177> in <module>
----> 1 myarray.reshape((2,9))

ValueError: cannot reshape array of size 16 into shape (2,9)
myarray.reshape(16)
array([1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1.])