Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Numpy - ndarray
Next article icon

NumPy stands for Numerical Python and is used for handling large, multi-dimensional arrays and matrices.Unlike Python's built-in lists NumPy arrays provide efficient storage and faster processing for numerical and scientific computations. It offers functions for linear algebra and random number generation making it important for data science and machine learning.

Types of Array

Various types of arrays are as follows:

1. One Dimensional Array:

A one-dimensional array is a type of linear array.

One Dimensional Array

Example:

Python
importnumpyasnplist=[1,2,3,4]sample_array=np.array(list)print("List in python : ",list)print("Numpy Array in python :",sample_array)

Output:

n1
One Dimensional Array:

Check data type for list and array:

Python
print(type(list_1))print(type(sample_array))

Output:

n2
Check data type

2. Multi-Dimensional Array:

A Multi-Dimensional Array is an array that can store data in more than one dimension such as rows and columns. In simple terms it is a array of arrays.

For example a 2D array is like a table with rows and columns where each element is accessed by two indices: one for the row and one for the column. Higher dimensions like 3D arrays involve adding additional layers.

Two Dimensional Array

Example:

Python
importnumpyasnplist_1=[1,2,3,4]list_2=[5,6,7,8]list_3=[9,10,11,12]sample_array=np.array([list_1,list_2,list_3])print("Numpy multi dimensional array in python\n",sample_array)

Output:

n3
Multi-Dimensional Array

Note: use[ ] operators inside numpy.array() for multi-dimensional.

Parameters of a Numpy Array

1. Axis:Axis of an array describes the order of the indexing into the array.

Axis 0 = one dimensional
Axis 1 = Two dimensional
Axis 2 = Three dimensional 

2. Shape:Number of elements along with each axis and is returned as a tuple.

Example:

Python
importnumpyasnpsample_array=np.array([[0,4,2],[3,4,5],[23,4,5],[2,34,5],[5,6,7]])print("shape of the array :",sample_array.shape)

Output:

shape of the array : (5, 3)

3. Rank:Rank of an array is simply the number of axes or dimensions it has.

One-dimensional array has rank 1.

Rank 1

Two-dimensional array has rank 2.

Rank 2

4. Data type objects (dtype):Data type objects (dtype) is an example ofnumpy.dtypeclass. It describes how the bytes in the fixed-size block of memory corresponding to an array item should be interpreted.

Example:

Python
importnumpyasnpsample_array_1=np.array([[0,4,2]])sample_array_2=np.array([0.2,0.4,2.4])print("Data type of the array 1 :",sample_array_1.dtype)print("Data type of array 2 :",sample_array_2.dtype)

Output: 

n5
dtype

Different Ways of Creating Numpy Array

1. numpy.array():Numpy array object in Numpy is called ndarray. We can create ndarray using thisfunction.

Syntax:numpy.array(parameter)

Example: 

Python
importnumpyasnparr=np.array([3,4,5,5])print("Array :",arr)

Output:

Array : [3 4 5 5]

2.numpy.fromiter():The fromiter() function create a new one-dimensional array from an iterable object.

Syntax: numpy.fromiter(iterable, dtype, count=-1)

Example:

Python
importnumpyasnpvar="Geekforgeeks"arr=np.fromiter(var,dtype='U2')print("fromiter() array :",arr)

Output:

fromiter() array : ['G' 'e' 'e' 'k' 'f' 'o' 'r' 'g' 'e' 'e' 'k' 's']
 

3.numpy.arange():This is an inbuilt NumPy function that returns evenly spaced values within a given interval.

Syntax: numpy.arange( start , stop, step , dtype=None )

Example:

Python
importnumpyasnpnp.arange(1,20,2,dtype=np.float32)

Output:

array([ 1.,  3.,  5.,  7.,  9., 11., 13., 15., 17., 19.], dtype=float32) 

4.numpy.linspace():This function returns evenly spaced numbers over a specified between two limits. 

Syntax:numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

Example:

Python
importnumpyasnpnp.linspace(3.5,10,3,dtype=np.int32)

Output:

array([ 3, 6, 10], dtype=int32)

5.numpy.empty():This function create a new array of given shape and type without initializing value.

Syntax: numpy.empty(shape, dtype=float, order='C')

Example:

Python
importnumpyasnpnp.empty([4,3],dtype=np.int32,order='f')

Output:

n6
numpy.empty()

6.numpy.ones():This function is used to get a new array of given shape and type filled with ones (1).

Syntax: numpy.ones(shape, dtype=None, order='C')

Example:

Python
importnumpyasnpnp.ones([4,3],dtype=np.int32,order='f')

Output:

n7
numpy.ones()

7.numpy.zeros():This function is used to get a new array of given shape and type filled with zeros (0). 

Syntax: numpy.ones(shape, dtype=None)

Example:

Python
importnumpyasnpnp.zeros([4,3],dtype=np.int32,order='f')

Output:

n8
numpy.zeros()

As we move forward NumPy will remain an important tool for efficient data manipulation and mastering its core concepts will helps us to solve more complex computational challenges in the future.


Improve
Article Tags :
Practice Tags :

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