Task 3: Using numpy with random
Contents
Task 3: Using numpy
with random
¶
In this section, we will practice using the numpy
library.
First, import the numpy
library
import numpy as np
# Your Solution here
3.1: Create a vector¶
Task: Create an empty vector of size 10 filled with NaN.
Hint: you need to use zeros()
method or the empty()
method
# Your Solution here
3.2: Working with Vectors¶
Task: Create a random vector of size 10 and then find its mean, max, min, and sum value.
Hint: for random vector you need to use np.random.random()
and for the mean, max, min, sum you need to use build-in numpy methods mean()
,max()
,min()
,sum()
.
Sample output (Your numbers will be different)¶
[0.66698639 0.32937943 0.12074085 0.21788496 0.75628444 0.56461791 0.38162184 0.60966053 0.00491222 0.80007239]
The max is: 0.800
The min is: 0.005
The sum is: 4.452
The mean is: 0.445
# Your Solution Here
3.4: More vectors¶
Task: This is a multi-step question. Read all the directions before you start working on the question and plan out your solution.
First, using
numpy
, create a vector of size 15 which contains random values ranging from 10 to 90Then, replace the maximum value of your vector with 500.
Then, replace and the minimum value with -500.
Print the list (sorted by ascending order) and its mean before replacement, as well as the sorted list after replacement with the new mean.
Hint: To do this problem as intended, you may need to use the following numpy functions: copy()
, sort()
, argmax()
, and argmin()
. Also, don’t forget about f-strings
and triple-quoted strings for printing.
Sample output¶
Before number replacement
vector: [12 14 14 19 22 25 25 28 28 35 45 47 68 69 73]
vector_mean = 34.93
After number replacement
vector: [-500 14 14 19 22 25 25 28 28 35 45 47 68 69 500]
new_vector_mean = 29.27
# Your solution here