This document introduces Numpy for scientific computing, covering array creation, indexing, slicing, vector operations, universal functions, and plotting. Key concepts include speed, memory efficiency, and practical data science applications.
Numpy is a foundational Python library for scientific computing, offering efficient array creation, indexing, slicing, and vector operations. This document covers basic usage, attributes, universal functions, and practical examples for data science and mathematical analysis.
Numpy provides powerful tools for scientific computing, including ND arrays for storing and manipulating data. Arrays are fixed in size and contain elements of the same type, enabling fast and memory-efficient operations.
Arrays can be created by casting Python lists:
1import numpy as np
2lst = [1, 2, 3, 4, 5]
3a = np.array(lst)
Key attributes include:
| Attribute | Description |
|---|---|
| size | Number of elements |
| ndim | Number of dimensions |
| shape | Size in each dimension |
| dtype | Data type of elements |
Elements are accessed and modified using integer indices and square brackets:
1a[0] = 100 # Change first element
2a[4] = 0 # Change fifth element
Slicing extracts subarrays:
1d = a[1:4] # Elements from index 1 to 3
Numpy supports fast vector operations, including addition, subtraction, scalar multiplication, Hadamard product, and dot product:
1u = np.array([1, 0])
2v = np.array([0, 1])
3z = u + v # Vector addition
4z = u - v # Vector subtraction
5y = np.array([2, 3])
6y2 = 2 * y # Scalar multiplication
7z = u * v # Hadamard product
8result = np.dot(u, v) # Dot product
Universal functions operate on ND arrays element-wise:
1mean_val = a.mean() # Mean of elements
2max_val = a.max() # Maximum value
Broadcasting allows operations between arrays and scalars:
1u = np.array([1, 2, 3])
2u_plus_5 = u + 5 # Adds 5 to each element
Numpy provides mathematical functions and tools for plotting:
1x = np.linspace(0, 2 * np.pi, 100)
2y = np.sin(x)
To plot functions (in Jupyter):
1import matplotlib.pyplot as plt
2%matplotlib inline
3plt.plot(x, y)
Numpy streamlines scientific computing in Python, offering efficient array operations, mathematical functions, and plotting capabilities. Its speed and memory efficiency make it essential for data science and numerical analysis.
(2) Numpy arrays are designed for fast, memory-efficient scientific computing with homogeneous data types.
(2) Slicing excludes the element at the last index, similar to Python lists.
(1) Vector addition can be performed with one line of code in Numpy.
| Concept | Description |
|---|---|
| A. Broadcasting | 1. Element-wise multiplication of two arrays |
| B. Hadamard product | 2. Adds a scalar to each array element |
| C. Dot product | 3. Returns a single number representing similarity |
| D. Universal function | 4. Operates element-wise on ND arrays |
A-2, B-1, C-3, D-4.
The mean() method in Numpy returns the average value of all elements in an array.
True. The mean() method calculates the average value of array elements.
(1) The correct syntax is np.array([1, 2, 3]).