Module 2 · NumPy and SciPy · Lesson 11 of 66
Random numbers, reshape and attributes
Random arrays, changing shape, and finding the biggest value.
In this lesson · about 14 minutes
- Make random arrays with rand, randn and randint
- Reshape arrays and read their shape and dtype
- Find max, min and where they are
Random numbers
| Code | Gives |
|---|---|
np.random.rand(5, 5) | Uniform random numbers from 0 up to (not including) 1, in the shape you ask for |
np.random.randn(5) | Samples from the standard normal distribution (centered on 0, most values between −2 and 2) |
np.random.randint(1, 100, 10) | 10 random integers from 1 up to (not including) 100 |
Random numbers change on every run. To get the same "random" numbers each time, set a seed first: np.random.seed(101). That makes your results repeatable, which matters when you share work.
Shape, reshape and dtype
arr.shape is an attribute (no parentheses): a tuple with the size of each dimension. A vector of 25 values has shape (25,). arr.reshape(5, 5) returns the same data arranged in a new shape, as long as the sizes multiply to the same total. arr.dtype tells you the type of the values, like int64 or float64.
max, min, argmax, argmin
arr.max() and arr.min() give the largest and smallest values. arr.argmax() and arr.argmin() give their index positions instead.
Exercise
A 3 × 3 matrix from 0 to 8
Create a 3 × 3 matrix with values 0 through 8.
Exercise
From 0.01 to 1
Create this 10 × 10 matrix:
array([[0.01, 0.02, 0.03, …, 0.10],
[0.11, 0.12, 0.13, …, 0.20],
…
[0.91, 0.92, 0.93, …, 1.00]])Exercise
A random number between 0 and 1
Use NumPy to make an array holding one random number between 0 and 1.
Exercise
25 standard normal samples
Make an array of 25 random numbers from the standard normal distribution.
Exercise
Which day sold the most?
sales holds one week of sales, Monday first. Set best_day to the index of the biggest day and best to its amount.
Check your understanding0 of 3 correct
Quiz score: 0 of 3
Exercises solved: 0 of 5
Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.