We use np.power() and np.square() to quickly raise numbers or arrays of numbers to a power. This helps us do math easily on many numbers at once.
np.power() and np.square() in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NumPy
np.power(base, exponent) np.square(x)
np.power() takes two inputs: the base and the exponent.
np.square() is a shortcut to square numbers (raise to power 2).
Examples
NumPy
np.power(3, 2)
NumPy
np.power([1, 2, 3], 3)
NumPy
np.square(4)NumPy
np.square([2, 5, 7])
Sample Program
This program creates an array of numbers. It then cubes each number using np.power() and squares each number using np.square(). Finally, it prints all results.
NumPy
import numpy as np # Using np.power to cube numbers numbers = np.array([2, 3, 4]) cubed = np.power(numbers, 3) # Using np.square to square numbers squared = np.square(numbers) print('Original numbers:', numbers) print('Cubed:', cubed) print('Squared:', squared)
Important Notes
np.power() can raise numbers to any power, not just integers.
np.square() is faster and simpler when you only need to square numbers.
Both functions work element-wise on arrays, so they apply the operation to each number separately.
Summary
np.power() raises numbers or arrays to any power you choose.
np.square() is a quick way to square numbers or arrays.
Both help you do math on many numbers easily and quickly.
Practice
1. What does the
np.square() function do in NumPy?easy
Solution
Step 1: Understand the purpose of np.square()
The functionnp.square()takes each element in an array and raises it to the power of 2.Step 2: Compare with other options
It does not calculate square roots, multiply arrays, or sum squares; it only squares each element.Final Answer:
It raises each element of an array to the power of 2. -> Option AQuick Check:
np.square(x) = x² [OK]
Hint: Remember: square means power of 2, not root or sum [OK]
Common Mistakes:
- Confusing square with square root
- Thinking it sums squares instead of element-wise operation
- Mixing with multiplication of arrays
2. Which of the following is the correct syntax to square all elements in a NumPy array
arr using np.power()?easy
Solution
Step 1: Understand np.power() syntax
The functionnp.power(base, exponent)raises each element inbaseto theexponent.Step 2: Apply to square elements
To square elements ofarr, usenp.power(arr, 2). Other options misuse the order or miss the exponent.Final Answer:
np.power(arr, 2) -> Option CQuick Check:
np.power(arr, 2) = arr squared [OK]
Hint: np.power(base, exponent) order matters: base first [OK]
Common Mistakes:
- Swapping base and exponent
- Omitting the exponent argument
- Using the array as exponent incorrectly
3. What is the output of the following code?
import numpy as np arr = np.array([1, 2, 3]) result = np.power(arr, 3) print(result)
medium
Solution
Step 1: Understand np.power(arr, 3)
This raises each element ofarrto the power of 3.Step 2: Calculate each element
1³=1, 2³=8, 3³=27, so the result is [1, 8, 27].Final Answer:
[1 8 27] -> Option BQuick Check:
np.power([1,2,3],3) = [1,8,27] [OK]
Hint: Power 3 means cube each element [OK]
Common Mistakes:
- Calculating square instead of cube
- Adding elements instead of powering
- Confusing element-wise with sum
4. Identify the error in the following code snippet:
import numpy as np arr = np.array([2, 4, 6]) result = np.square(arr, 2) print(result)
medium
Solution
Step 1: Check np.square() function signature
np.square()accepts only one argument: the array or number to square.Step 2: Analyze the code error
The code passes two arguments, which causes a TypeError.Final Answer:
np.square() takes only one argument, but two were given. -> Option AQuick Check:
np.square(x) needs 1 argument [OK]
Hint: np.square() needs exactly one argument [OK]
Common Mistakes:
- Passing extra arguments to np.square()
- Thinking np.square() needs base and exponent
- Confusing np.square() with np.power()
5. You have a NumPy array
data = np.array([1, -2, 3, -4]). You want to create a new array where each element is squared, but negative values should be squared and then multiplied by -1 to keep their sign effect. Which code correctly achieves this?hard
Solution
Step 1: Understand the requirement
Negative values should be squared and then multiplied by -1; positive values just squared.Step 2: Analyze each option
np.where(data < 0, -np.square(data), np.square(data)) usesnp.whereto apply-np.square()for negatives andnp.square()for positives, matching the requirement exactly.Final Answer:
np.where(data < 0, -np.square(data), np.square(data)) -> Option DQuick Check:
Conditional square with sign handled = np.where(data < 0, -np.square(data), np.square(data)) [OK]
Hint: Use np.where for conditionally applying functions [OK]
Common Mistakes:
- Multiplying sign before squaring
- Using np.sign(data) directly without condition
- Confusing absolute value with sign handling
