Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Essential NumPy Operations for Python Data Manipulation

Tech 2

Generating Sequential Arrays with arange

seq_arr = np.arange(8)  # Generates [0, 8) as a half-open interval
print(seq_arr)
step_arr = np.arange(0, 8, 7)
print(step_arr)

Output:

[0 1 2 3 4 5 6 7]
[0 7]

Transposing and Reshaping ndarray Objects

# Create a one-dimensional array from 0 to 8
base_arr = np.arange(9)
# Reshape into a 3x3 two-dimensional array
reshaped_arr = base_arr.reshape((3, 3))
print(base_arr)
print(reshaped_arr)

# Transpose the matrix (swap rows and columns)
print(reshaped_arr.T)

# Perform matrix multiplication
print(np.dot(reshaped_arr, reshaped_arr.T))

# Working with multi-dimensional arrays
# Create a 3D array with shape (2,2,2)
three_d_arr = np.arange(8).reshape((2, 2, 2))
print(three_d_arr)
# Access element at position [1][1][0]
print(three_d_arr[1][1][0])

Output:

[0 1 2 3 4 5 6 7 8]
[[0 1 2]
 [3 4 5]
 [6 7 8]]
[[0 3 6]
 [1 4 7]
 [2 5 8]]
[[ 5 14 23]
 [14 50 86]
 [23 86 149]]
[[[0 1]
  [2 3]]
 [[4 5]
  [6 7]]]
6

Common Unary Functions in NumPy

Function Description
abs/fabs Compute absolute values; fabs is faster for non-negative numbers
sqrt Calculate square roots of elements
square Compute squares of elements
exp Calculate exponential values (e^x)
log, log10, log2, log1p Natural log (base e), base-10 log, base-2 log, and log(1+x)
sign Determine sign: 1 (positive), 0 (zero), -1 (negative)
ceil Compute ceiling values (smlalest integer ≥ element)
floor Compute floor values (largest integer ≤ element)
rint Round elmeents to nearest integers while preserving dtype
modf Return fractional and integer parts as separate arrays
isnan Return boolean array indicating NaN (Not a Number) values
isfinite, isinf Return boolean arrays to finite and infinite values

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.