Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Basics of NumPy Arrays
Next article icon

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

In this article, we will exploreNumPy 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 thearray()function

Example:

Python
importnumpyasnp# Creating a 1D arrayx=np.array([1,2,3])# Creating a 2D arrayy=np.array([[1,2],[3,4]])# Creating a 3D arrayz=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:

  • shape: Returns the dimensions of the array.
  • dtype: Returns the data type of the elements.
  • ndim: Returns the number of dimensions.

Example:

Python
importnumpyasnparr=np.array([[1,2,3],[4,5,6]])print(arr.shape)print(arr.dtype)print(arr.ndim)

Output
(2, 3)int642

Operations on NumPy Arrays

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

Example:

Python
importnumpyasnp# Element-wise additionx=np.array([1,2,3])y=np.array([4,5,6])print(x+y)# Output: [5 7 9]# Matrix multiplicationa=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.

NameExample
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

  • Fixed Size: Arrays have a fixed size, while lists can dynamically grow.
  • Homogeneous Data: Arrays require uniform data types; lists can store mixed types.
  • Performance: Arrays are faster due to their optimized implementation.
  • Memory Efficiency: Arrays use contiguous memory blocks, unlike lists.

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp