Comments

Numpy Tutorial_1


Creating NumPy Arrays (Tutorial: 1 )

python using list of data type to store collection of data in list it use different of data type in array it use the same data type disadvantage of list it computatinal consuming and cannot be use for large number of data

# to create a list:
L = [0, 1, "inas", "ML", True]
print(L)
[0, 1, 'inas', 'ML', True]

Numpy is the solution of storing large number of data due to :

  • efficient in computational.
  • can store array of high dimension.
  • all the element of Numpy is the same type (ndarray)
  • fixed size items

to create Numpy:

# import import numpy as np N = np.arange(10) print("the array of numpy is:",N,"The type is :",type(N),"The shape is :", N.shape) # when the shape output is (10,) we think as it matrix 10 x 1
the array of numpy is: [0 1 2 3 4 5 6 7 8 9] The type is : <class 'numpy.ndarray'> The shape is : (10,)
# to create numpy array between 0-10 and make increment equal to 2 :
N = np.arange(0,10,2)
print(N)
[0 2 4 6 8]
# to create numpy array equal to 0:
N = np.zeros(5)
print(N)
[0. 0. 0. 0. 0.]
# to create numpy array with two dimentional row = 2, column = 3 with zero as value
N = np.zeros((2,3))
print(N)
[[0. 0. 0.]
 [0. 0. 0.]]
# to create numpy array with two dimentional row = 3 , column = 5 with any value 
N = np.full((3,5),8)
print(N)
[[8 8 8 8 8]
 [8 8 8 8 8]
 [8 8 8 8 8]]
# ro create identity matrix , where the dignoal equal to one, else zero and row = column
N = np.eye(10)
print(N)
[[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
# to create randam numpy with value between interval [0.0 - 1.0] ,3
N = np.random.random((2,3))
print(N)
[[0.37051344 0.7424673  0.83775036]
 [0.73445116 0.35260695 0.65050845]]
# create Numpy from list
L = [0, 1, 100, 42, 13, 7]
print(type(L))
N = np.array(L)
print(type(N))
<class 'list'>
<class 'numpy.ndarray'>
 
 
Share on Google Plus

About Inas AL-Kamachy

    Blogger Comment
    Facebook Comment

0 Comments:

Post a Comment