You are on page 1of 9

Practice | GeeksforGeeks | A computer science portal for geeks https://www.geeksforgeeks.org/batch/fork-data-science/track/da-python-maste...

 Prev

Next 

1 sur 9 11/04/2024 09:11


Practice | GeeksforGeeks | A computer science portal for geeks https://www.geeksforgeeks.org/batch/fork-data-science/track/da-python-maste...

Getting started with Numpy

Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array


object, and tools for working with these arrays. It is the fundamental package for scienti�c computing with Python.
Besides its obvious scienti�c uses, Numpy can also be used as an e�cient multi-dimensional container of generic
data.

If you do not have numpy installed in your system �rst you need to install it.

Python3

!pip install numpy

 Prev A list in Python is a linear data structure that can hold heterogeneous elements they do not require to be declared
and are �exible to shrink and grow. On the other hand, an array is a data structure which can hold homogeneous
Next  elements, arrays are implemented in Python using the NumPy library. Arrays require less memory than list.

The similarity between an array and a list is that the elements of both array and a list can be identi�ed by its index
value.

2 sur 9 11/04/2024 09:11


Practice | GeeksforGeeks | A computer science portal for geeks https://www.geeksforgeeks.org/batch/fork-data-science/track/da-python-maste...

Now let us convert a list into an array

Python3

import numpy as np

l=[2,3,4,5]
print(l)
print(type(l))
print()
arr=np.array(l)
print(arr)
print(type(arr))

Output:

[2, 3, 4, 5]
<class 'list'>

[2 3 4 5]
<class 'numpy.ndarray'>

So we have �rst printed the list and then converted the list to an array and displayed that as well
 Prev

Next 

numpy.ndarray.ndim() function return the number of dimensions of an array.

3 sur 9 11/04/2024 09:11


Practice | GeeksforGeeks | A computer science portal for geeks https://www.geeksforgeeks.org/batch/fork-data-science/track/da-python-maste...

Syntax : numpy.ndarray.ndim(arr)

Parameters :
arr : [array_like] Input array. If it is not already an ndarray, a conversion is attempted.

Return : [int] Return the number of dimensions in arr.

Python3

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

Output:

1
2

So we can the dimension of array a is 1 dimension and the array b is 2 dimention

 Prev

Next 

Now we have few more functions such as np.array.shape, np.array.size and np.array.dtype

shape will basically return us the total number of rows and columns of the array in a tuple format (number of rows,
number of columns)

4 sur 9 11/04/2024 09:11


Practice | GeeksforGeeks | A computer science portal for geeks https://www.geeksforgeeks.org/batch/fork-data-science/track/da-python-maste...

number of columns)

size will tell us the total number of elements or values present in the array

dtype will tell us the data type of values which the array is containing

Let's see the implementation of all these

Python3

b=np.array([[1,2,3,4],[5,6,7,8]])
print(b.shape)
print(b.size)
print(b.dtype)

Output:

(2, 4)
8
int32
 Prev
• So we have 2 rows and 4 columns in the array b
Next 
• We have 8 elements present in the array
• The data type of the elements present in the array is int

5 sur 9 11/04/2024 09:11


Practice | GeeksforGeeks | A computer science portal for geeks https://www.geeksforgeeks.org/batch/fork-data-science/track/da-python-maste...

The numpy.zeros() function returns a new array of given shape and type, with zeros. Syntax:

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

Parameters :

shape : integer or sequence of integers


order : C_contiguous or F_contiguous
C-contiguous order in memory(last index varies the fastest)
C order means that operating row-rise on the array will be slightly quicker
FORTRAN-contiguous order in memory (first index varies the fastest).
F order means that column-wise operations will be faster.
dtype : [optional, float(byDeafult)] Data type of returned array.

Returns :

ndarray of zeros having given shape, order and datatype.

Python3

# Python Program illustrating


# numpy.zeros method
 Prev

import numpy as geek


Next 
b = geek.zeros(2, dtype = int)
print("Matrix b : \n", b)

a = geek.zeros([2, 2], dtype = int)

6 sur 9 11/04/2024 09:11


Practice | GeeksforGeeks | A computer science portal for geeks https://www.geeksforgeeks.org/batch/fork-data-science/track/da-python-maste...

print("\nMatrix a : \n", a)

c = geek.zeros([3, 3])
print("\nMatrix c : \n", c)

Output :

Matrix b :
[0 0]

Matrix a :
[[0 0]
[0 0]]

Matrix c :
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]

The numpy.ones() function returns a new array of given shape and type, with ones.
 Prev

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

Parameters :

shape : integer or sequence of integers

7 sur 9 11/04/2024 09:11


Practice | GeeksforGeeks | A computer science portal for geeks https://www.geeksforgeeks.org/batch/fork-data-science/track/da-python-maste...

shape : integer or sequence of integers


order : C_contiguous or F_contiguous
C-contiguous order in memory(last index varies the fastest)
C order means that operating row-rise on the array will be slightly quicker
FORTRAN-contiguous order in memory (first index varies the fastest).
F order means that column-wise operations will be faster.
dtype : [optional, float(byDefault)] Data type of returned array.

Returns :

ndarray of ones having given shape, order and datatype.

Python3

# Python Program illustrating


# numpy.ones method

import numpy as geek

b = geek.ones(2, dtype = int)


print("Matrix b : \n", b)

a = geek.ones([2, 2], dtype = int)


print("\nMatrix a : \n", a)

 Prev c = geek.ones([3, 3])


print("\nMatrix c : \n", c)
Next 

8 sur 9 11/04/2024 09:11


Practice | GeeksforGeeks | A computer science portal for geeks https://www.geeksforgeeks.org/batch/fork-data-science/track/da-python-maste...

Output :

Matrix b :
[1 1]

Matrix a :
[[1 1]
[1 1]]

Matrix c :
[[ 1. 1. 1.]
[ 1. 1. 1.]
[ 1. 1. 1.]]

 Prev

Next 

9 sur 9 11/04/2024 09:11

You might also like