python 2.1.1 (2)
python 2.1.1 (2)
In Python, data types can be primitive types like int, float, or str, and they can also be
collections such as list, tuple, and dict. However, when working with large datasets or
performing heavy computations, Python's built-in data types aren't as efficient. NumPy
arrays are more optimized and allow operations on numerical data with great speed.
NumPy arrays have a fixed size for each element and store elements of the same data type.
This makes them more efficient for numerical operations compared to Python's built-in data
types like lists.
Example:
import numpy as np
print(int_array, int_array.dtype)
print(float_array, float_array.dtype)
print(bool_array, bool_array.dtype)
Output:
[1 2 3] int32
[1.1 2.2 3.3] float64
[ True False True] bool
Here, we created arrays of different types (int32, float64, bool) using the dtype
parameter.
2. The Basics of NumPy Arrays
NumPy arrays are the core data structure in NumPy. They are efficient, multidimensional
containers that can store elements of the same type. These arrays are faster and require less
memory than standard Python lists.
Example:
import numpy as np
# 1D Array (Vector)
arr_1d = np.array([1, 2, 3, 4, 5])
print("1D Array:", arr_1d)
# 2D Array (Matrix)
arr_2d = np.array([[1, 2], [3, 4], [5, 6]])
print("2D Array:\n", arr_2d)
# 3D Array
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print("3D Array:\n", arr_3d)
Output:
1D Array: [1 2 3 4 5]
2D Array:
[[1 2]
[3 4]
[5 6]]
3D Array:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
Universal Functions (ufuncs) are functions that operate element-wise on NumPy arrays.
They are highly optimized and allow operations to be performed on the entire array at once,
instead of looping over elements individually.
# Element-wise addition
result_add = np.add(arr1, arr2)
print("Addition:", result_add)
# Element-wise multiplication
result_mul = np.multiply(arr1, arr2)
print("Multiplication:", result_mul)
Output:
Here, we applied addition, multiplication, and square root operations element-wise to the
arrays using the corresponding universal functions.
NumPy provides several aggregation functions like np.min(), np.max(), np.sum(), and
np.mean() to perform quick calculations on the entire array or along specific axes
(rows/columns).
Example of Aggregation:
import numpy as np
Output:
Min: 1
Max: 5
Sum: 15
Mean: 3.0
Here, we calculated the minimum, maximum, sum, and mean of the array.
Broadcasting:
Example of Broadcasting:
import numpy as np
Output:
In this case, arr2 (which is a scalar) is broadcasted to each element of arr1, so the operation
is performed element-wise.
You can compare arrays element-wise, resulting in a boolean array, and apply logical
operations.
Output:
6. Fancy Indexing
Fancy indexing allows you to index arrays using other arrays (or lists), rather than just
integers. This technique provides more flexibility.
import numpy as np
Output:
7. Sorting Arrays
Sorting arrays is a common operation. NumPy provides the np.sort() function for sorting
arrays.
Example of Sorting:
Output:
Sorted Array: [1 2 3 4 5]
import numpy as np
Output:
Structured Array:
[('Alice', 25) ('Bob', 30) ('Charlie', 35)]
In this example, we created a structured array where each element has two fields: name (a
string of 10 characters) and age (an integer).
Sure! Below are 15 easier theory-based questions on NumPy concepts that students
can answer with concise explanations. These questions are designed to test basic
understanding and are more approachable for students:
Explain the concept of NumPy data types (dtypes). What are the common dtypes used in NumPy
arrays?
What are NumPy arrays, and how do they differ from Python lists in terms of performance and
functionality?
Explain how the aggregation functions np.min(), np.max(), and np.mean() are used on NumPy
arrays. Provide examples.
Describe what fancy indexing in NumPy is and give an example of how it is used to access
elements in an array.
Explain how the np.sort() function is used to sort a NumPy array. What is the default sorting
order?
Compare NumPy arrays and Python lists. Discuss the advantages of using NumPy arrays for
numerical computations.
What are the key differences between a 1D, 2D, and 3D NumPy array? Provide examples of each.