-
Notifications
You must be signed in to change notification settings - Fork 4.1k
[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
base: main
Are you sure you want to change the base?
Changes from all commits
4565457
203f00a
c25ae99
48b15d5
c0c1978
8a74636
7d979c0
ee78b88
379e63f
f644d04
6673f8f
f688672
49fd744
d9df706
560ff81
d1acf3e
ca58d85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
Uh oh!
There was an error while loading. Please reload this page.