Skip to content

[Term Entry] Python - NumPy Built in functions: .zeros() #6944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions content/numpy/concepts/built-in-functions/terms/zeros/zeros.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
Title: '.zeros()'
Description: 'Creates a new array filled with zeros.'
Subjects:
- 'AI'
- 'Computer Science'
- 'Data Science'
- 'Data Visualization'
- 'Machine Learning'
Tags:
- 'AI'
- 'Data Structures'
- 'Linear Algebra'
- 'NumPy'
- 'Python'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

**`.zeros()`** is a NumPy function used to create a new array of a specified shape, filled entirely with zeros. It is commonly used to initialize arrays before assigning them meaningful data.

## Syntax

```pseudo
numpy.zeros(shape, dtype=float, order='C')
```

**Parameters:**

- `shape`: An integer or tuple of integers indicating the dimensions of the array. For example, `(3, 2)` creates a 2D array with 3 rows and 2 columns.
- `dtype`: The desired data type for the array elements (default is `float`).
- `order`: Whether to store the array in row-major (`'C'`, default) or column-major (`'F'`) order.

## Example
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you give a one line description of the example


This example creates a 1D array of five zeros and a 2D array of zeros with shape `(3, 2)` and integer data type using NumPy's `.zeros()` function:

```py
import numpy as np

array1 = np.zeros(5)
print(array1)

array2 = np.zeros((3, 2), dtype=int)
print(array2)
```

The output of this code will be:

```shell
[0. 0. 0. 0. 0.]
[[0 0]
[0 0]
[0 0]]
```

## Codebyte Example: Initializing a Sensor Readings Matrix

In this example, we use `.zeros()` to initialize a 2D array to store temperature sensor readings for 7 days across 3 different locations:

```codebyte/python
import numpy as np

# Initialize a 7x3 matrix for daily temperature readings across 3 locations
# Rows: Days (1 to 7), Columns: Location 1, 2, and 3
temperature_readings = np.zeros((7, 3))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a ## Codebyte Example block as well. Please note that the codebyte example block is not followed by the output block

print("Initial temperature matrix:")
print(temperature_readings)
```

Here, `.zeros()` prepares a placeholder matrix to store daily sensor readings, which will be updated later as data is collected.