Rolex Pearlmaster Replica
  Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
This article is part of in the series
Published: Saturday 25th February 2023

codes on screen

NumPy is one of the foundational modules in the Python library that provides developers working with other modules such as scikit-learn and pandas rely on.

Its zeros() function enables the creation of new arrays of various types filled with zero values. It accepts three parameters and returns arrays filled with floating-point zeros by default. The datatype can be modified by passing the parameters. 

In this brief guide, we walk you through the syntax, parameters, expected return values, and other nuances of the zeros() function. We illustrate the creation of ndarrays filled with zeros with examples. 

NumPy zeros() Function: Basic Examples

# Begin by importing numpy
import numpy as np

# Creating a 1-D array with zeros() 
exampleArray = np.zeros(5)
print(exampleArray)
# Output: [0. 0. 0. 0. 0.]

# Creating an array of integer zeros 
exampleArray = np.zeros(3, int)
print(exampleArray)
# Output: [0 0 0]

# Creating a two-dimensional array of zeros
exampleArray = np.zeros((3,4))
print(exampleArray)
# Output:
# [[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]

# Creating a three-dimensional array of zeros
exampleArray = np.zeros((4, 3, 5))
print(exampleArray)
# Output:
#[[[0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]]
#
# [[0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]]
#
# [[0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]]
#
# [[0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 0.]]]

# Creating an array with heterogeneous data types
exampleArray = np.zeros((3,2), dtype=[('x', 'int'), ('y', 'float')])
print(exampleArray)
# Output:
# [[(0, 0.) (0, 0.)]
#  [(0, 0.) (0, 0.)]
#  [(0, 0.) (0, 0.)]]

NumPy zeros() Function: Syntax

The syntax of the zeros() function of the numpy module is:

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

As you can see, the function has three parameters:

  • dtype: Defines the type of elements such as numpy.int8. The default dtype is numpy.float64.
  • order: Stores multi-dimensional data in either row-major (C) or column-major (F) pattern in the memory location.
  • shape: Specifies the shape of an array, which can be an int or a tuple containing ints. 

The zeros() function returns a ndarray of zeros in the supplied shape, datatype, and order.

NumPy zeros() Function: Usage

The function creates ndarrays made out of zeroes in the shape and type you specify. By default, it returns floating zeros of ndarray. 

Let's see how to create an array by passing a single integer to zeros(). 

import numpy as np

exampleArray = np.zeros(4)
print(exampleArray)

# Output:
# [0. 0. 0. 0.]

As you can see, we didn't specify a datatype or the order. A one-dimensional array of the supplied size was created and filled with zeroes.

You can also use a datatype parameter to return an array with integers:

exampleArray = np.zeros(5, int)
print(exampleArray)

# Output
# [0 0 0 0 0]

Creating 2-D Arrays with zeros()

You can create a two-dimensional array of zeros using the zeros() function's shape parameter. Let's see how you can make an array with three rows and four columns using the function:

exampleArray = np.zeros((3,4))
print(exampleArray)

# Output
# [[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]

You can also create three-dimensional arrays with the same function if you pass three numeric parameters instead of two. The first parameter defines the number of two-dimensional arrays that must be in this three-dimensional array. The second and third values define the row and column values of the two-dimensional arrays.

Latest Articles


Tags

  • deque
  • heap
  • Data Structure
  • howto
  • dict
  • csv in python
  • logging in python
  • Python Counter
  • python subprocess
  • numpy module
  • Python code generators
  • KMS
  • Office
  • modules
  • web scraping
  • scalable
  • pipx
  • templates
  • python not
  • pytesseract
  • env
  • push
  • search
  • Node
  • python tutorial
  • dictionary
  • csv file python
  • python logging
  • Counter class
  • Python assert
  • linspace
  • numbers_list
  • Tool
  • Key
  • automation
  • website data
  • autoscale
  • packages
  • snusbase
  • boolean
  • ocr
  • pyside6
  • pop
  • binary search
  • Insert Node
  • Python tips
  • python dictionary
  • Python's Built-in CSV Library
  • logging APIs
  • Constructing Counters
  • Assertions
  • Matplotlib Plotting
  • any() Function
  • Activation
  • Patch
  • threading
  • scrapy
  • game analysis
  • dependencies
  • security
  • not operation
  • pdf
  • build gui
  • dequeue
  • linear search
  • Add Node
  • Python tools
  • function
  • python update
  • logging module
  • Concatenate Data Frames
  • python comments
  • matplotlib
  • Recursion Limit
  • License
  • Pirated
  • square root
  • website extract python
  • steamspy
  • processing
  • cybersecurity
  • variable
  • image processing
  • incrementing
  • Data structures
  • algorithm
  • Print Node
  • installation
  • python function
  • pandas installation
  • Zen of Python
  • concatenation
  • Echo Client
  • Pygame
  • NumPy Pad()
  • Unlock
  • Bypass
  • pytorch
  • zipp
  • steam
  • multiprocessing
  • type hinting
  • global
  • argh
  • c vs python
  • Python
  • stacks
  • Sort
  • algorithms
  • install python
  • Scopes
  • how to install pandas
  • Philosophy of Programming
  • concat() function
  • Socket State
  • % Operator
  • Python YAML
  • Crack
  • Reddit
  • lightning
  • zip files
  • python reduce
  • library
  • dynamic
  • local
  • command line
  • define function
  • Pickle
  • enqueue
  • ascending
  • remove a node
  • Django
  • function scope
  • Tuple in Python
  • pandas groupby
  • pyenv
  • socket programming
  • Python Modulo
  • Dictionary Update()
  • Hack
  • sdk
  • python automation
  • main
  • reduce
  • typing
  • ord
  • print
  • network
  • matplotlib inline
  • Pickling
  • datastructure
  • bubble sort
  • find a node
  • Flask
  • calling function
  • tuple
  • GroupBy method
  • Pythonbrew
  • Np.Arange()
  • Modulo Operator
  • Python Or Operator
  • Keygen
  • cloud
  • pyautogui
  • python main
  • reduce function
  • type hints
  • python ord
  • format
  • python socket
  • jupyter
  • Unpickling
  • array
  • sorting
  • reversal
  • Python salaries
  • list sort
  • Pip
  • .groupby()
  • pyenv global
  • NumPy arrays
  • Modulo
  • OpenCV
  • Torrent
  • data
  • int function
  • file conversion
  • calculus
  • python typing
  • encryption
  • strings
  • big o calculator
  • gamin
  • HTML
  • list
  • insertion sort
  • in place reversal
  • learn python
  • String
  • python packages
  • FastAPI
  • argparse
  • zeros() function
  • AWS Lambda
  • Scikit Learn
  • Free
  • classes
  • turtle
  • convert file
  • abs()
  • python do while
  • set operations
  • data visualization
  • efficient coding
  • data analysis
  • HTML Parser
  • circular queue
  • effiiciency
  • Learning
  • windows
  • reverse
  • Python IDE
  • python maps
  • dataframes
  • Num Py Zeros
  • Python Lists
  • Fprintf
  • Version
  • immutable
  • python turtle
  • pandoc
  • semantic kernel
  • do while
  • set
  • tabulate
  • optimize code
  • object oriented
  • HTML Extraction
  • head
  • selection sort
  • Programming
  • install python on windows
  • reverse string
  • python Code Editors
  • Pytest
  • pandas.reset_index
  • NumPy
  • Infinite Numbers in Python
  • Python Readlines()
  • Trial
  • youtube
  • interactive
  • deep
  • kernel
  • while loop
  • union
  • tutorials
  • audio
  • github
  • Parsing
  • tail
  • merge sort
  • Programming language
  • remove python
  • concatenate string
  • Code Editors
  • unittest
  • reset_index()
  • Train Test Split
  • Local Testing Server
  • Python Input
  • Studio
  • excel
  • sgd
  • deeplearning
  • pandas
  • class python
  • intersection
  • logic
  • pydub
  • git
  • Scrapping
  • priority queue
  • quick sort
  • web development
  • uninstall python
  • python string
  • code interface
  • PyUnit
  • round numbers
  • train_test_split()
  • Flask module
  • Software
  • FL
  • llm
  • data science
  • testing
  • pathlib
  • oop
  • gui
  • visualization
  • audio edit
  • requests
  • stack
  • min heap
  • Linked List
  • machine learning
  • scripts
  • compare string
  • time delay
  • PythonZip
  • pandas dataframes
  • arange() method
  • SQLAlchemy
  • Activator
  • Music
  • AI
  • ML
  • import
  • file
  • jinja
  • pysimplegui
  • notebook
  • decouple
  • queue
  • heapify
  • Singly Linked List
  • intro
  • python scripts
  • learning python
  • python bugs
  • ZipFunction
  • plus equals
  • np.linspace
  • SQLAlchemy advance
  • Download
  • No
  • nlp
  • machiine learning
  • dask
  • file management
  • jinja2
  • ui
  • tdqm
  • configuration
  • Python is a beautiful language.