Home Machine Learning 150+ Numpy python bundle follow workout routines

150+ Numpy python bundle follow workout routines

0
150+ Numpy python bundle follow workout routines

[ad_1]

Numpy workout routines offer you a very good publicity on studying the numpy as it’s a base for many of linear algebra calculations in python.

There is no such thing as a explicit order in these numpy workout routines, it’s principally primarily based what a standard information scientist makes use of in day after day functioning.

Numpy exercises
Numpy workout routines

1) The best way to import numpy in jupyter pocket book?

import numpy as np
# You should use any outlined variable as a substitute of np.

2) The best way to print numpy model?

np.__version__

3) The best way to print numpy configuration?

np.__config__

4) The best way to create a null vector of measurement 15?

np.zeros(15)

#Output
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

5) The best way to discover reminiscence measurement of any array?

#Create an array
X = np.zeros(15)
# Use measurement and itemsize operate, which returns reminiscence measurement in bytes
X.measurement * X.itemsize
#Output
120
Additionally See: 200+ Pandas workout routines in Python

6) The best way to create vector with values starting from 20 to 60?

np.arange(20,61)
array([20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
54, 55, 56, 57, 58, 59, 60])

Necessary: To get 60 as a price we now have to go until 61, as numpy follows python indexing which begins at zero.

7) The best way to reverse a vector in numpy?

X = np.arange(20)
X[::-1]

#Output - Bear in mind thought is to make final aspect first
array([19, 18, 17, 16, 15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3, 2,  1,  0])

8) The best way to create 4X4 with values from 0 to fifteen?

#Make a vector
X = np.arange(16)

#Use reshape operate to perform this
X.reshape(4,4)

#Or do it one line of code
np.arange(16).reshape(4,4)

#Output
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

9) The best way to discover indices for non-zero parts from array [0,3,0,0,4,0]?

#Create an array
X = np.array([0,3,0,0,4,0])

# Discover parts that are non-zero utilizing nonzero operate.
X.nonzero()

#Output
(array([1, 4], dtype=int64),)

10) The best way to create 5×5 id matrix?

np.eye(5)

#Output
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])

11) The best way to create 3X3X3 array with random values?

np.random.random((3,3,3))

#Output
array([[[0.9982351 , 0.66082219, 0.25639028],
        [0.19607748, 0.89347098, 0.45959688],
        [0.74053213, 0.53736123, 0.22473853]],

       [[0.80181724, 0.93279786, 0.55937109],
        [0.17398461, 0.78169959, 0.2020516 ],
        [0.50990114, 0.65455622, 0.80240091]],

       [[0.13396566, 0.25127465, 0.40984855],
        [0.28918461, 0.22093434, 0.09146537],
        [0.36363114, 0.0779843 , 0.38857124]]])

12) The best way to discover most values in an array?

X = np.array([1,13,0,56,71,22])

X.max()
#Output
71

13) The best way to discover minimal values in an array?

Taking the above array.
X.min()
#output
0

14) The best way to discover imply values in a numpy array?

Taking the above array.
X.imply()
#Output
27.166666666666668

15) The best way to discover commonplace deviation in a numpy array?

Taking the above array.
X.std()
#Output
27.088845592892206
Taking the above array.
np.median(X)
#Output
17.5

17) The best way to normalize a 6X6 random matrix?

There is no such thing as a inbuilt operate accessible in numpy for this, nonetheless thought is to make use of statistics after which apply it utilizing different inbuilt operate. One of many components to normalize any dataset is

X- imply(X)/std(X)

X = np.random.random((6,6))

#Making use of the components
X = (X-np.imply(X)/np.std(X))
#Output
X
array([[-0.85474855, -1.21354557, -1.2677121 , -0.75216064, -0.79639396,
        -0.93831933],[-1.31067505, -1.53903902, -0.63014541, -1.3649538 , -1.4812477 ,-1.36599474],
       [-1.02716266, -1.23143665, -1.23133983, -1.24246045, -0.74060478,
        -0.77004037],[-1.57277085, -1.15594137, -1.19967881, -0.68276788, -1.45151712,-0.93112109],
       [-1.50585333, -0.88228056, -1.60306409, -0.70004118, -1.32388217,
        -1.38337237],[-1.31703657, -1.3254611 , -0.81102426, -0.918267  , -1.32037384,-1.48949687]])

18) The best way to do reshape arrays in numpy?

np.arange(16).reshape(4,4)

19) The best way to transpose arrays in numpy?

X = np.arange(4).reshape((2,2))
X
array([[0, 1],
       [2, 3]])

#Output
np.transpose(X)
array([[0, 2],
       [1, 3]])

20) The best way to create an array utilizing random operate in numpy?

There are greater than 25 methods you need to use to random operate for producing random numbers. I’m exhibiting just one approach.

np.random.random((2,2))
#Output
array([[0.39891187, 0.7295893 ],
       [0.73831152, 0.27858847]])

21) The best way to append arrays in numpy?

#Create an array
X = np.array([1,2,3])
# Use numpy inbuilt append operate to append one other array
X = np.array(X,[4,5,6])
#Output
array([1, 2, 3, 4, 5, 6])

22) The best way to add arrays in numpy?

#Create an array
X = np.array([1,2,3])
# Use numpy inbuilt add operate
np.add(X,[4,5,6])
#Output
array([5, 7, 9])

23) The best way to add subtract arrays in numpy?

#Create an array
X = np.array([1,2,3])
# Use numpy inbuilt subtract operate
np.subtract(X,[4,5,6])
#Output
array([-3, -3, -3])

24) The best way to multiply arrays in numpy?

#Create an array
X = np.array([1,2,3])
# Use numpy inbuilt a number of operate
np.multiply(X,X)
#Output
array([1, 4, 9])

25) The best way to divide arrays in numpy?

#Create an array
X = np.array([1,2,3])
# Use numpy inbuilt divide operate
np.divide(X,X)
#Output
array([1, 1, 1])

26) The best way to discover mode worth in numpy array?

#Create an array
X = np.random.randint(0,10,50)
Use Bincount and argmax operate collectively
print(np.bincount(X).argmax())
7

You’re outcome will likely be completely different than shared out as random quantity operate will generate completely different outcomes.

Additionally learn : The best way to make 35+ completely different plots in Seaborn?

27) The best way to discover sum in numpy array?

#Create an array
X = np.random.randint(0,10,50)
#Use the inbuilt sum operate
X.sum()
236

You’re outcome will likely be completely different than shared out as random quantity operate will generate completely different outcomes.

28) The best way to discover log in numpy array?

#Create an array
X = np.random.randint(0,10,50)
np.log(X)
#Outcome
array([      -inf,       -inf, 0.69314718, 1.79175947, 1.38629436,
       1.94591015,       -inf, 1.94591015, 2.19722458,       -inf,
       1.38629436,       -inf, 1.60943791, 1.38629436, 1.94591015,
       1.79175947, 1.60943791, 1.09861229, 1.60943791, 1.79175947,
       0.69314718, 1.94591015, 1.09861229, 2.07944154, 1.09861229,
       1.79175947, 2.19722458, 2.19722458, 2.19722458, 1.38629436,
       1.94591015,       -inf, 0.        , 2.19722458,       -inf,
       0.        , 1.79175947, 1.79175947, 1.94591015, 1.94591015,
       1.60943791, 1.09861229, 0.69314718, 2.19722458, 2.19722458,
       1.94591015, 1.38629436, 2.07944154,       -inf, 1.60943791])

29) The best way to discover log2 in numpy array?

#Create an array
X = np.random.randint(0,10,50)
np.log2(X)
#Outcome
array([      -inf,       -inf, 1.        , 2.5849625 , 2.        ,
       2.80735492,       -inf, 2.80735492, 3.169925  ,       -inf,
       2.        ,       -inf, 2.32192809, 2.        , 2.80735492,
       2.5849625 , 2.32192809, 1.5849625 , 2.32192809, 2.5849625 ,
       1.        , 2.80735492, 1.5849625 , 3.        , 1.5849625 ,
       2.5849625 , 3.169925  , 3.169925  , 3.169925  , 2.        ,
       2.80735492,       -inf, 0.        , 3.169925  ,       -inf,
       0.        , 2.5849625 , 2.5849625 , 2.80735492, 2.80735492,
       2.32192809, 1.5849625 , 1.        , 3.169925  , 3.169925  ,
       2.80735492, 2.        , 3.        ,       -inf, 2.32192809])

30) The best way to discover log10 in numpy array?

#Create an array
X = np.random.randint(0,10,50)
np.log10(X)

#Outcome
array([      -inf,       -inf, 0.30103   , 0.77815125, 0.60205999,
       0.84509804,       -inf, 0.84509804, 0.95424251,       -inf,
       0.60205999,       -inf, 0.69897   , 0.60205999, 0.84509804,
       0.77815125, 0.69897   , 0.47712125, 0.69897   , 0.77815125,
       0.30103   , 0.84509804, 0.47712125, 0.90308999, 0.47712125,
       0.77815125, 0.95424251, 0.95424251, 0.95424251, 0.60205999,
       0.84509804,       -inf, 0.        , 0.95424251,       -inf,
       0.        , 0.77815125, 0.77815125, 0.84509804, 0.84509804,
       0.69897   , 0.47712125, 0.30103   , 0.95424251, 0.95424251,
       0.84509804, 0.60205999, 0.90308999,       -inf, 0.69897   ])

31) The best way to inverse matrix in numpy?

X = np.array([1,2,3,4,5,6,7,8,9,0])
X[::-1]
#Output
array([0, 9, 8, 7, 6, 5, 4, 3, 2, 1])

32) The best way to use repeat operate in numpy?

np.repeat(6,4)

#Output
array([6, 6, 6, 6])

33) The best way to type arrays in numpy?

X = np.array([1,2,3,100,5,6,-1,8,9,0])
np.type(X)
#Output
array([ -1,   0,   1,   2,   3,   5,   6,   8,   9, 100])

34) The best way to use argmax operate in numpy?

X = np.arange(6).reshape(2,3)
X
array([[0, 1, 2],
       [3, 4, 5]])
# Use inbuilt argmax operate
X.argmax()
#Output
5

35) The best way to use argmix operate in numpy?

X = np.arange(6).reshape(2,3)
X
array([[0, 1, 2],
       [3, 4, 5]])
# Use inbuilt argmin operate
X.argmmin()
#Output
0

36) The best way to use argsort in numpy?

X = np.arange(6).reshape(2,3)
X
array([[0, 1, 2],
       [3, 4, 5]])
# Use inbuilt argmin operate
X.argsort()
#Output
array([[0, 1, 2],
       [0, 1, 2]], dtype=int64)

37) The best way to initialize numpy array?

np.array([1,2,3])

38) The best way to use absolute operate in numpy array?

X = np.array([1,-2,3,-100,5,6,-1,8,-9,0])
#Use inbuilt absolute operate
np.absolute(X)

#Output
array([  1,   2,   3, 100,   5,   6,   1,   8,   9,   0])

39) The best way to inverse matrix in numpy?

X = np.array([1,-2,3,-100,5,6,-1,8,-9,0])
#Use inbuilt invert operate
np.invert(X)

#Output
array([-2,  1, -4, 99, -6, -7,  0, -9,  8, -1], dtype=int32)

40) The best way to discover absolute worth in numpy array?

X = np.array([1,-2,3,-100,5,6,-1,8,-9,0])
#Use inbuilt abs operate
np.abs(X)

#Output
array([  1,   2,   3, 100,   5,   6,   1,   8,   9,   0])

41) The best way to convert tensor to numpy array?

import tensorflow as tf

a = tf.fixed([[1, 2], [3, 4]])                 
b = tf.add(a, 1)

a.numpy()
# array([[1, 2],
#        [3, 4]], dtype=int32)

b.numpy()
# array([[2, 3],
#        [4, 5]], dtype=int32)

tf.multiply(a, b).numpy()
# array([[ 2,  6],
#        [12, 20]], dtype=int32)

42) The best way to use regular array in numpy?

np.random.regular(3, 2.5, measurement=(2, 4))

#output
array([[ 0.84673217,  3.84248666,  5.57427092,  0.49001164],
       [ 0.97016851, -3.05984677,  4.89746286,  2.39590382]])

43) The best way to use numpy cosine operate?

X = np.array([1,-2,3,-100,5,6,-1,8,-9,0])
#Use the inbuilt cos operate
np.cos(X)

#output
array([ 0.54030231, -0.41614684, -0.9899925 ,  0.86231887,  0.28366219,
        0.96017029,  0.54030231, -0.14550003, -0.91113026,  1.        ])

44) The best way to spherical numpy array to int?

X = np.random.regular(3, 2.5, measurement=(2, 4))
array([[3.57042396, 8.35559273, 0.7621579 , 3.53006476],
       [0.98356653, 7.66801256, 3.32831346, 4.70137415]])
#Use the inbuilt spherical operate
np.spherical(X,decimals=2)

#Output
array([[3.57, 8.36, 0.76, 3.53],
       [0.98, 7.67, 3.33, 4.7 ]])

45) The best way to use numpy resize operate?

X = np.arange(6).reshape(2,3)

array([[0, 1, 2],
       [3, 4, 5]])
#use inbuilt resize operate
np.resize(X,(3,2))

#output
array([[0, 1],
       [2, 3],
       [4, 5]])

46) The best way to use numpy arange operate?

X = np.arange(6)
array([0, 1, 2, 3, 4, 5])

47) The best way to use numpy change?

a = np.array([[0, 1, 2],[0, 2, 4],[0, 3, 6]])
#use inbuilt operate referred to as the place
np.the place(a < 4,a,10)
#output
array([[ 0,  1,  2],
       [ 0,  2, 10],
       [ 0,  3, 10]])

48) The best way to add aspect in numpy array?

#Create an array
X = np.array([1,2,3])
# Use numpy inbuilt append operate to append one other array
X = np.array(X,[4,5,6])
#Output
array([1, 2, 3, 4, 5, 6])

49) The best way to use numpy ciel operate?

a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
#Use inbuilt operate referred to as ceil
np.ceil(a)

#output
array([-1., -1., -0.,  1.,  2.,  2.,  2.])

Extra numpy workout routines to come back…..

Numpy Documentation

[ad_2]