Functions of numpy arrays
In NumPy, the numpy.zeros() function is used to create an array filled with zeros of a specified shape. This function is commonly used when you need to initialize an array with zeros before populating it with actual data. The syntax of the numpy.zeros() function is as follows.
numpy.zeros(shape, dtype=float, order='C')
- shape: The shape of the array you want to create. It can be an integer or a tuple of integers specifying the dimensions of the array.
- dtype (optional): The data type of the elements in the array. It is set to float by default. You can choose from various data types like int, float, complex, etc.
- order (optional): The memory layout of the array. It can be either 'C' (C-style) or 'F' (Fortran-style). 'C' means row-major order, and 'F' means column-major order.
Now, let's look at some examples of using
1: Create a 1D array with 5 elements filled with zeros (default data type is float).numpy.zeros()
import numpy as np zeros_array = np.zeros(5) print(zeros_array) # Output: [0. 0. 0. 0. 0.]
2: Create a 2D array with shape (3, 4) filled with zeros.
import numpy as np zeros_array = np.zeros((3, 4)) print(zeros_array) # Output:
# [[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]
3: Create a 3D array with shape (2, 3, 2) filled with zeros and data type set to int.
import numpy as np
zeros_array = np.zeros((2, 3, 2), dtype=int)
print(zeros_array)
# Output: # [[[0 0] # [0 0] # [0 0]] # # [[0 0] # [0 0] # [0 0]]]
4: Create a 2D array with shape (2, 3) filled with zeros in Fortran-style order.
import numpy as np zeros_array = np.zeros((2, 3), order='F') print(zeros_array)
# Output:
# [[0. 0. 0.]# [0. 0. 0.]]
In NumPy, the numpy.ones() function is used to create an array filled with ones of a specified shape. This function is similar to numpy.zeros(), but instead of zeros, it populates the array with ones. The syntax of the numpy.ones() function is as follows
numpy.ones(shape, dtype=float, order='C')
- shape: The shape of the array you want to create. It can be an integer or a tuple of integers specifying the dimensions of the array.
- dtype (optional): The data type of the elements in the array. It is set to float by default. You can choose from various data types like int, float, complex, etc.
- order (optional): The memory layout of the array. It can be either 'C' (C-style) or 'F' (Fortran-style). 'C' means row-major order, and 'F' means column-major order.
Now, let's look at some examples of using numpy.ones().
1: Create a 1D array with 5 elements filled with ones (default data type is float).
import numpy as np
ones_array = np.ones(5) print(ones_array) # Output: [1. 1. 1. 1. 1.]
2: Create a 2D array with shape (3, 4) filled with ones.
import numpy as np
ones_array = np.ones((3, 4)) print(ones_array) # Output:
# [[1. 1. 1. 1.]
# [1. 1. 1. 1.]
# [1. 1. 1. 1.]]
numpy.identity(): This function creates a square identity matrix of the given size, where all the diagonal elements are 1 and all other elements are 0. The syntax of the numpy.identity() function is as follows.
numpy.identity(n, dtype=None)
Parameters:
- n: The number of rows (and columns) in the square identity matrix.
- dtype (optional): The data type of the elements in the identity matrix. It is set to None by default, which means it will be inferred based on the other parameters.
import numpy as np
identity_matrix = np.identity(3) print(identity_matrix)Output:
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
numpy.linspace(): Create an array with evenly spaced values over a specified range.
import numpy as np
# Creating an array with 5 values from 0 to 1 (inclusive)
my_array = np.linspace(0, 1, 5)
print(my_array) # Output: [0. 0.25 0.5 0.75 1. ]
numpy.random.rand(): Create an array of random values from a uniform distribution.
import numpy as np
# Creating a 1x5 array of random values between 0 and 1
my_array = np.random.rand(1, 5)
print(my_array) # Output: [[0.42166506 0.32613859 0.93923835 0.54435295 0.67768108]]
numpy.random.randn(): Create an array of random values from a standard normal distribution.
import numpy as np
# Creating a 2x3 array of random values from the standard normal distribution
my_array = np.random.randn(2, 3)
print(my_array)
# Output:
# [[-0.41972706 0.49224303 0.86585742]
# [-0.63676498 -1.30393558 -0.53657815]]
numpy.reshape(): Change the shape of an array without changing its data.
import numpy as np
# Creating a 1D array
my_array = np.arange(1, 7)
# Reshaping the array into a 2x3 matrix
reshaped_array = my_array.reshape(2, 3)
print(reshaped_array)
# Output:
# [[1 2 3]
# [4 5 6]]
numpy.sum(): Compute the sum of array elements.
import numpy as np
# Creating a 1D array
my_array = np.array([1, 2, 3, 4, 5])
# Calculating the sum of the array elements
sum_result = np.sum(my_array)
print(sum_result) # Output: 15
numpy.mean(): Compute the mean of array elements.
import numpy as np
# Creating a 1D array
my_array = np.array([1, 2, 3, 4, 5])
# Calculating the mean of the array elements
mean_result = np.mean(my_array)
print(mean_result) # Output: 3.0
No comments:
Post a Comment