Course 1.1Lesson 1 of 5
NumPy is the library that scientific Python is built on. Its main object is the array, which holds many numbers of one type and does maths on all of them at once. np.array turns a list into an array, and an array adds, multiplies and divides place by place, where a list joins and repeats. An array has a dtype, a shape, a number of dimensions and a length. np.arange works like range and leaves out the stop, np.linspace gives a fixed number of points with both ends included, and np.zeros, np.ones and np.full make arrays of one value. One type serves all the numbers, so one decimal turns the whole array into decimals, and astype changes the type. An array does a million operations far faster than a loop.
Try the code and the simulations beside the lesson.
After this lesson you can
np.array, np.arange, np.linspace, np.zeros, np.ones and np.full,dtype, the shape and the length of an array, andA science experiment gives numbers, and often many of them. A sensor writes a temperature every second, and in one hour there are 3600 numbers. A telescope records millions. You want to find the average, to convert the units, to find the biggest, and to draw a graph. With a list, you must write a loop for every one of these jobs.
NumPy (the name comes from Numerical Python) is the library that solves this. Its main object is the array: a row of numbers, all of the same type, on which you can do maths all at once, with no loop. Almost all scientific work in Python, from physics to biology, is built on NumPy. The libraries that you meet later in this book, SciPy, matplotlib and pandas, all use its arrays.
On this site, NumPy is ready to use. The first time that your code uses it, the site needs a few seconds to load it. After that, it is quick. By custom, we write the import in this way:
np is a short name, and every NumPy program in the world uses it.
Predict first. Look at the four lines that print. Which two print the same thing? Then run it.
An array does what a mathematician expects. This is the whole idea.
Which one to choose, arange or linspace? A good rule: use arange for whole-number steps (0, 2, 4, ...), and linspace when you need exactly a number of points, or when the step is a decimal. With a decimal step, arange can give one number too many or too few, because decimals are not exact. linspace always gives the number that you asked for.
Use linspace when you know how many points you want, as for a smooth graph. Use arange when you know the step.
The one type is what makes arrays fast. NumPy knows that every number takes the same space, so it can work through them quickly.
import numpy as np. Then np is not defined, and Python says NameError.np.array the numbers one by one. It needs one list: np.array([1, 2, 3]).arange includes the stop. np.arange(0, 10) ends at 9. on a list and expecting maths.** For a list, *` repeats. Make an array first.astype(int) rounds. It cuts off the decimal part.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
np.array(list), np.arange(start, stop, step), np.linspace(start, stop, count), np.zeros, np.ones, np.full.dtype, shape, ndim and len describe an array.astype changes the type. One decimal makes the whole array decimals.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
np.array([1, 2]) + np.array([3, 4]) give? And [1, 2] + [3, 4]?np.arange(0, 1, 0.25) give, and does it include 1?np.linspace(0, 1, 11) give, and what is the step?np.array([1, 2.0, 3])?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.