We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 14
yonei21, 212PM NumPy_exercisesipynb - Colaboratory
~ NumPy exercises
# Import the numpy package under the name np
import numpy as np
# Print the numpy version and the configuration
print(np.__version_)
1.19.5
~ Array creation
~ Create a numpy array of size 10, filled with zeros.
# your code goes here
aenp.array([8,0,0,0,0,0,0,0,0,0])
array([@, @, @, ®, @, @, @, ® @ 2])
ee
» Create a numpy array with values ranging from 10 to 49
# your code goes here
benp.arange(10, 50)
b
@ array((1e, 11, 12, 13, 14, 15, 16, 17, 18, 19, 28, 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])
a
» Create a numpy matrix of 2*2 integers, filled with ones.
# your code goes here
ce=np.array([[@,@],[@,@]])
hitpsicolab.research.google.comlervel gTYUSVVKsFaSytAleyx_4ISzcFGqnGmfscral
mF uPomSMBl&printMode=true aneyonei21, 212PM NumPy_exercisesipynb - Colaboratory
c
array([[@, @],
fe, @)])
+ Create a numpy matrix of 3*2 float numbers, filled with ones.
# your code goes here
dnp array({(1,1], [1,1], [1,1] ],dtype-float)
a
array({[t., 1-1,
an
f, 1-1)
Given the X numpy array, create a new numpy array with the same shape and
type as X, filled with ones.
# your code goes here
Xenp.array([@,2,0,0])
Xi=np.ones(X. shape, dtype=X.dtype)
print (x1)
[za24)
Given the X numpy matrix, create a new numpy matrix with the same shape
and type as X, filled with zeros.
# your code goes here
Xenp.array([2,2,0,])
Xi=np.zeros(X. shape, dtype=X.dtype)
print(x1)
[@ 0 ee]
+ Create a numpy matrix of 4*4 integers, filled with fives.
hitps:icolab research. google.comicrive! tgTYUSVVKSF8SytAleyDx_4ISzcF GqnGmiscralTo=TmFulPomSMoigprinMode=true aneyonei21, 212PM NumPy_exercisesipynb - Colaboratory
# your code goes here
e=np.full(4*4,5).reshape(4,4)
e
array([[5, 5, 5, 5],
(5,5, 5, 5],
[5, 5, 5, 5],
[s, 5, 5, 5]])
Given the X numpy matrix, create a new numpy matrix with the same shape
and type as X, filled with sevens.
# your code goes here
fenp.full_like(e,7)
£
array([[7, 7, 75 7],
(7, 7, 7, 71,
(7,7. 75 71,
(7, 7, 7, 71])
Create a 3*3 identity numpy matrix with ones on the diagonal and zeros
elsewhere.
# your code goes here
g=np.eye(3,dtype=int)
8
array([[1, ®, @],
{e, 1, @],
fe, @, 1]])
~ Create a numpy array, filled with 3 random integer values between 1 and 10.
# your code goes here
h=np.random.randint(1,10,(1,3))
h
array([[4, 7, 7]])
hitps:icolab research. google.comicrive! tgTYUSVVKSF8SytAleyDx_4ISzcF GqnGmiscralTo=TmFulPomSMoigprinMode=true aneyonei21, 212PM NumPy_exercisesipynb - Colaboratory
~ Create a 3*3*3 numpy matrix, filled with random float values.
# your code goes here
isnp.random.rand(3,3,3)
i
array([[[@.33489841, @.49214966, ¢.41200148),
[0.53888296, 0.56870758, 2.40803978],
[@.52221904, 0.46505793, 0.76296786] ],
[[@.01847499, @,7105094 , @.09617034),
[0.47662941, 0.65266601, 2.39536189],
[@.57087978, 0.11136233, .00677635]],
[[2.86609683, 0.16126706, 0.32468952],
[0.3239674 , @.00560272, 0.38679539],
[e.548995@5, 0.48389531, 0.68813783]]])
a
~ Given the X python list convert it to an Y numpy array
# your code goes here
X=[1,2,3,4,5]
Yenp.array(X)
Y
array([1, 2, 3, 4, 5])
a
~ Given the X numpy array, make a copy and store it on Y.
# your code goes here
Xenp-array([1,1,1,1,1])
Yenp.copy(X)
y
array({1, 1, 1, 1, 1])
~ Create a numpy array with numbers from 1 to 10
# your code goes here
Aznp.arange(1,11)
A
hitpsicolab.research.google.comlervel gTYUSVVKsFaSytAleyx_4ISzcFGqnGmfscral
mF uPomSMBl&printMode=true 4neyonei21, 212PM NumPy_exercisesipynb - Colaboratory
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
~ Create a numpy array with the odd numbers between 1 to 10
# your code goes here
A=nparange(1, 11,2)
>
array([1, 3, 5, 7, 9])
~ Create a numpy array with numbers from 1 to 10, in descending order.
# your code goes here
Aznp.arange(1@,@, -1)
print(A)
[e987 65 4 3 2 1)
> Create a 3*3 numpy matrix, filled with values ranging from 0 to 8
# your code goes here
Aznp.arange(@,9).reshape(3, 3)
>
array([[@, 1, 2],
(3, 4 5],
[6, 7, 8]])
~ Show the memory size of the given Z numpy matrix
# your code goes here
A.size
> Array indexation
hitps:icolab research. google.comicrive! tgTYUSVVKSF8SytAleyDx_4ISzcF GqnGmiscralTo=TmFulPomSMoigprinMode=true sinasonst, 3:12PM NumPy_exercisesjpynb - Colaboratory
~ Given the X numpy array, show it's first element
# your code goes here
Benp.array([1,2,3,4])
Be]
a
> Given the X numpy array, show it's last element
# your code goes here
Benp.array([1,2,3,4])
B[-1]
a
y Given the X numpy array, show it's first three elements
# your code goes here
p-array([1,2,3,4])
B[@:3]
array({1, 2, 3])
a
y Given the X numpy array, show all middle elements
# your code goes here
Benp.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
print (B[1:3,1:3])
{L6 7)
[10 11]]
~ Given the X numpy array, show the elements in reverse position
# your code goes here
A=np.array([1,2,3,4,5,6])
hitpsicolab research. google.comicrivel 1gTYUSVVKsF8SytAleyDx_4ISzcF GqnGmiscralTo=TmFuiPomSMoigprinMode=true aneyonei21, 212 PM NumPy_exercisesipynb - Colaboratory
Alii-1]
array([6, 5, 4, 3, 2, 1])
4
Given the X numpy array, show the elements in an odd position
# your code goes here
A=np.array([1,2,3,4,5,6])
AL::2]
array([1, 3, 5])
4
Given the X numpy matrix, show the first row elements
# your code goes here
Benp.array([[1,2,3],[4,5,6],[7,8,91])
Be)
array([1, 2, 3])
4
Given the X numpy matrix, show the last row elements
# your code goes here
B=np.array([[1,2,3],[4,5,6],[7,8,91])
B[-1]
array([7, 8, 9])
4
Given the X numpy matrix, show the first element on first row
# your code goes here
Benp.array([[1,2,3],[4,5,6],[7,8,9]])
8[@} [2]
~ Given the X numpy matrix, show the last element on last row
hitps:icolab research. google.comicrive! tgTYUSVVKSF8SytAleyDx_4ISzcF GqnGmiscralTo=TmFulPomSMoigprinMode=true m6yonei21, 212PM NumPy_exercisesipynb - Colaboratory
# your code goes here
Benp.array([[1,2,3],[4,5,6],[7,8,9]])
B[-1][-1]
9
ec
4
Given the X numpy matrix, show the middle row elements.
# your code goes here
Benp.array([[1,2,3],[4,5,6],[7,8,9]])
print(B[1])
[45 6]
4
Given the X numpy matrix, show the first two elements on the first two rows
# your code goes here
X = np.array([
1 2 3, 41,
[5 6 7 81,
(9, 10, 11, 121,
[13, 14, 15, 16]
array([[1, 2],
(5, 6]])
‘
Given the X numpy matrix, show the last two elements on the last two rows
# your code goes here
X = np.array([
a 23 4)
(5, 6, 7, 81,
[9, 10, 11, 12],
(a3, 14, 15, 16]
X[+2:, -2:]
hitps:icolab research. google.comicrive! tgTYUSVVKSF8SytAleyDx_4ISzcF GqnGmiscralTo=TmFulPomSMoigprinMode=true aneyonei21, 212PM NumPy_exercisesipynb - Colaboratory
array([[11, 12],
(15, 16]])
y Array manipulation
~ Convert the given integer numpy array to float
# your code goes here
Xenp.array([[1,2,3],[4,5,6]4[7,8,91])
X-X.astype(np. float)
x
array([[1-, 2., 3.],
[4., 5.5 6],
(7. 8.5 9.1]
4
Reverse the given numpy array (first element becomes last)
# your code goes here
Xenp.array([1,2,3,4,5])
XeX[ 2-2]
x
array([5, 4, 3, 2, 1])
‘
Order (sort) the given numpy array
# your code goes here
Xenp.array([2,3,1,5,4])
np.sort(x)
x
array([2, 3, 1, 5, 4])
a
> Given the X numpy array, set the fifth element equal to 1
# your code goes here
Xenp.array([2,3,1,5,4])
hitpsicolab research. google.comicrivel pT YUSVVKSF8SytAleyDx_4ISzcF GqnGmiscrolTo=TmFuiPomSMoigprinMode=true oneyonei21, 212PM NumPy_exercisesipynb - Colaboratory
x[4]=1.
x
array([2, 3, 1, 5, 1])
~ Given the X numpy array, change the 50 with a 40
# your code goes here
Xenp.array([2,3,1,50,4])
X=np.where(X==50,40,X)
x
array([ 2, 3, 1, 40, 4])
4
Given the X numpy matrix, change the last row with all 1
# your code goes here
Xenp.array([ [1,253], [4,5.6],[7,8,91])
X[-1]=2
x
array([[1, 2, 3],
[4, 5, 6],
[1, 1, 1]])
4
Given the X numpy matrix, change the last item on the last row with a 0
# your code goes here
Xenp.array([[1,2,3],[4,5,6],[7,8,9]])
X[-][-2]=0
x
array([[1, 2, 3],
[4 5, 6],
(7, 8 @]])
~ Given the X numpy matrix, add 5 to every element
# your code goes here
X=np.array([[1,2,3],[4,5,6],[7,8,9]])
hitpsicolab research. google.comicrivel tgTYUSVVKsF8SytAleyDx_4ISzcF GqnGmiscrolTo=TmFulPomSMoigprinMode=tue soneyonei21, 212PM NumPy_exercisesipynb - Colaboratory
XeX4S
x
array([[ 6, 7, 8],
[ 9, 18, 21],
(a2, 13, 14]})
<
Boolean arrays (also called masks)
4
Given the X numpy array, make a mask showing negative elements
# your code goes here
X=np.array([[-1,2,3],[-4,-5,6],[7,8,-9]])
Xo
array({[ True, False, False],
[ True, True, False],
[False, False, True]])
4
Given the X numpy array, get the negative elements
# your code goes here
Xenp.array([(-1,2,3],[-4-5,6],[7,8,-9]])
x[Xa]
array([-1, -4, -5, -9])
4
Given the X numpy array, get numbers higher than 5
# your code goes here
Xenp.array([[-1,2,3],[-4,-5,6],[7,8,-9]])
X[X95]
array([6, 7, 8])
~ Given the X numpy array, get numbers higher than the elements mean
# your code goes here
Iitpscolab research google.comisvelgTYUSVWKsF8SyiAleyDx_é132cF GqnGmiserolTo=TmFulPomSMaiéprin’Mode=true wetort6r21, 3:12PM NumPy_exercisesjpynb - Colaboratory
X=np.array([[1,2,3],[4,5,6],[7,8,9]])
X[X>X.mean()]
array([6, 7, 8, 9])
4
Logic functions
4
Given the X numpy array, return True if none of its elements is zero
# your code goes here
Xenp.array([[1,2,3],[4,5,6],[7,8,9]])
X.all()
True
4
Given the X numpy array, return True if any of its elements is zero
# your code goes here
Xenp array({[1,2,3],14,0,6]4[7.8,9]])
~x.all()
True
~ Summary statistics
4
Given the X numpy array, show the sum of its elements
# your code goes here
X=np.array([[1,2,3],[4,5,6],[7,8,9]])
X.sum()
45
a
~ Given the X numpy array, show the mean value of its elements
# your code goes here
hitpsicolab research. google.comicrive! tgTYUSVVKSF8SytAleyDx_4ISzcF GqnGmiscrolTo=TmFulPomSMoigprinMode=true saneyonei21, 212PM NumPy_exercisesipynb - Colaboratory
X=np.array([[1,2,3],[4,5,6],[7,8,91])
X.mean()
°
~ Given the X numpy matrix, show the sum of its columns
# your code goes here
X=np.array([[1,2,3],[4,5,6],[7,8,91])
X.sum(axis=@)
array([12, 15, 18])
4
Given the X numpy matrix, show the mean value of its rows
# your code goes here
X=np.array([[1,2,3],[4,5,6],[7,8,91])
X.sum(axis=1)
array([ 6, 15, 24])
Given the X numpy array, show the max value of its elements
4
# your code goes here
X=np.array([[1,2,3],[4,5,6],[7,8,9]])
X.max()
hitps:icolab research. google.comicrive! tgTYUSVVKSF8SytAleyDx_4ISzcF GqnGmiscralTo=TmFulPomSMoigprinMode=true 134yonei21, 212PM NumPy_exercisesipynb - Colaboratory
hitps:icolab research. google.comicrive! tgTYUSVVKSF8SytAleyDx_4ISzcF GqnGmiscralTo=TmFulPomSMoigprinMode=true sane