Get startedGet started for free

Introducing arrays

1. Introducing arrays

Hi, I'm Izzy, your NumPy coach! By the end of this course, I hope you'll be an enthusiastic member of Team NumPy. Let's get started!

2. NumPy and the Python ecosystem

NumPy is the core library for scientific computing in Python. Foundational Python libraries such as pandas, SciPy, and Matplotlib are built on top of NumPy's API. So are machine learning libraries such as TensorFlow and scikit-learn, which use NumPy arrays as inputs. Anyone who uses numbers in Python will come across NumPy arrays. What are they, and how do we create them?

3. NumPy arrays

The array is the main object in NumPy; it's a grid-like structure that holds data. An array can have any number of dimensions, and each dimension can be any length.

4. Importing NumPy

To get started, import NumPy, aliasing the import as np.

5. Creating 1D arrays from lists

We can create arrays from Python lists by passing a list as an argument to the np-dot-array function. The data type of this array is a NumPy n-d-array, or n-dimensional array.

6. Creating 2D arrays from lists

To create a 2-dimensional array, pass np-dot-array a list of lists. As you might expect, a list of lists of lists would generate a 3-dimensional array.

7. Arrays vs. Python lists

Why use arrays rather than lists? While Python lists can include many different data types, all of the elements in a NumPy array must be the same data type. This makes NumPy very efficient: there's no need for NumPy to check the data type of each element in an array since they must all be the same. Having only a single data type also means that a NumPy array takes up less space in memory than the same information would if stored as a Python list.

8. Creating arrays from scratch

We've seen how to create NumPy arrays from Python lists. We can also create them from scratch using functions such as np-dot-zeros, np-dot-random-dot-random, and np-dot-arange.

9. Creating arrays: np.zeros()

np-dot-zeros creates an array full of zeros. We can use the zeros array just as we might use an empty Python list, filling it with data later on. We tell np-dot-zeros the shape of the desired array using a tuple of integers, each representing the length of a given dimension. A tuple is a Python data type used to store collections of data, similar to a list. Tuples are created with parentheses rather than the square brackets used for lists. Here, when np-dot-zeros is given a tuple of five and three, it creates an array with five rows and three columns.

10. Creating arrays: np.random.random()

np-dot-random-dot-random also accepts a tuple with the desired array's shape. The array will be made up of random floats between zero and one. Why is np-dot-random-dot-random called that, rather than just np-dot-random? It's because np-dot-random-dot-random is a function within NumPy's random module, which contains useful functions for random sampling.

11. Creating arrays with np.arange()

np-dot-arange creates an evenly-spaced array of numbers based on given start and stop values. By default, np-dot-arange creates an array of sequential integers. The start value is included in the output array, but the stop value is not. The start value can be omitted if the range begins with zero. If a third argument is passed, it is interpreted as the step value. Now, the desired distance between elements is three. np-dot-arange is particularly useful for plotting!

12. Let's practice!

Let's put your new skills to work with grid-like data that you might be familiar with: a sudoku game!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.