numpy
Contents
numpy#
The first three slides will go by a little fast because you should be mostly familiar with these ideas for numpy
since they closely match the semantics of pandas
. The last two reading slides will most likely be the more complex topics of this reading.
Like always, you get better at things with practice and we will continue to get practice with numpy
!
Overview#
numpy
is one of the most popular Python libraries that is used for numerical processing. We have used numpy
(imported as np
) a few times before, but this week we will really dive into how to use it.
numpy
’s primary data structure is the numpy.array
. An array will store a sequence of values of the same type . You can think of a numpy.array
as being something a lot like a pandas.Series
, but the index is always integer values like a list
. The syntax to create one of these numpy.array
s looks like the following.
import numpy as np
x = np.array([1, 2, 3, 4])
print(x)
# If there are mixed types, it coerces values to the same type (int -> float)
x = np.array([3.14, 2, 3])
print(x)
Notice we are calling the function np.array
passing in a list of values, the return type is a numpy
object called an ndarray
(stands for \(n\)-dimensional array).
Applications#
Any time you need to represent a series of numbers in Python, numpy
is almost always the way to go because it is an incredibly efficient library for numerical computations. Just like we showed you that using built-in functions like min
or max
can significantly improve the speed of your programs, using numpy
can do the same for numerical processing.
Some example applications include:
Representing images (each pixel stores a value indicating color)
More complex representation where we make a
numpy.array
ofnumpy.array
s.
Representing sound (each point in time shows an amplitude for a frequency)
Creating numpy.array
s#
We showed you how to create numpy.array
s, but there are a few more helpful ones that are used commonly to make an array of any size.
import numpy as np
# array initializer takes a list of values
x = np.array([1, 2, 3])
# arange works like range to make an array of numbers from start (inclusive)
# to stop (exclusive)
x = np.arange(1, 5)
print(x)
# Makes an array storing all 1s of the given length
x = np.ones(5)
print(x)