0
0
Data Analysis Pythondata~5 mins

Array arithmetic (element-wise) in Data Analysis Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does element-wise arithmetic mean when working with arrays?
Element-wise arithmetic means performing operations on each pair of corresponding elements from two arrays separately, like adding or multiplying each element one by one.
Click to reveal answer
beginner
How do you add two arrays element-wise in Python using NumPy?
You simply use the + operator between two NumPy arrays of the same shape, for example: array1 + array2 adds each element of array1 to the corresponding element of array2.
Click to reveal answer
intermediate
What happens if you try element-wise operations on arrays of different shapes?
If the shapes are not compatible, NumPy will raise an error. But if shapes are compatible by broadcasting rules, it will perform element-wise operations by stretching the smaller array.
Click to reveal answer
beginner
Show an example of element-wise multiplication of two arrays.
Example:<br><code>import numpy as np<br>a = np.array([1, 2, 3])<br>b = np.array([4, 5, 6])<br>c = a * b  # c is array([4, 10, 18])</code>
Click to reveal answer
beginner
Why is element-wise array arithmetic useful in data science?
It allows fast and simple calculations on datasets, like scaling, combining, or transforming data without writing loops, making code cleaner and faster.
Click to reveal answer
What does the expression array1 + array2 do in NumPy if both arrays have the same shape?
AConcatenates the two arrays
BAdds each element of array1 to the corresponding element of array2
CMultiplies each element of array1 by the corresponding element of array2
DRaises an error
What will happen if you try to add arrays of shapes (3,) and (2,)?
ARaises a shape mismatch error
BElement-wise addition with broadcasting
CConcatenates the arrays
DPerforms dot product
Which operator is used for element-wise multiplication of two NumPy arrays?
A-
B+
C@
D*
If a = np.array([1, 2, 3]) and b = np.array([4, 5, 6]), what is a * b?
A[5, 7, 9]
BError
C[4, 10, 18]
D[1, 2, 3, 4, 5, 6]
Why is element-wise arithmetic preferred over loops in data science?
AIt is faster and more concise
BIt requires less memory
CIt is slower but easier to read
DIt avoids using arrays
Explain what element-wise arithmetic is and give an example using Python arrays.
Think about how you add or multiply numbers one by one in two lists.
You got /3 concepts.
    Describe what happens when you try to perform element-wise operations on arrays with incompatible shapes.
    Consider what happens if you try to add a list of 3 numbers to a list of 2 numbers.
    You got /3 concepts.