numpy
April 19, 2025
[1]: import numpy
[2]: import xyz
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[2], line 1
----> 1 import xyz
ModuleNotFoundError: No module named 'xyz'
[ ]: ModuleNotFoundError:
1. Spelling
2. Case sensitive
3. Install this module
[ ]: 1. Go to website pypi
2. Search for numpy
3. Find the code pip install numpy
4. Use ! before the command and install in your code
[4]: !pip install numpy
Requirement already satisfied: numpy in c:\users\admin\anaconda3\lib\site-
packages (1.26.4)
[ ]: # ! opens terminal( heart of your code ) from there it executes
[ ]: to pass parameters to a function we use: ()
list : []
numpy.array() # we should pass a parameter called list/tuple
[5]: import numpy
[6]: ar1 = numpy.array([1,2,3,4,5])
ar1
1
[6]: array([1, 2, 3, 4, 5])
[7]: list1 = [1,2,3,4,5]
[i**2 for i in list1]
[7]: [1, 4, 9, 16, 25]
[8]: ar1 ** 2
[8]: array([ 1, 4, 9, 16, 25])
[ ]:
[9]: import numpy as np
ar1 = np.array([1,2,3,4,5])
[12]: ar1 **3
[12]: array([ 1, 8, 27, 64, 125], dtype=int32)
[ ]: int32/64: integer
float32/64: float
[11]: ar1.dtype # data type
# array 1 has integers --> int32
[11]: dtype('int32')
[13]: np.zeros([3,2]) # shape : [3,2] --> 3 rows and 2 columns
# creates an array filled with zeros
# 0. --> 0
[13]: array([[0., 0.],
[0., 0.],
[0., 0.]])
[15]: help(np.zeros)
Help on built-in function zeros in module numpy:
zeros(…)
zeros(shape, dtype=float, order='C', *, like=None)
Return a new array of given shape and type, filled with zeros.
Parameters
----------
shape : int or tuple of ints
2
Shape of the new array, e.g., ``(2, 3)`` or ``2``.
dtype : data-type, optional
The desired data-type for the array, e.g., `numpy.int8`. Default is
`numpy.float64`.
order : {'C', 'F'}, optional, default: 'C'
Whether to store multi-dimensional data in row-major
(C-style) or column-major (Fortran-style) order in
memory.
like : array_like, optional
Reference object to allow the creation of arrays which are not
NumPy arrays. If an array-like passed in as ``like`` supports
the ``__array_function__`` protocol, the result will be defined
by it. In this case, it ensures the creation of an array object
compatible with that passed in via this argument.
.. versionadded:: 1.20.0
Returns
-------
out : ndarray
Array of zeros with the given shape, dtype, and order.
See Also
--------
zeros_like : Return an array of zeros with shape and type of input.
empty : Return a new uninitialized array.
ones : Return a new array setting values to one.
full : Return a new array of given shape filled with value.
Examples
--------
>>> np.zeros(5)
array([ 0., 0., 0., 0., 0.])
>>> np.zeros((5,), dtype=int)
array([0, 0, 0, 0, 0])
>>> np.zeros((2, 1))
array([[ 0.],
[ 0.]])
>>> s = (2,2)
>>> np.zeros(s)
array([[ 0., 0.],
[ 0., 0.]])
>>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype
array([(0, 0), (0, 0)],
3
dtype=[('x', '<i4'), ('y', '<i4')])
[26]: np.zeros([3,2])
[26]: array([[0., 0.],
[0., 0.],
[0., 0.]])
[25]: np.random.random([3,2]) # shape --> [3,2] --> 3 rows and 2 columns
# creates a random array with values from 0 to 1
[25]: array([[0.65644003, 0.74442908],
[0.54061825, 0.61104814],
[0.51349282, 0.24717018]])
[ ]:
Array Initialization
[28]: np.full([3,2], 10) # 2 parameters --> shape , value
# create an array with 3,2 and fill the entire array with value 10
[28]: array([[10, 10],
[10, 10],
[10, 10]])
[29]: list(range(1,5))
[29]: [1, 2, 3, 4]
[30]: list(range(1,10,2))
[30]: [1, 3, 5, 7, 9]
[31]: np.arange(1,10,2) # start , end-1, step
# array + range --> output will be in array
[31]: array([1, 3, 5, 7, 9])
[37]: help(np.linspace)
Help on _ArrayFunctionDispatcher in module numpy:
linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
Return evenly spaced numbers over a specified interval.
Returns `num` evenly spaced samples, calculated over the
4
interval [`start`, `stop`].
The endpoint of the interval can optionally be excluded.
.. versionchanged:: 1.16.0
Non-scalar `start` and `stop` are now supported.
.. versionchanged:: 1.20.0
Values are rounded towards ``-inf`` instead of ``0`` when an
integer ``dtype`` is specified. The old behavior can
still be obtained with ``np.linspace(start, stop, num).astype(int)``
Parameters
----------
start : array_like
The starting value of the sequence.
stop : array_like
The end value of the sequence, unless `endpoint` is set to False.
In that case, the sequence consists of all but the last of ``num + 1``
evenly spaced samples, so that `stop` is excluded. Note that the step
size changes when `endpoint` is False.
num : int, optional
Number of samples to generate. Default is 50. Must be non-negative.
endpoint : bool, optional
If True, `stop` is the last sample. Otherwise, it is not included.
Default is True.
retstep : bool, optional
If True, return (`samples`, `step`), where `step` is the spacing
between samples.
dtype : dtype, optional
The type of the output array. If `dtype` is not given, the data type
is inferred from `start` and `stop`. The inferred dtype will never be
an integer; `float` is chosen even if the arguments would produce an
array of integers.
.. versionadded:: 1.9.0
axis : int, optional
The axis in the result to store the samples. Relevant only if start
or stop are array-like. By default (0), the samples will be along a
new axis inserted at the beginning. Use -1 to get an axis at the end.
.. versionadded:: 1.16.0
Returns
-------
samples : ndarray
There are `num` equally spaced samples in the closed interval
5
``[start, stop]`` or the half-open interval ``[start, stop)``
(depending on whether `endpoint` is True or False).
step : float, optional
Only returned if `retstep` is True
Size of spacing between samples.
See Also
--------
arange : Similar to `linspace`, but uses a step size (instead of the
number of samples).
geomspace : Similar to `linspace`, but with numbers spaced evenly on a log
scale (a geometric progression).
logspace : Similar to `geomspace`, but with the end points specified as
logarithms.
:ref:`how-to-partition`
Examples
--------
>>> np.linspace(2.0, 3.0, num=5)
array([2. , 2.25, 2.5 , 2.75, 3. ])
>>> np.linspace(2.0, 3.0, num=5, endpoint=False)
array([2. , 2.2, 2.4, 2.6, 2.8])
>>> np.linspace(2.0, 3.0, num=5, retstep=True)
(array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
Graphical illustration:
>>> import matplotlib.pyplot as plt
>>> N = 8
>>> y = np.zeros(N)
>>> x1 = np.linspace(0, 10, N, endpoint=True)
>>> x2 = np.linspace(0, 10, N, endpoint=False)
>>> plt.plot(x1, y, 'o')
[<matplotlib.lines.Line2D object at 0x…>]
>>> plt.plot(x2, y + 0.5, 'o')
[<matplotlib.lines.Line2D object at 0x…>]
>>> plt.ylim([-0.5, 1])
(-0.5, 1)
>>> plt.show()
[34]: np.linspace(1,20,11) # start , end , no.of digits
# Generate 11 numbers between 1 to 20 with equal interval( difference )
[34]: array([ 1. , 2.9, 4.8, 6.7, 8.6, 10.5, 12.4, 14.3, 16.2, 18.1, 20. ])
6
[35]: 2.9-1
[35]: 1.9
[36]: 14.3 - 12.4
[36]: 1.9000000000000004
[ ]:
[ ]:
[ ]:
[ ]:
[ ]: