Python Program
Python Program
NumPy arrays and Python lists are both used to store collections of items, but they
have some key differences. Here are the main distinctions between NumPy arrays and
Python lists:
1. Homogeneity:
NumPy Array: Homogeneous, meaning all elements must be of the same data
type.
List: Heterogeneous, allowing elements of different data types in the same list.
2. Performance:
NumPy Array: Offers better performance for numerical operations due to its
homogeneous nature and optimized operations.
List: May have slower performance for numerical operations compared to
NumPy arrays.
3. Size:
NumPy Array: Generally more memory-efficient than lists, especially for large
datasets.
List: May consume more memory than NumPy arrays.
4. Operations:
NumPy Array: Supports element-wise operations without the need for explicit
loops.
List: Requires explicit loops for element-wise operations.
5. Functionality:
NumPy Array: Provides a wide range of mathematical functions and operations
optimized for arrays.
List: Offers basic list operations, but lacks the extensive functionality provided by
NumPy for numerical computations.
6. Syntax:
NumPy Array: The syntax for array operations is often more concise and
readable.
List: Requires more explicit syntax for numerical operations.
Example:
pythonCopy code
# NumPy Array
import numpy as np
numpy_array = np.array([1, 2, 3, 4, 5])
result = numpy_array * 2
# Python List
python_list = [1, 2, 3, 4, 5]
result_list = [x * 2 for x in python_list]
Execution time
Ar2 = np.array([[1,2,3,4],[1,2,3,4]])
Print(ar2)
Print(ar2.ndim)----to know dimension
ar_zero = np.zeros(4)
ar_zero1 = np.zeros((3,4))
ar_one = np.ones(4)
ar_one1 = np.ones((3,4))
ar_empty = np.empty(4)
ar_empty1 = np.empty((3,4))
ar_range = np.arange(4)
ar_dia = np.eye(3)
print(ar_dia)
ar_dia = np.eye((3,5))
linspace
ar_lin = np.linspace(1,10,num=5)
print(ar_lin)
Rand()
var = np.random.rand(4)
var1 = np.random.rand(2,5)
randn( )
var = np.random.randn(5)
var1 = np.random.rand(2,5)
ranf( )
Installation of TOR browser