Unit-VI: Data Analysis with Python

Lecture 1: Introduction to NumPy - Arrays, Data Types, and Attributes

Estimated: 30-45 minutes

1. NumPy Overview

NumPy (Numerical Python) is the fundamental package for scientific computing in Python. It provides:

  • N-dimensional array object (ndarray)
  • Mathematical functions for array operations
  • Tools for working with large datasets
import numpy as np  # Standard import convention

# Create a simple array
arr = np.array([1, 2, 3, 4, 5])
print("Array:", arr)
print("Type:", type(arr))
2. Creating Arrays

NumPy provides several ways to create arrays:

# From a list
arr1 = np.array([1, 2, 3, 4, 5])

# Multi-dimensional array
arr2 = np.array([[1, 2, 3], [4, 5, 6]])

# Using built-in functions
zeros = np.zeros(5)        # Array of zeros
ones = np.ones((2, 3))     # 2x3 array of ones
range_arr = np.arange(5)   # Similar to range()

print("Zeros:", zeros)
print("Ones array:\n", ones)
print("Range array:", range_arr)
3. Array Attributes

Important array attributes:

Attribute Description Example
ndim Number of dimensions arr.ndim
shape Dimensions as a tuple arr.shape
size Total number of elements arr.size
dtype Data type arr.dtype
arr = np.array([[1, 2, 3], [4, 5, 6]])

print("Dimensions:", arr.ndim)     # 2
print("Shape:", arr.shape)         # (2, 3)
print("Size:", arr.size)           # 6
print("Data type:", arr.dtype)     # int64
4. Data Types

NumPy supports various data types:

# Creating arrays with specific data types
int_array = np.array([1, 2, 3], dtype=np.int32)
float_array = np.array([1.0, 2.5, 3.7], dtype=np.float32)
bool_array = np.array([True, False, True])

# Type conversion
converted = int_array.astype(np.float64)

print("Integer array:", int_array.dtype)
print("Converted to float:", converted.dtype)
Common Data Types: int32, int64, float32, float64, bool_, string_
5. Basic Array Operations

Element-wise operations with arrays:

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Element-wise operations
print("Addition:", a + b)      # [5 7 9]
print("Multiplication:", a * 2) # [2 4 6]
print("Dot product:", np.dot(a, b))  # 32

# Universal functions (ufuncs)
print("Square root:", np.sqrt(a))    # [1.         1.41421356 1.73205081]
print("Exponential:", np.exp(a))     # [ 2.71828183  7.3890561  20.08553692]
6. Practice Exercise

Create a 3x3 matrix with values ranging from 1 to 9, then:

  1. Calculate the sum of all elements
  2. Find the mean of each row
  3. Multiply it by a scalar value