NumPy Library and Function
NumPy Library and Function
NumPy Library and Function
import numpy
print(arr)
NumPy as np
NumPy is usually imported under the np alias.
alias: In Python alias are an alternate name for referring to the same
thing.
import numpy as np
print(arr)
Create a NumPy ndarray Object
print(arr)
print(type(arr))
Output
[1 2 3 4 5]
<class 'numpy.ndarray'>
Dimensions in Arrays
We can also pass a collection object into the array routine to create the
equivalent n-dimensional array. The syntax is given below.
>>> numpy.array(object, dtype = None, copy = True, order = None, su
bok = False, ndmin = 0)
The parameters are described in the following table.
SN Parameter Description
1 object It represents the collection object. It can be a list, tuple, dictionary,
set, etc.
2 dtype We can change the data type of the array elements by changing this
option to the specified type. The default is none.
The returned array will be base class array by default. We can change
5 subok this to make the subclasses passes through by setting this option to
true.
import numpy as np
print(arr)
print(type(arr))
Output
[1 2 3 4 5]
<class 'numpy.ndarray'>
Dimensions of Array
0-D Arrays
0-D arrays, or Scalars, are the elements in an array. Each value in an
array is a 0-D array.
import numpy as np
arr = np.array(42)
print(arr)
Output
42
1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.
These are the most common and basic arrays.
import numpy as np
print(arr)
Output
[1 2 3 4 5]
2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.
These are often used to represent matrix or 2nd order tensors.
import numpy as np
print(arr)
[[1 2 3]
[4 5 6]]
3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D array.
import numpy as np
print(arr)
Output
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
Check Number of Dimensions?
NumPy Arrays provides the ndim attribute that returns an integer that tells
us how many dimensions the array have.
import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim) Output
print(d.ndim)
0
1
2
3
Higher Dimensional Arrays
An array can have any number of dimensions.
When the array is created, you can define the number of dimensions by using the ndmin argument.
import numpy as np
print(arr)
print('number of dimensions :', arr.ndim)
Output:
Data type of the array 1 : int32
Data type of array 2 : float64
Integer
Yype Bytes Range
np.int8 1 -128 to 127
np.int16 2 -32768 to 32767
np.int32 4 -2147483648 to 2147483647
np.int64 8 -9223372036854775808 to
9223372036854775807
Unsigned integers
Unsigned integers are similar to normal integers, but they can only hold non-zero values. Here are the
available types:
In the example below, we are using numpy.linspace() function to create three arrays
(array1, array2, and array3) with specified ranges and configurations.
The "array1" is created with 10 evenly spaced values from 0 to 5, inclusive.
The "array2 consists of 5 values ranging from 1 to just under 2, excluding the endpoint.
The "array3" is created with 5 values from 0 to 10, and returns both the array and the step size
between consecutive values −
import numpy as np # Creating an array of 10 evenly spaced
values from 0 to 5
array1 = np.linspace(0, 5, num=10)
print("array1:",array1)
array2 = np.linspace(1, 2, num=5, endpoint=False)
print("array2:",array2) # Creating an array and returning the
step value
array3, step = np.linspace(0, 10, num=5, retstep=True)
print("array3:",array3)
print("Step size:", step)
1: [0. 0.55555556 1.11111111 1.66666667 2.22222222 2.77777778 3.3333
44444 5. ]
2: [1. 1.2 1.4 1.6 1.8]
3: [ 0. 2.5 5. 7.5 10. ]
size: 2.5
random.rand() Function
Alternatively, we can use the numpy.random.rand() function for creating an array by specifying
the dimensions of the array as parameters.
Unlike numpy.zeros() function and numpy.ones() function, which initialize array elements to zero
and one respectively, the numpy.empty() function does not initialize the elements. Instead, it
allocates the memory required for the array without setting any values.
In the following example, we are using the numpy.full() function to create a 2-dimensional array with
dimensions 2x3, filled entirely with the value 5 −
Output
[[5 5 5]
[5 5 5]]
Array Manipulation
Changing Shape
In NumPy, to change shape is to alter the shape of arrays without changing their data −
Output
The original array: [0 1 2 3 4 5 6 7]
The modified array:
[[0 1]
[2 3]
[4 5]
[6 7]]
The Numpy ndarray.flat Attribute which returns a 1-D iterator over the
array.
This iterator allows us to iterate over the array as if it were flattened but it
does not create a new array.
Syntax
Here is the syntax of Numpy ndarray.flat() Attribute −
numpy.ndarray.flat
import numpy as np # Creating a 2D numpy array
array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Using flat to
1
iterate over elements
2
for item in array_2d.flat: 3
4
print(item)
5
6
import numpy as np # Creating a 2D numpy array
array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Modifying elements using flat
iterator
Output
[[ 2 4 6]
[ 8 10 12]]
import numpy as np # Creating a 3D numpy array
array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
# Accessing specific elements using flat
print(array_3d.flat[0]) # First element
print(array_3d.flat[5]) # Sixth element
print(array_3d.flat[-1]) # Last element
Output
1
6
8
The Numpy ndarray.flatten()method which is used to return a new 1-
D array that is a copy of the original array which is flattened.
The ndarray.flat attribute which is an iterator
and ndarray.flatten() which creates a new array.
Syntax
The syntax for the Numpy ndarray.flatten function is as follows −
ndarray.flatten(order='C')
Parameter
This function takes a single parameter i.e. 'C' which is row major which
is by default.
We can assign 'F': column major, 'A': flatten in column-major order, if a
is Fortran contiguous in memory, row-major order otherwise 'K': flatten
a in the order the elements occur in the memory
Example 1
import numpy as np # Creating a 2D numpy array
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
# Flattening the array using the default order ('C')
flattened_array = array_2d.flatten()
print("Original 2D array:")
print(array_2d)
print("\n Flattened array (row-major order):")
print(flattened_array)
Output
Original 2D array:
[[1 2 3]
[4 5 6]]
Flattened array (row-major order):
[1 2 3 4 5 6]
Example 2
Here in this example we show how a 3D array is flattened into a 1D
array in column-major order which yields [1, 5, 3, 7, 2, 6, 4, 8] −
import numpy as np # Creating a 3D numpy array
array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
# Flattening the array using the 'F' order
flattened_array = array_3d.flatten(order='F')
print("Original 3D array:")
print(array_3d)
print("\nFlattened array (column-major order):")
print(flattened_array) Output
Original 3D array:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
Flattened array (column-major order):
[1 5 3 7 2 6 4 8]
Example 3
Below example shows flattening a Fortran-contiguous array using the 'A'
order which results in column-major order flattening, giving [1, 4, 2, 5, 3,
6] −
Sr.N
Array & Description
o.
1 concatenate Joins a sequence of arrays along an existing axis
2 stack Joins a sequence of arrays along a new axis
3 hstack Stacks arrays in sequence horizontally (column wise)
4 vstack Stacks arrays in sequence vertically (row wise)
The Numpy Concatenate() Function is used to join a sequence of
arrays along an existing axis. This function takes a tuple or list of arrays
to concatenate and an optional axis parameter that specifies the axis
along which the arrays will be joined.
numpy.concatenate((a1, a2, ...),axis=0,out=None,dtype=None, casting="same_kind")
Parameters
Following are the parameters of the Numpy concatenate() function −
•a1, a2, ...: array_like : These are the arrays to be concatenated. They must
have the same shape, except in the dimension corresponding to axis.
•axis(int, optional): The axis along which the arrays will be joined. The default
is 0. If axis is None, the arrays are flattened before use.
•out(ndarray, optional): If provided, the destination to place the result. It must
have the right shape to hold the output.
•dtype(data-type, optional): The type to use in the output array. By default,
the dtype will be inferred from the inputs.
•casting({'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional): Controls
what kind of data casting may occur. Defaults to 'same_kind'.
Example 1
Following is the example of Numpy Concatenate() Function which
demonstrates concatenating two 1-D arrays along the default axis
(axis=0), resulting in a single 1-D array −
Output
Result of concatenation along axis 1:
[[1 2 5 6]
[3 4 7 8]]
The Numpy stack() Function is used to join a sequence of arrays along
a new axis. All input arrays must have the same shape.
This function is useful for combining arrays of the same shape along a
specified dimension while creating a new dimension in the output array.
For example, stacking two 2D arrays along a new axis creates a 3D
array.
Parameters
•arrays: These are the arrays you want to stack. All arrays must have the same shape.
•axis: The axis along which the arrays will be stacked. It must be between 0 and the number of
dimensions of the input arrays.
•out: If provided, the destination to place the result. It should be of the appropriate shape and
dtype.
•dtype: If provided, the dtype to use for the resulting array.
•casting: Controls what kind of data casting may occur.
Example 1
Following is the basic example of using Numpy stack() Function. In this
example the two 1-D arrays are stacked along a new axis, resulting in a 2-
D array
import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
stacked_array = np.stack((array1, array2))
print("Stacked Array:\n", stacked_array)
Output
Stacked Array:
[[1 2 3]
[4 5 6]]
Example 2
This is another example of using the stack() function, here in this example
two 2-D arrays are stacked along axis 1 which results in a 3-D array −
import numpy as np
array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])
stacked_array = np.stack((array1, array2), axis=1)
print("Stacked Array:\n", stacked_array)
Output
Stacked Array:
[[[1 2]
[5 6]]
[[3 4]
[7 8]]]
Example 3
Here in ths example two 3-D arrays are stacked along axis 2, resulting in
a 4-D array −
import numpy as np
array1 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
array2 = np.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]])
stacked_array = np.stack((array1, array2), axis=2)
print("Stacked Array:\n", stacked_array) Stacked Array:
[[[[ 1 2]
[ 9 10]]
[[ 3 4]
[11 12]]]
[[[ 5 6]
[13 14]]
[[ 7 8]
[15 16]]]]
The Numpy hstack() Function which is used to horizontally stack arrays. It
combines a sequence of arrays along their horizontal axis i.e. axis 1.
All input arrays must have the same number of rows or compatible shapes
for broadcasting and the function returns a new array with columns
concatenated.
import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
result = np.vstack((array1,
array2)) print(result)
Output
[[1 2 3]
[4 5 6]]
Example 2
Below is the example which shows how a 1-D array can be stacked with a
2-D array, provided they are compatible in shape along the axis being
stacked −
import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([[4, 5, 6], [7, 8, 9]])
result = np.vstack((array1, array2))
print(result)
Output
[[1 2 3]
[4 5 6]
[7 8 9]]
Splitting Arrays
Splitting arrays in NumPy splits arrays into smaller arrays along specified axes
Sr.N
Array & Description
o.
1 split Splits an array into multiple sub-arrays
Parameters
Following are the parameters of the Numpy hsplit() Function −
•ary: The input array to be split.
•indices_or_sections(int or 1-D array): This is either an integer
indicating the number of equal division sections to split the array into
along the second axis i.e. axis=1 or a list of indices where the array is
Example 1
Following is the example of Numpy hsplit() function which splits the
array into 2 sub-arrays along the columns −
import numpy as np
arr = np.arange(12).reshape(3, 4)
print("Original Array:")
print(arr) split_arrays =
np.hsplit(arr, 2) print("\nSplit
Arrays:")
for a in split_arrays:
print(a)
Original Array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Split Arrays:
[[0 1]
[4 5]
[8 9]]
[[ 2 3]
[ 6 7]
[10 11]]
Example 2
Here in this example we show how numpy hsplit() function handles indices that
exceed array dimensions by creating empty arrays −
import numpy as np
arr = np.arange(10).reshape(2, 5)
print("Original Array:")
print(arr)
split_arrays = np.hsplit(arr, [2, 6]) # Using indices exceeding array size
print("\nSplit Arrays:")
for a in split_arrays:
print(a)
Original Array:
[[0 1 2 3 4]
[5 6 7 8 9]]
Split Arrays:
[[0 1]
[5 6]]
[[2 3 4]
[7 8 9]]
[]
Example 3
In this example hsplit() function divides the original array into two
equal-width sections horizontally. Each section is then printed to
show the result of the horizontal split −
import numpy as np # Create a 4x4 array with values from 0 to 15
a = np.arange(16).reshape(4, 4)
print('First array:')
print(a)
print('\n') # Split the array horizontally into 2 equal parts
b = np.hsplit(a, 2)
print('Horizontal splitting:')
for i, section in enumerate(b):
print('Section {}:'.format(i + 1))
print(section)
print('\n')
First array:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Horizontal splitting:
Section 1:
[[ 0 1]
[ 4 5]
[ 8 9]
[12 13]]
Section 2:
[[ 2 3]
[ 6 7]
[10 11]
[14 15]]
The Numpy vsplit() function is used to split an array into multiple sub-
arrays along the vertical axis (axis 0). It is used for dividing a 2D array
into smaller arrays row-wise. This function requires two arguments one is
the array to split and the other one is number of sections to create.
For example, if we have a 2D array with shape (6, 4) and use
np.vsplit(array, 3), it will return three sub-arrays, each with shape (2, 4).
numpy.vsplit(array, indices_or_sections)
Parameters
Following is the syntax of Numpy vsplit() function −
•ary(array_like): The input array to be split.
•indices_or_sections: This parameter can be an integer indicating the number of equally shaped
sub-arrays to create along the vertical axis or a 1-D array of sorted integers specifying the split
points.
Example 1
Following is the example of Numpy vsplit() function in which the
array arr is split vertically into 2 equally shaped sub-arrays −
import numpy as np
# Arrange elements from 0 to 19
a = np.arrange(20)
print("\n Array is:\n ",a)
print("\n a[15]=",a[15])
# a[start:stop:step]
print("\n a[-8:17:1] = ",a[-8:17:1])
print("\n a[10:] = ",a[10:])
Array is:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
a[15]= 15
a[-8:17:1] = [12 13 14 15 16]
a[10:] = [10 11 12 13 14 15 16 17 18 19]
Advanced indexing
NumPy Advanced indexing returns a copy of data rather than a view
of it. Advanced indexing is of two types integer and Boolean.
Advanced indexing in the NumPy array allows you to access and
manipulate complex patterns of the data.
Advanced indexing is triggered when obj is :
•an ndarray of type integer or Boolean
•or a tuple with at least one sequence object
•is a non tuple sequence object
Types of Advanced Indexing
There are two types of Advanced Indexing in NumPy array indexing:
•Purely integer indexing
•Boolean integer indexing
Purely integer array indexing
Purely integer array indexing allows us to access elements from an
ndarray (N-dimensional array) using integers.
When integers are used for indexing. Each element of the first
dimension is paired with the element of the second dimension. So the
index of the elements in this case are (0,0),(1,0),(2,1) and the
corresponding elements are selected.
Example: Using purely integer array indexing
[1 3 6]
Boolean Indexing
This indexing has some boolean expressions as the index.
Those elements are returned which satisfy that Boolean expression. It is
used for filtering the desired element values.
[80 100]
Example 2: Using boolean indexing on NumPy array to find numbers
whose sum row is 10
string = "GFG"
ch_iterator = iter(string)
print(next(ch_iterator))
print(next(ch_iterator))
print(next(ch_iterator))
G
F
G
Numpy | Iterating
Over Array
NumPy package contains an iterator object numpy.nditer. It is an efficient
multidimensional iterator object using which it is possible to iterate over
an array. Each element of an array is visited using Python’s standard
Iterator interface.
# Python program for
# iterating over array
import numpy as geek
# creating an array using arrange method
a = geek.arange(12)
# shape array with 3 rows and 4 columns
a = a.reshape(3,4)
print('Original array is:')
print(a)
print()
print('Modified array is:')
# iterating an array
for x in geek.nditer(a):
print(x)
Original array is:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Modified array is:
0
1
2
3
4
5
6
7
8
9
10
11
Controlling Iteration Order:
There are times when it is important to visit the elements of an array in a
specific order, irrespective of the layout of the elements in memory. The
nditer object provides an order parameter to control this aspect of
iteration.
The default, having the behavior described above, is order=’K’ to keep
the existing order. This can be overridden with order=’C’ for C order and
order=’F’ for Fortran order.
# Python program for
# iterating over array
# using particular order
import numpy as geek
# creating an array using arrange method
a = geek.arange(12)
# shape array with 3 rows and 4 columns
a = a.reshape(3,4)
print('Original array is:')
print(a)
print()
print('Modified array in C-style order:')
# iterating an array in a given # order
for x in geek.nditer(a, order = 'C'):
print(x)
Original array is:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Modified array is:
0
1
2
3
4
5
6
7
8
9
10
11
# Python program for
# iterating over array
# using particular order
import numpy as geek
# creating an array using arrange method
a = geek.arange(0,60,5)
# shape array with 3 rows and 4 columns
a = a.reshape(3,4)
print('Original array is:')
print(a)
print()
print('Modified array in F-style order:')
# iterating an array in a given order
for x in geek.nditer(a, order = 'F’):
print(x)
Original array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Modified array in F-style order:
0
20
40
5
25
45
10
30
50
15
35
55
Modifying Array Values:
The nditer object has another optional parameter called op_flags. Its
default value is read-only, but can be set to read-write or write-only mode.
This will enable modifying array elements using this iterator.
# Python program for
# modifying array values
import numpy as geek
# creating an array using arrange method
a = geek.arange(12)
# shape array with 3 rows and 4 columns
a = a.reshape(3,4)
print('Original array is:')
print(a)
# modifying array values
for x in geek.nditer(a, op_flags = ['readwrite']):
x[...] = 5*x
print('Modified array is:')
Original array is:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Modified array is:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
Broadcasting Iteration:
If two arrays are broadcastable, a combined nditer object is able to
iterate upon them concurrently. Assuming that an array a has dimension
3X4, and there is another array b of dimension 1X4, the iterator of
following type is used (array b is broadcast to size of a).
# Python program for
# iterating array
import numpy as geek
# creating an array using arrange method
a = geek.arange(12)
# shape array with 3 rows and 4 columns
a = a.reshape(3,4)
print('First array is:')
print(a)
print()
# Creating second array using array method
print('Second array is:')
b = geek.array([5, 6, 7, 8], dtype = int)
print(b)
print()
print('Modified array is:')
for x,y in geek.nditer([a,b]):
print("%d:%d" % (x,y))
First array is:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Second array is:
[5 6 7 8]
Modified array is:
0:5
1:6
2:7
3:8
4:5
5:6
6:7
7:8
8:5
9:6
10:7
Boolean Array in NumPy
import numpy as np
arr = np.array([1, 0, 1, 0, 0, 1, 0])
print(f'Original Array: {arr}')
bool_arr = np.array(arr, dtype='bool')
print(f'Boolean Array: {bool_arr}')
Original Array:
[1 0 1 0 0 1 0]
Boolean Array:
[ True False True False False True False]
import numpy as np
arr = np.array([5, None, 1, 25, -10, 0, 'A'])
print(f'Original Array: {arr}')
bool_arr = np.array(arr, dtype='bool')
print(f'Boolean Array: {bool_arr}')
Original Array:
[5 None 1 25 -10 0 'A’]
Boolean Array: [ True False True True True False True]
NumPy where() Multiple Conditions( Conditional Array )
When we pass the NumPy array to the numpy.where() it will return the indices of NumPy
array elements
You can use the np.where() function with multiple conditions to modify the existing
array based on those conditions.
In the below example, you can use the | (bitwise OR) operator to filter elements from a
NumPy array.
import numpy as np
# Create a numpy array
arr = np.array([5, 10, 15, 20, 25])
# Multiple conditions using | operator #
Use numpy.where() function
arr2 = arr[np.where((arr >5) | (arr % 2 == 0))]
print("Get selected array elements:\n", arr2)
# Output:
# Get selected array elements: # [10 15 20 25]
Use where() Multiple Conditions With the logical_and()
import numpy as np
# Create a numpy array
arr = np.array([5, 10, 15, 20, 25])
# Use numpy.where() multiple conditions with the .logical_or()
arr2 = arr[np.where(np.logical_or(arr >5, arr % 2 == 0))]
print("Get selected array element:", arr2)
# Output:
# Get selected array element: [ 10 15 20 25]
Numpy | Linear Algebra
The Linear Algebra module of NumPy offers various methods to apply
linear algebra on any numpy array.
One can find:
•rank, determinant, trace, etc. of an array.
•eigen values of matrices
•matrix and vector products (dot, inner, outer,etc. product), matrix
exponentiation
•solve linear or tensor equations and much more!
# Importing numpy as np
import numpy as np
A = np.array([[6, 1, 1],
[4, -2, 5],
[2, 8, 7]])
# Rank of a matrix
print("Rank of A:", np.linalg.matrix_rank(A))
# Trace of matrix A
print("\nTrace of A:", np.trace(A))
# Determinant of a matrix
print("\nDeterminant of A:", np.linalg.det(A))
# Inverse of matrix A
print("\nInverse of A:\n", np.linalg.inv(A))
print("\nMatrix A raised to power 3:\n",np.linalg.matrix_power(A, 3))
Rank of A: 3
Trace of A: 11
Determinant of A: -306.0
Inverse of A: [[ 0.17647059 -0.00326797 -0.02287582]
[ 0.05882353 -0.13071895 0.08496732]
[-0.11764706 0.1503268 0.05228758]]
Matrix A r...
NumPy Linear Algebra
Numpy provides the following functions to perform the different
algebraic calculations on the input data.
SN Function Definition
The dot product is calculated as: [100 * 10 + 200 * 12, 100 * 20 + 200 * 21] [23*10+12*12,
23*20 + 12*21]
numpy.vdot() function
This function is used to calculate the dot product of two vectors. It can be defined as the
sum of the product of corresponding elements of multi-dimensional arrays.
import numpy as np
a = np.array([[100,200],[23,12]])
b = np.array([[10,20],[12,21]])
vdot = np.vdot(a,b)
print(vdot)
5528
np.vdot(a,b) = 100 *10 + 200 * 20 + 23 * 12 + 12 * 21 = 5528
numpy.inner() function
This function returns the sum of the product of inner elements of the one-dimensional
array. For n-dimensional arrays, it returns the sum of the product of elements over the
last axis.
import numpy as np
a = np.array([1,2,3,4,5,6])
b = np.array([23,23,12,2,1,2])
nner = np.inner(a,b)
print(inner)
130
NumPy outer() Function
The outer() function in NumPy computes the outer product of two arrays,
which is the product of all possible pairs of their entries.
import numpy as np
array1 = np.array([1, 3, 5])
array2 = np.array([2, 4, 6])
# outer() to perform outer multiplication
result = np.outer(array1, array2)
print(result)
[[ 2 4 6]
[ 6 12 18]
[10 20 30]]
NumPy solve() Function
In NumPy, we use the solve() function to solve a system of linear
equations.
For a given matrix A and a vector b, solve(A, b) finds the solution
vector x that satisfies the equation Ax = b.
import numpy as np
# define the coefficient matrix A
A = np.array([[2, 4],
[6, 8]])
# define the constant vector b
b = np.array([5, 6])
# solve the system of linear equations Ax = b
x = np.linalg.solve(A, b)
print(x)
# Output: [-2. 2.25]
Python provides built-in functions for creating, writing, and reading files.
Two types of files can be handled in Python, normal text files and binary
files (written in binary language, 0s, and 1s).
•Text files: In this type of file, Each line of text is terminated with a
special character called EOL (End of Line), which is the new line character
(‘\n’) in Python by default.
•Binary files: In this type of file, there is no terminator for a line, and the
data is stored after converting it into machine-understandable binary
language.