A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/numpy-array-in-python/ below:

NumPy Array in Python - GeeksforGeeks

NumPy Array in Python

Last Updated : 24 Jan, 2025

NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python.

In this article, we will explore NumPy Array in Python.

Create NumPy Arrays

To start using NumPy, import it as follows:

import numpy as np

NumPy array’s objects allow us to work with arrays in Python. The array object is called ndarray. NumPy arrays are created using the array() function

Example:

Python
import numpy as np

# Creating a 1D array
x = np.array([1, 2, 3])

# Creating a 2D array
y = np.array([[1, 2], [3, 4]])

# Creating a 3D array
z = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

print(x)
print(y)
print(z)

Output
[1 2 3]
[[1 2]
 [3 4]]
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]
Key Attributes of NumPy Arrays

NumPy arrays have attributes that provide information about the array:

Example:

Python
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr.shape)  
print(arr.dtype)  
print(arr.ndim)  
Operations on NumPy Arrays

NumPy supports element-wise and matrix operations, including addition, subtraction, multiplication, and division:

Example:

Python
import numpy as np

# Element-wise addition
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
print(x + y)  # Output: [5 7 9]

# Matrix multiplication
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.dot(a, b))

Output
[5 7 9]
[[19 22]
 [43 50]]
Dimensions in NumPy Arrays

NumPy arrays can have multiple dimensions, allowing users to store data in multilayered structures.

Name Example 0D (zero-dimensional) Scalar - A single element 1D (one-dimensional) Vector- A list of integers. 2D (two-dimensional) Matrix- A spreadsheet of data 3D (three-dimensional) Tensor- Storing a color image NumPy Arrays vs Python Lists

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4