EXPERIMEN2
EXPERIMEN2
AIM:-
Write a program to demonstrate basic array characteristics using
numpy.
THEORY:-
Basic Characteristics of Arrays
1. Array Definition:
An array is a grid of values, all of the same type, indexed by a tuple of non-
negative integers. Arrays in NumPy are more versatile and efficient than
Python's built-in lists due to their fixed size and type homogeneity.
2. Dimensionality:
o 1D Array: A single row of elements (vector).
o 2D Array: A table of elements with rows and columns (matrix).
o 3D Array and Beyond: Arrays with more than two dimensions, often
used in advanced applications like image processing and machine
learning.
3. Array Properties:
o Shape: The dimensions of the array (e.g., rows and columns for 2D
arrays).
o Size: The total number of elements in the array.
o Data Type (dtype): The type of elements stored in the array (e.g., int,
float).
o Memory Efficiency: Arrays use less memory compared to Python lists
as they store elements of the same type.
4. Array Creation:
Arrays can be created using the numpy.array() function from Python
sequences like lists. Special arrays like zeros, ones, and identity matrices are
created using functions like numpy.zeros(), numpy.ones(), and numpy.eye().
6. Reshaping:
Arrays can be reshaped to change their structure without altering their data
using the reshape() method.
7. Broadcasting:
NumPy allows arithmetic operations between arrays of different shapes
through broadcasting, expanding the smaller array to match the dimensions
of the larger one during the operation.
8. Arithmetic Operations:
NumPy supports element-wise operations like addition, subtraction,
multiplication, and division. These operations are faster than loops in
Python due to NumPy’s optimized C-based backend.
CODE:-
import numpy as np
# 1. Creating arrays
# 1D Array
array_1d = np.array([10, 20, 30, 40, 50])
print("1D Array:", array_1d)
# 2D Array
array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("\n2D Array:\n", array_2d)
# 3D Array
array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print("\n3D Array:\n", array_3d)
# 2. Array properties
print("\nArray Properties:")
print("Shape of 1D Array:", array_1d.shape)
print("Shape of 2D Array:", array_2d.shape)
print("Shape of 3D Array:", array_3d.shape)
print("Size of 1D Array:", array_1d.size)
print("Size of 2D Array:", array_2d.size)
print("Size of 3D Array:", array_3d.size)
print("Data type of elements in 1D Array:", array_1d.dtype)
# 3. Accessing elements
print("\nAccessing Elements:")
print("First element of 1D Array:", array_1d[0])
print("Element at [1, 2] in 2D Array:", array_2d[1, 2])
print("Element at [1, 0, 1] in 3D Array:", array_3d[1, 0, 1])
# 4. Slicing arrays
print("\nSlicing:")
print("First two elements of 1D Array:", array_1d[:2])
print("First row of 2D Array:", array_2d[0, :])
print("First column of 2D Array:", array_2d[:, 0])
# 5. Reshaping arrays
reshaped_array = array_1d.reshape(5, 1)
print("\nReshaped 1D Array to 2D:\n", reshaped_array)
# 6. Arithmetic operations
array_add = array_2d + 10
array_mul = array_2d * 2
print("\nArithmetic Operations:")
print("Adding 10 to 2D Array:\n", array_add)
print("Multiplying 2D Array by 2:\n", array_mul)
# 7. Broadcasting
broadcast_result = array_2d + np.array([1, 2, 3])
print("\nBroadcasting Example (Adding [1, 2, 3] to 2D Array):\n",
broadcast_result)
# 8. Creating special arrays
zeros_array = np.zeros((3, 3))
ones_array = np.ones((2, 4))
identity_array = np.eye(3)
print("\nSpecial Arrays:")
print("3x3 Array of Zeros:\n", zeros_array)
print("2x4 Array of Ones:\n", ones_array)
print("3x3 Identity Matrix:\n", identity_array)
OUTPUT:-