NumPy: lesson 1 of 3

NumPy

PATH 01MODULE 03LESSON 01 OF 03Next: Indexing, Slicing, Shapes, and Reshaping

NumPy Arrays and Why They Matter

Learn why NumPy arrays are the foundation for efficient numerical Data Science work.

Beginner14 min readnumpyarraysnumerical-computingdata-science

Concept

NumPy is a Python library for numerical computing. Its central object is the array: an ordered block of values that can be one-dimensional, like customer ages, or two-dimensional, like a table of observations and features. Import it by convention as np.

import numpy as np

Why It Matters

Python lists are flexible, but Data Science often needs the same numeric operation applied across many values. NumPy arrays are designed for this style of work and give later tools such as Pandas and scikit-learn a common numerical representation. Arrays also describe the shape of ML inputs clearly: observations by features.

Intuition

Think of a list as a general-purpose notebook page that can hold many kinds of objects. Think of a NumPy array as a numeric spreadsheet block: its values normally share one primary data type, and operations can be applied across the block in one expression.

Creating Arrays

Use np.array() to create an array from a Python list.

customer_ages = np.array([24, 31, 29, 45])
monthly_sales = np.array([1200.0, 1500.0, 1800.0])

print(customer_ages)
print(monthly_sales)

Expected output:

[24 31 29 45]
[1200. 1500. 1800.]

A one-dimensional array has one sequence of values. A two-dimensional array has rows and columns.

feature_matrix = np.array([
    [24, 42000],
    [31, 55000],
    [45, 68000],
])

print(feature_matrix)

Each row can represent one customer; the columns can represent age and annual income.

Basic Array Properties

Arrays expose useful properties without parentheses:

print(feature_matrix.ndim)
print(feature_matrix.shape)
print(feature_matrix.size)
print(feature_matrix.dtype)

Expected output:

2
(3, 2)
6
int64

The exact integer dtype can vary by system, but the meaning is the same. ndim is the number of dimensions, shape gives the size of each dimension, size counts all values, and dtype describes the primary stored type.

Python Lists Versus NumPy Arrays

The difference becomes visible when applying arithmetic.

sales_list = [10, 20, 30]
sales_array = np.array([10, 20, 30])

print(sales_list * 2)
print(sales_array * 2)

Expected output:

[10, 20, 30, 10, 20, 30]
[20 40 60]

For a list, * 2 repeats the list. For a NumPy array, * 2 multiplies each numeric element. This element-wise behavior is a major reason arrays are useful. The next lessons explore it more fully as vectorization.

Creating Useful Starter Arrays

NumPy can create arrays without first writing every value.

print(np.zeros(3))
print(np.ones((2, 3)))
print(np.arange(0, 10, 2))
print(np.linspace(0, 1, 5))

Expected output:

[0. 0. 0.]
[[1. 1. 1.]
 [1. 1. 1.]]
[0 2 4 6 8]
[0.   0.25 0.5  0.75 1.  ]

zeros() and ones() are useful for initializing arrays. arange() creates values separated by a step, while linspace() creates a requested number of evenly spaced values over an interval.

Real-World / Data Science Example

Model prediction probabilities are naturally numeric arrays.

model_predictions = np.array([0.12, 0.78, 0.41, 0.92])
average_prediction = model_predictions.mean()

print(average_prediction)

Expected output: 0.5575. Later you might compare these probabilities with a classification threshold, calculate metrics, or inspect their distribution.

Code Explanation

np.array() converts the provided nested list into one array object. A two-dimensional nested list must have consistent row lengths. NumPy will often convert mixed numeric values to a compatible common dtype, such as converting integers to floats. Mixing numbers and text can instead produce a text array, which is usually not what a numerical calculation needs.

When to Use It

Use NumPy arrays for numerical vectors, matrices, sensor readings, model predictions, feature matrices, and repeated mathematical calculations. A regular list remains useful for flexible mixed Python objects or when numerical operations are not central.

Failure Signals

Common Mistakes

  1. Expecting list multiplication and array multiplication to behave the same way.
  2. Treating shape as a function; it is a property, so use array.shape.
  3. Creating a ragged two-dimensional input where rows have different lengths.
  4. Mixing strings and numbers unintentionally, then expecting numeric calculations.

Best Practices

Inspect shape and dtype before a major calculation. Name arrays by what they represent, such as customer_ages or feature_matrix. Use floating-point arrays for quantities that need decimal calculations, and keep a consistent numeric representation for a feature.

Data Science Perspective

Pandas columns are often backed by NumPy-like arrays, and ML libraries commonly expect a matrix where rows are observations and columns are features. Learning to inspect dimensions now prevents confusion later when a model expects (samples, features) rather than one long list.

Interview Perspective

Question: Why might you choose a NumPy array instead of a Python list for numeric data? A strong answer: arrays support compact numerical operations across many values, have explicit shape and dtype information, and are a standard representation for numerical Data Science workflows.

Practice Questions

  1. Create an array of five customer ages and print its shape and dtype.
  2. Create a two-row, three-column feature matrix using np.array().
  3. Explain why [10, 20, 30] * 2 differs from np.array([10, 20, 30]) * 2.

Quick Quiz

  1. What does ndim describe? Answer: the number of array dimensions.
  2. Which function creates values from 0 through 8 with a step of 2? Answer: np.arange(0, 10, 2).
  3. What does size count? Answer: every element in the array.

Key Takeaway

Key Takeaways

NumPy arrays are the basic numeric container for Data Science. Their shape, dtype, and element-wise operations make numerical work more direct than using general Python lists alone.

Next Lesson

Next, learn how to select, filter, and reshape the values inside an array.

Finish this lesson on your terms

Mark it complete when you have worked through the material and are ready to move on.