Numpy_Introduction .ipynb - Colab https://colab.research.google.com/drive/1bKqhraW4...
5+7
12
Numpy - numerical python foundation library for scienti�c computing in python. It provides
DS and high performing functions that the basic package of python cannot provide.
Numpy speci�es ndimensional array i.e. ndarray
import numpy as np
import numpy as mp
mp.array([1,2,3])
array([1, 2, 3])
ndarray- multidimensional, homogeneous(the same type and the same size) array with a
predetermined number of items.
dtype object - data type Each ndarray is associated with only one type of dtype.
• NumPy arrays is that their size is �xed, that is, once you de�ne their size at the time
of creation, it remains unchanged.
• To de�ne a new ndarray is to use the array() function, passing a Python list containing
the elements to be included in it as an argument
a=np.array([1,2,3,4,5])
array([1, 2, 3, 4, 5])
##check the type of array
type(a)
numpy.ndarray
##To know the associated dtype to the newly created ndarray, you have to use the dtype
a.dtype
dtype('int64')
Note: The result of dtype, shape, and other a�ributes can vary among di�erent operating
systems and Python distributions.
1 of 7 03/09/25, 11:11
Numpy_Introduction .ipynb - Colab https://colab.research.google.com/drive/1bKqhraW4...
systems and Python distributions.
b=np.array([1.2,1.3,1.4,1.5])
b
array([1.2, 1.3, 1.4, 1.5])
type(b)
numpy.ndarray
b.dtype
dtype('float64')
## ndim attribute for getting the axes
a.ndim
## size attribute to determine the array length
a.size
## the shape attribute to get its shape
a.shape
(5,)
import numpy as np
d=np.array(['abcs','bdsfdfddd'])
d
array(['abcs', 'bdsfdfddd'], dtype='<U9')
d.dtype
dtype('<U9')
d.dtype.name
'str288'
type(d)
numpy.ndarray
2 of 7 03/09/25, 11:11
Numpy_Introduction .ipynb - Colab https://colab.research.google.com/drive/1bKqhraW4...
d.dtype
dtype('<U9')
d.size
d.shape
(2,)
d.itemsize
36
De�ne a two-dimensional array 2x2:
c= np.array([[1.3, 2.4],[0.3, 4.1]])
array([[1.3, 2.4],
[0.3, 4.1]])
type(c)
numpy.ndarray
c.dtype
dtype('float64')
c.ndim
c.size
c.shape
##This array has rank 2, since it has two axes, each of length 2.
(2, 2)
3 of 7 03/09/25, 11:11
Numpy_Introduction .ipynb - Colab https://colab.research.google.com/drive/1bKqhraW4...
## It defines the size in bytes of each item in the array,
a.itemsize
## data is the buffer containing the actual elements of the array
a.data
<memory at 0x7b3ffdb24e80>
Array using array(), using list, tuple and sequences of tuples
c=np.array([[1,2,3],[4,5,6]])
c
array([[1, 2, 3],
[4, 5, 6]])
d=np.array(((1,2,3,4),(3,4,5,6),(5,6,7,8)))
e=np.array([(1,2,3),[4,5,6],(7,8,9)])
f=np.array(([1,2,3],[4,5,6],[7,8,9]),dtype=float)
array([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
## define the complex numbers
g = np.array([[1, 2, 3],[4, 5, 6]], dtype=complex)
g
array([[1.+0.j, 2.+0.j, 3.+0.j],
[4.+0.j, 5.+0.j, 6.+0.j]])
Intrinsic Creation of an Array
zeros()- creates a full array of zeros with dimensions de�ned by the shape of the
argument.
ones() - creates an array full of ones in a very similar way.
Both functions create arrays with the �oat64 data type.
np.zeros((3,4))
array([[0., 0., 0., 0.],
4 of 7 03/09/25, 11:11
Numpy_Introduction .ipynb - Colab https://colab.research.google.com/drive/1bKqhraW4...
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
np.ones((4,3))
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
arange()- function generates arrays with numerical sequences that respond to particular
rules depending on the passed arguments.
import numpy as np
np.arange(0, 10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(4, 10)
array([4, 5, 6, 7, 8, 9])
##the third argument represent gap between one value and next one in sequence of value
np.arange(0, 12,2)
array([ 0, 2, 4, 6, 8, 10])
np.arange(0, 6, 0.6)
array([0. , 0.6, 1.2, 1.8, 2.4, 3. , 3.6, 4.2, 4.8, 5.4])
np.arange(0,5,0.6)
array([0. , 0.6, 1.2, 1.8, 2.4, 3. , 3.6, 4.2, 4.8])
To generate two-dimensional arrays, you can still continue to use the arange() function but
combined with the reshape() function. This function divides a linear array in di�erent parts
in the manner speci�ed by the shape argument.
np.arange(0, 12).reshape(3, 4)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
np.arange(0, 11).reshape(3, 4)
5 of 7 03/09/25, 11:11
Numpy_Introduction .ipynb - Colab https://colab.research.google.com/drive/1bKqhraW4...
---------------------------------------------------------------------------
ValueError Traceback (most recent call
last)
/tmp/ipython-input-3050367959.py in <cell line: 0>()
----> 1 np.arange(0, 11).reshape(3, 4)
ValueError: cannot reshape array of size 11 into shape (3,4)
linspace() - function has 3 parameter:
1st parameter : start value of sequence
2nd parameter : end value of sequence
3rd parameter: de�nes the number of elements into which you want the interval to be split.
np.linspace(0,10,5)
array([ 0. , 2.5, 5. , 7.5, 10. ])
np.linspace(0,10,4)
array([ 0. , 3.33333333, 6.66666667, 10. ])
np.linspace(0,10,3)
array([ 0., 5., 10.])
np.linspace(0,10,2)
array([ 0., 10.])
np.linspace(0,10,1)
array([0.])
random() - obtain arrays already containing values is to �ll them with random values. It will
generate an array with as many elements as speci�ed in the argument. This function is
present in numpy.random module.
np.random.random(3)
array([0.55264619, 0.74200089, 0.2122555 ])
np.random.random(3) ## it provides different values than previous cell
array([0.84073668, 0.57646003, 0.20500503])
6 of 7 03/09/25, 11:11
Numpy_Introduction .ipynb - Colab https://colab.research.google.com/drive/1bKqhraW4...
Start coding or generate with AI.
7 of 7 03/09/25, 11:11