Beginning Numpy


NumPy is a Python library that stands for Numerical Python. It is a powerful library that provides support for multidimensional arrays, mathematical functions, linear algebra operations, random number generation, and much more. NumPy is the fundamental package for scientific computing with Python. In this article, we will introduce you to the basics of NumPy using Python.
Installing NumPy
Before using NumPy, you need to install it. You can install NumPy using pip, which is a package manager for Python. To install NumPy using pip, open the terminal or command prompt and type the following command:
Importing NumPy
To use NumPy in your Python program, you need to import it. You can import NumPy using the following command:

The above command imports NumPy and assigns it the name 'np'. This is the most common way of importing NumPy.
Creating NumPy Arrays
NumPy provides support for multidimensional arrays. You can create a NumPy array using the array() function. The array() function takes a list as an argument and returns a NumPy array. For example:


The above code creates a one-dimensional NumPy array.
You can also create a two-dimensional NumPy array by passing a list of lists to the array() function. For example:




Explanation:
  • We define the coefficients of the linear equations using NumPy arrays. In this case, A is a 2x2 matrix that represents the coefficients of the variables x and y, and b is a 1D array that represents the constant terms on the right-hand side of the equations.
  • We then use the np.linalg.solve() function to solve the system of equations. This function takes two arguments: the coefficient matrix A and the constant vector b, and returns the solution vector x that satisfies the equations.
  • Finally, we print the solution to the equations. In this case, the solution is x = 1 and y = 1.
Note that the np.linalg.solve() function can also be used to solve larger systems of linear equations. In general, the function takes as input an nxn coefficient matrix A and an n-dimensional constant vector b, and returns the n-dimensional solution vector x that satisfies the equations.




 


pip install numpy

import numpy as np


import numpy as np
a = np.array([1, 2, 3])
print(a)

Output:
[1 2 3]

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)

Output:


[[1 2 3]
 [4 5 6]]

Example that generates arrays with zeros, ones, and random values using NumPy in Python:
import numpy as np
# Generate a one-dimensional array with five elements, all set to zero
a = np.zeros(5)
print("Array with zeros:")
print(a)
# Generate a two-dimensional array with three rows and two columns, all set to zero
b = np.zeros((3, 2))
print("Two-dimensional array with zeros:")
print(b)
# Generate a one-dimensional array with five elements, all set to one
c = np.ones(5)
print("Array with ones:")
print(c)
# Generate a two-dimensional array with three rows and two columns, all set to one
d = np.ones((3, 2))
print("Two-dimensional array with ones:")
print(d)
# Generate a one-dimensional array with five random values between 0 and 1
e = np.random.random(5)
print("Array with random values between 0 and 1:")
print(e)
# Generate a two-dimensional array with three rows and two columns with random values between 0 and 1
f = np.random.rand(3, 2)
print("Two-dimensional array with random values between 0 and 1:")
print(f)
# Generate a two-dimensional array with three rows and two columns with random values from the standard normal distribution
g = np.random.randn(3, 2)
print("Two-dimensional array with random values from the standard normal distribution:")
print(g)



Example that explains some of the commonly used attributes of NumPy arrays in Python:
import numpy as np
# Create a 2D array
a = np.array([[1, 2, 3], [4, 5, 6]])
# Print the shape of the array
print("Shape of the array:")
print(a.shape)
# Print the number of dimensions of the array
print("Number of dimensions of the array:")
print(a.ndim)
# Print the data type of the array
print("Data type of the array:")
print(a.dtype)
# Print the size of the array (total number of elements)
print("Size of the array:")
print(a.size)
# Print the item size of the array (size of each element in bytes)
print("Item size of the array:")
print(a.itemsize)
# Print the strides of the array (number of bytes to step in each dimension)
print("Strides of the array:")
print(a.strides)
# Print the transpose of the array
print("Transpose of the array:")
print(a.T)

Output
Shape of the array:
(2, 3)
Number of dimensions of the array:
2
Data type of the array:
int64
Size of the array:
6
Item size of the array:
8
Strides of the array:
(24, 8)
Transpose of the array:
[[1 4]
 [2 5]
 [3 6]]

Reshaping

import numpy as np
# Create a 1D array of 10 elements
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# Reshape the array to a 2D matrix with 5 rows and 2 columns
b = a.reshape(5, 2)
# Reshape the array to a 3D matrix with 2 rows, 5 columns, and 1 depth
c = a.reshape(2, 5, 1)
# Reshape the array to a 2D matrix with 2 rows and -1 columns (automatically infer the number of columns)
d = a.reshape(2, -1)
# Reshape the array to a 2D matrix with -1 rows and 5 columns (automatically infer the number of rows)
e = a.reshape(-1, 5)
# Print all the reshaped arrays
print("Reshaped array b:")
print(b)
print("Reshaped array c:")
print(c)
print("Reshaped array d:")
print(d)
print("Reshaped array e:")
print(e)
Output

Reshaped array b:
[[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]]
Reshaped array c:
[[[ 1]
  [ 2]
  [ 3]
  [ 4]
  [ 5]]
 [[ 6]
  [ 7]
  [ 8]
  [ 9]
  [10]]]
Reshaped array d:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]
Reshaped array e:
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]


Arithmetic Operations
import numpy as np
# Create two 2D arrays of the same shape
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# Addition: element-wise addition of the two arrays
c = a + b
# Subtraction: element-wise subtraction of the two arrays
d = a - b
# Multiplication: element-wise multiplication of the two arrays
e = a * b
# Division: element-wise division of the two arrays
f = a / b
# Scalar multiplication: multiply each element of an array by a scalar value
g = a * 2
# Scalar addition: add a scalar value to each element of an array
h = a + 3
# Dot product: matrix multiplication of two arrays
i = np.dot(a, b)
# Print the results
print("Array a:")
print(a)
print("Array b:")
print(b)
print("Array a + b:")
print(c)
print("Array a - b:")
print(d)
print("Array a * b:")
print(e)
print("Array a / b:")
print(f)
print("Array a * 2:")
print(g)
print("Array a + 3:")
print(h)
print("Array a dot b:")
print(i)

Output
Array a:
[[1 2]
 [3 4]]
Array b:
[[5 6]
 [7 8]]
Array a + b:
[[ 6  8]
 [10 12]]
Array a - b:
[[-4 -4]
 [-4 -4]]
Array a * b:
[[ 5 12]
 [21 32]]
Array a / b:
[[0.2        0.33333333]
 [0.42857143 0.5       ]]
Array a * 2:
[[2 4]
 [6 8]]
Array a + 3:
[[4 5]
 [6 7]]
Array a dot b:
[[19 22]
 [43 50]]


Solving simultaneous equations
import numpy as np
# Define the coefficients of the linear equations
A = np.array([[2, 1], [1, 1]])
b = np.array([3, 2])
# Solve the system of equations
x = np.linalg.solve(A, b)
# Print the solution
print("The solution of the system of linear equations is:")
print("x =", x[0])
print("y =", x[1])

Output

The solution of the system of linear equations is:
x = 1.0
y = 1.0








Post a Comment

0 Comments

Me