Machine Learning
39.1K subscribers
3.82K photos
32 videos
41 files
1.3K links
Machine learning insights, practical tutorials, and clear explanations for beginners and aspiring data scientists. Follow the channel for models, algorithms, coding guides, and real-world ML applications.

Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
import numpy as np
np.random.seed(42)
print(np.random.rand(2))
np.random.seed(42) # Resetting seed
print(np.random.rand(2)) # Same output as above


Python tip:
Create a deep copy of an array using .copy() to avoid unintended modifications.

import numpy as np
original = np.array([1, 2, 3])
copy = original.copy()
copy[0] = 100
print(original) # Output: [1 2 3] (original is unchanged)
print(copy) # Output: [100 2 3]


Python tip:
Using arr[index] for assignment modifies the original array (views, not copies, for slices).

import numpy as np
original = np.arange(5)
view = original[1:4]
view[0] = 99 # Modifies original[1]
print(original) # Output: [ 0 99 2 3 4]


Python tip:
Compare arrays element-wise using comparison operators, resulting in a boolean array.

import numpy as np
a = np.array([1, 2, 3])
b = np.array([1, 0, 4])
print(a == b) # Output: [ True False False]
print(a > b) # Output: [False True False]


Python tip:
Use boolean indexing to select elements based on a condition.

import numpy as np
arr = np.array([10, 25, 5, 30, 15])
filtered_arr = arr[arr > 20] # Select elements greater than 20
print(filtered_arr) # Output: [25 30]


Python tip:
Combine multiple boolean conditions using & (AND), | (OR), and ~ (NOT) for element-wise logical operations.

import numpy as np
arr = np.array([1, 6, 2, 7, 3, 8])
filtered = arr[(arr > 3) & (arr < 8)] # Elements greater than 3 AND less than 8
print(filtered) # Output: [6 7]


Python tip:
Use np.clip() to limit the values in an array to a specified range.

import numpy as np
arr = np.array([1, 10, 3, 15, 6])
clipped_arr = np.clip(arr, 3, 10) # Values below 3 become 3, above 10 become 10
print(clipped_arr) # Output: [ 3 10 3 10 6]


Python tip:
Round array elements to the nearest integer using np.round().

import numpy as np
arr = np.array([1.2, 2.7, 3.5, 4.1])
rounded_arr = np.round(arr)
print(rounded_arr) # Output: [1. 3. 4. 4.]


Python tip:
Use np.floor() to round down to the nearest integer.

import numpy as np
arr = np.array([1.2, 2.7, 3.5, 4.9])
floored_arr = np.floor(arr)
print(floored_arr) # Output: [1. 2. 3. 4.]


Python tip:
Use np.ceil() to round up to the nearest integer.

import numpy as np
arr = np.array([1.2, 2.7, 3.5, 4.1])
ceiled_arr = np.ceil(arr)
print(ceiled_arr) # Output: [2. 3. 4. 5.]


Python tip:
Calculate the absolute value of each element using np.abs().

import numpy as np
arr = np.array([-1, 5, -3, 0])
abs_arr = np.abs(arr)
print(abs_arr) # Output: [1 5 3 0]


Python tip:
Transpose a 2D array using the .T attribute or np.transpose().

import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
transposed_matrix = matrix.T
print(transposed_matrix)


Python tip:
Use np.isin() to check if elements of one array are present in another.

import numpy as np
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([2, 4, 6])
presence = np.isin(arr1, arr2)
print(presence) # Output: [False True False True False]


Python tip:
Use np.diff() to calculate the difference between consecutive elements.

import numpy as np
arr = np.array([1, 3, 7, 12, 10])
differences = np.diff(arr)
print(differences) # Output: [ 2 4 5 -2]


Python tip:
Load data from a text file into a NumPy array using np.loadtxt().

import numpy as np
# Create a dummy file for the example
with open("data.txt", "w") as f:
f.write("1 2 3\n")
f.write("4 5 6\n")

data = np.loadtxt("data.txt")
print(data)


Python tip:
Save a NumPy array to a text file using np.savetxt().
import numpy as np
arr = np.array([[10, 20], [30, 40]])
np.savetxt("output.txt", arr, fmt="%d", delimiter=",")
# Check output.txt, it will contain:
# 10,20
# 30,40


Python tip:
Save and load arrays in NumPy's native binary format (.npy) using np.save() and np.load().

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
np.save("my_array.npy", arr)
loaded_arr = np.load("my_array.npy")
print(loaded_arr)


Python tip:
Handle missing data using np.nan (Not a Number).

import numpy as np
arr_with_nan = np.array([1, 2, np.nan, 4])
print(arr_with_nan)


Python tip:
Check for NaN values using np.isnan().

import numpy as np
arr = np.array([1, 2, np.nan, 4])
is_nan_arr = np.isnan(arr)
print(is_nan_arr) # Output: [False False True False]


Python tip:
Use np.inf for infinity.

import numpy as np
result = 10 / np.inf
print(result) # Output: 0.0


Python tip:
Check for infinite values using np.isinf().

import numpy as np
arr = np.array([1, np.inf, -np.inf, 4])
is_inf_arr = np.isinf(arr)
print(is_inf_arr) # Output: [False True True False]


Python tip:
Replace NaN values using boolean indexing or np.nan_to_num().

import numpy as np
arr = np.array([1, 2, np.nan, 4, np.nan])
arr[np.isnan(arr)] = 0 # Replace NaN with 0
print(arr)


Python tip:
Check if all elements in a boolean array are true using np.all().

import numpy as np
bool_arr = np.array([True, True, True])
print(np.all(bool_arr)) # Output: True


Python tip:
Check if any element in a boolean array is true using np.any().

import numpy as np
bool_arr = np.array([False, False, True])
print(np.any(bool_arr)) # Output: True


Python tip:
Get the dimensions of an array by unpacking .shape.

import numpy as np
matrix = np.zeros((2, 3, 4))
d1, d2, d3 = matrix.shape
print(f"Dim 1: {d1}, Dim 2: {d2}, Dim 3: {d3}")


Python tip:
Use np.transpose() with a tuple of axes to reorder dimensions for multi-dimensional arrays.

import numpy as np
arr_3d = np.arange(24).reshape(2, 3, 4)
# Swap axis 0 and 2
transposed_arr = np.transpose(arr_3d, (2, 1, 0)) # (4, 3, 2)
print(transposed_arr.shape)


Python tip:
Generate a diagonal array from a 1D array or extract the diagonal from a 2D array using np.diag().

import numpy as np
arr_1d = np.array([1, 2, 3])
diag_matrix = np.diag(arr_1d)
print(diag_matrix)


Python tip:
Repeat elements of an array using np.repeat().

import numpy as np
arr = np.array([1, 2])
repeated_arr = np.repeat(arr, 3) # Each element repeated 3 times
print(repeated_arr) # Output: [1 1 1 2 2 2]


Python tip:
Repeat an entire array or tile it using np.tile().

import numpy as np
arr = np.array([[1, 2], [3, 4]])
tiled_arr = np.tile(arr, (2, 1)) # Repeat 2 times vertically, 1 time horizontally
print(tiled_arr)


Python tip:
Dot product of two 1D arrays or matrix multiplication of 2D arrays with np.dot().

import numpy as np
v1 = np.array([1, 2])
v2 = np.array([3, 4])
dot_product = np.dot(v1, v2)
print(dot_product) # Output: 11 (1*3 + 2*4)


Python tip:
Compute the outer product of two vectors using np.outer().

import numpy as np
v1 = np.array([1, 2])
v2 = np.array([3, 4, 5])
outer_product = np.outer(v1, v2)
print(outer_product)


Python tip:
Calculate the inverse of a matrix using np.linalg.inv().

import numpy as np
matrix = np.array([[1, 2], [3, 4]])
inverse = np.linalg.inv(matrix)
print(inverse)


Python tip:
Calculate the determinant of a matrix using np.linalg.det().
❀1
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
determinant = np.linalg.det(matrix)
print(determinant) # Output: -2.0


Python tip:
Solve a system of linear equations Ax = b using np.linalg.solve().

import numpy as np
A = np.array([[1, 2], [3, 5]])
b = np.array([1, 2])
x = np.linalg.solve(A, b)
print(x) # Output: [-1. 1.]
# Check: A @ x gives b


Python tip:
Compute the norm of a vector or matrix using np.linalg.norm().

import numpy as np
vec = np.array([3, 4])
norm = np.linalg.norm(vec) # Euclidean (L2) norm
print(norm) # Output: 5.0


Python tip:
Use np.meshgrid() to create coordinate matrices from coordinate vectors for plotting or functions of two variables.

import numpy as np
x = np.array([0, 1])
y = np.array([0, 1, 2])
X, Y = np.meshgrid(x, y)
print("X:\n", X)
print("Y:\n", Y)


Python tip:
Transform array elements using universal functions (ufuncs) for speed.

import numpy as np
arr = np.array([1, 2, 3])
print(np.sqrt(arr)) # Square root
print(np.exp(arr)) # Exponential


Python tip:
Apply a custom function to array elements using np.vectorize() (though slower than true ufuncs for complex operations).

import numpy as np
def my_func(x, y):
if x > y: return x
return y * 2

vfunc = np.vectorize(my_func)
a = np.array([1, 5, 3])
b = np.array([2, 3, 4])
print(vfunc(a, b)) # Output: [ 4 10 8]


Python tip:
Understand the difference between a view and a copy to avoid unexpected behavior. Slicing typically returns a view.

import numpy as np
original = np.arange(5)
view = original[1:4] # This is a view
view[:] = 0 # Modifies the original array
print(original) # Output: [0 0 0 0 4]


Python tip:
Use np.mean(axis=...), np.sum(axis=...) etc., to perform operations along specific dimensions.

import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
col_means = np.mean(matrix, axis=0) # Mean of each column
row_means = np.mean(matrix, axis=1) # Mean of each row
print(f"Column means: {col_means}")
print(f"Row means: {row_means}")


Python tip:
Convert an array to a list using .tolist().

import numpy as np
arr = np.array([1, 2, 3])
my_list = arr.tolist()
print(my_list)
print(type(my_list))


Python tip:
Create a 2D array (matrix) using nested lists.

import numpy as np
matrix = np.array([[1, 2], [3, 4]])
print(matrix)


Python tip:
Understand that NumPy arrays are homogeneous (all elements must be of the same data type).

import numpy as np
# If you try to mix types, NumPy will upcast if possible
arr = np.array([1, 2.5, 3])
print(arr.dtype) # Output: float64


Python tip:
Use dtype='object' if you genuinely need a heterogeneous array (but usually indicates a design flaw).

import numpy as np
hetero_arr = np.array([1, 'hello', 3.14], dtype='object')
print(hetero_arr)


Python tip:
Fill an array with random samples from a uniform distribution [0, 1) using np.random.random().

import numpy as np
random_uniform = np.random.random(size=(2, 2))
print(random_uniform)


Python tip:
Generate random samples from a standard normal distribution (mean 0, variance 1) using np.random.randn().

import numpy as np
random_normal = np.random.randn(2, 3) # 2x3 array
print(random_normal)


Python tip:
Randomly choose elements from an array using np.random.choice().

import numpy as np
my_array = np.array(['A', 'B', 'C', 'D'])
chosen_elements = np.random.choice(my_array, size=3, replace=False) # Choose 3 unique elements
print(chosen_elements)
Python tip:
Shuffle an array in-place using np.random.shuffle().

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
np.random.shuffle(arr)
print(arr)


Python tip:
Get the indices that would sort an array using np.argsort().

import numpy as np
arr = np.array([3, 1, 4, 1, 5])
sorted_indices = np.argsort(arr)
print(sorted_indices) # Output: [1 3 0 2 4] (indices of [1, 1, 3, 4, 5])


Python tip:
Use np.clip() to restrict values within a defined interval.

import numpy as np
data = np.array([-5, 0, 5, 10, 15])
clipped_data = np.clip(data, 0, 10)
print(clipped_data) # Output: [ 0 0 5 10 10]


Python tip:
Replace specific values in an array using np.where() or boolean indexing.

import numpy as np
arr = np.array([1, 2, 1, 3, 1, 4])
arr[arr == 1] = 99 # Replace all 1s with 99
print(arr) # Output: [99 2 99 3 99 4]


Python tip:
Use np.percentile() to calculate the q-th percentile of the data.

import numpy as np
data = np.arange(1, 11) # [1, 2, ..., 10]
p25 = np.percentile(data, 25)
p75 = np.percentile(data, 75)
print(f"25th percentile: {p25}") # Output: 3.25
print(f"75th percentile: {p75}") # Output: 7.75


Python tip:
Generate a histogram of non-negative integers using np.bincount().

import numpy as np
data = np.array([0, 1, 1, 3, 2, 1, 0])
counts = np.bincount(data) # Counts occurrences of each non-negative integer
print(counts) # Output: [2 3 1 1] (0 appeared 2 times, 1 appeared 3 times, etc.)


Python tip:
Use np.histogram() for general-purpose histograms over a range of values.

import numpy as np
data = np.array([1, 2, 2, 3, 4, 4, 4, 5])
hist, bin_edges = np.histogram(data, bins=3)
print(f"Histogram counts: {hist}")
print(f"Bin edges: {bin_edges}")


Python tip:
Calculate the correlation coefficient between two arrays using np.corrcoef().

import numpy as np
x = np.array([1, 2, 3, 4])
y = np.array([2, 4, 5, 4])
correlation_matrix = np.corrcoef(x, y)
print(correlation_matrix) # Output: [[1. 0.866...], [0.866... 1.]]


Python tip:
Calculate the covariance matrix using np.cov().

import numpy as np
x = np.array([1, 2, 3])
y = np.array([5, 6, 7])
covariance_matrix = np.cov(x, y)
print(covariance_matrix)


Python tip:
Use np.gradient() to compute the gradient of an N-dimensional array.

import numpy as np
data = np.array([1, 2, 4, 7, 11])
grad = np.gradient(data)
print(grad) # Output: [1. 1.5 2.5 3.5 4. ]


Python tip:
Check if two arrays are equal element-wise using np.array_equal().

import numpy as np
a = np.array([1, 2])
b = np.array([1, 2])
c = np.array([1, 3])
print(np.array_equal(a, b)) # Output: True
print(np.array_equal(a, c)) # Output: False


Python tip:
Use np.concatenate() with axis to join arrays. Remember tuple for arrays.

import numpy as np
a = np.array([[1, 2]])
b = np.array([[3, 4]])
c = np.concatenate((a, b), axis=0) # Vertically stack
d = np.concatenate((a, b), axis=1) # Horizontally stack (requires compatible shapes)
print(f"Vertical:\n{c}")
# print(f"Horizontal:\n{d}") # Would error as a,b are (1,2) and concatenation along axis 1 expects (1,2)+(1,2) not (1,2)+(1,2) resulting in (1,4) which is possible.
# Correct usage for d:
a_flat = np.array([1, 2])
b_flat = np.array([3, 4])
d = np.concatenate((a_flat, b_flat), axis=0) # Or hstack
print(f"Horizontal (flat):\n{d}")


Python tip:
Use np.insert() to insert values along a given axis before the given indices.

import numpy as np
arr = np.array([1, 2, 3, 4])
inserted_arr = np.insert(arr, 2, 99) # Insert 99 at index 2
print(inserted_arr) # Output: [ 1 2 99 3 4]
Python tip:
Use np.delete() to delete elements from an array.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
deleted_arr = np.delete(arr, [1, 3]) # Delete elements at indices 1 and 3
print(deleted_arr) # Output: [1 3 5]


Python tip:
Convert radians to degrees and vice versa using np.degrees() and np.radians().

import numpy as np
rad = np.pi / 2 # 90 degrees in radians
deg = np.degrees(rad)
print(f"Degrees: {deg}")
rad_again = np.radians(deg)
print(f"Radians: {rad_again}")


Python tip:
Use np.round(decimals=...) to specify the number of decimal places for rounding.

import numpy as np
arr = np.array([1.234, 5.678])
rounded_arr = np.round(arr, decimals=1)
print(rounded_arr) # Output: [1.2 5.7]


Python tip:
Replace elements satisfying a condition with another value using np.put().

import numpy as np
arr = np.array([10, 20, 30, 40, 50])
np.put(arr, [0, 2], [100, 300]) # Replace element at index 0 with 100, at index 2 with 300
print(arr) # Output: [100 20 300 40 50]


Python tip:
Use np.vstack() and np.hstack() as shortcuts for common concatenations.

import numpy as np
a = np.array([1, 2])
b = np.array([3, 4])
print(np.vstack((a, b)))
print(np.hstack((a, b)))


Python tip:
Use np.dstack() to stack arrays along the third axis.

import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
stacked_d = np.dstack((a, b)) # Resulting shape (2, 2, 2)
print(stacked_d)


Python tip:
Initialize an array using a function applied to its indices with np.fromfunction().

import numpy as np
def func(i, j):
return i + j

arr = np.fromfunction(func, (3, 3), dtype=int)
print(arr)


Python tip:
Access a contiguous block of memory for performance using array.view().

import numpy as np
arr = np.array([1, 2, 3, 4])
# View as different dtype (e.g., bytes)
byte_view = arr.view(np.int8)
print(byte_view)


Python tip:
Use np.unwrap() to unwrap phase angles.

import numpy as np
phase = np.array([0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi, 5*np.pi/2])
unwrapped_phase = np.unwrap(phase)
print(unwrapped_phase)


Python tip:
Compute the cumulative sum of array elements using np.cumsum().

import numpy as np
arr = np.array([1, 2, 3, 4])
cumulative_sum = np.cumsum(arr)
print(cumulative_sum) # Output: [ 1 3 6 10]


Python tip:
Compute the cumulative product of array elements using np.cumprod().

import numpy as np
arr = np.array([1, 2, 3, 4])
cumulative_prod = np.cumprod(arr)
print(cumulative_prod) # Output: [ 1 2 6 24]


Python tip:
Check if an array contains any element that evaluates to true using arr.any().

import numpy as np
arr_true = np.array([False, True, False])
arr_false = np.array([False, False, False])
print(arr_true.any()) # Output: True
print(arr_false.any()) # Output: False


Python tip:
Check if all elements in an array evaluate to true using arr.all().

import numpy as np
arr_true = np.array([True, True, True])
arr_false = np.array([True, False, True])
print(arr_true.all()) # Output: True
print(arr_false.all()) # Output: False


Python tip:
Use np.pad() to add padding to an array.

import numpy as np
arr = np.array([1, 2, 3])
padded_arr = np.pad(arr, (1, 2), 'constant', constant_values=0) # Pad 1 before, 2 after with 0s
print(padded_arr) # Output: [0 1 2 3 0 0]


Python tip:
Use np.trim_zeros() to remove leading/trailing zeros from a 1D array.

import numpy as np
arr = np.array([0, 0, 1, 2, 0, 3, 0, 0])
trimmed_arr = np.trim_zeros(arr)
print(trimmed_arr) # Output: [1 2 0 3]
Python tip:
Use np.count_nonzero() to count the number of non-zero elements.

import numpy as np
arr = np.array([0, 1, 0, 2, 0, 3])
non_zeros = np.count_nonzero(arr)
print(non_zeros) # Output: 3


Python tip:
Access the real part of complex numbers using .real.

import numpy as np
complex_arr = np.array([1+2j, 3-4j])
print(complex_arr.real) # Output: [1. 3.]


Python tip:
Access the imaginary part of complex numbers using .imag.

import numpy as np
complex_arr = np.array([1+2j, 3-4j])
print(complex_arr.imag) # Output: [ 2. -4.]


Python tip:
Calculate the element-wise difference between a sorted array and itself with a specified lag using np.diff().

import numpy as np
arr = np.array([1, 2, 4, 7, 11, 16])
diff = np.diff(arr)
print(diff) # Output: [1 2 3 4 5]


Python tip:
Perform element-wise maximum of two arrays using np.maximum().

import numpy as np
a = np.array([1, 5, 2])
b = np.array([3, 2, 6])
max_elements = np.maximum(a, b)
print(max_elements) # Output: [3 5 6]


Python tip:
Perform element-wise minimum of two arrays using np.minimum().

import numpy as np
a = np.array([1, 5, 2])
b = np.array([3, 2, 6])
min_elements = np.minimum(a, b)
print(min_elements) # Output: [1 2 2]


Python tip:
Use np.where() with a condition to replace elements.

import numpy as np
arr = np.array([1, -2, 3, -4, 5])
positive_arr = np.where(arr < 0, 0, arr) # Replace negative numbers with 0
print(positive_arr) # Output: [1 0 3 0 5]


Python tip:
Create a sequence of numbers with specific number of samples from np.linspace() when generating values for plotting.

import numpy as np
x_vals = np.linspace(-np.pi, np.pi, 100) # 100 points between -pi and pi
y_vals = np.sin(x_vals)
print(y_vals.shape)


Python tip:
Use np.trim_zeros() to remove leading and trailing zeros from an array.

import numpy as np
arr = np.array([0, 0, 1, 2, 0, 3, 0, 0])
trimmed = np.trim_zeros(arr)
print(trimmed) # Output: [1 2 0 3]


Python tip:
Use np.delete() with axis to delete rows or columns from 2D arrays.

import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Delete row at index 1
deleted_row = np.delete(matrix, 1, axis=0)
print(f"After deleting row 1:\n{deleted_row}")
# Delete column at index 0
deleted_col = np.delete(matrix, 0, axis=1)
print(f"After deleting col 0:\n{deleted_col}")


Python tip:
Use np.set_printoptions() to control how NumPy arrays are printed (e.g., precision, suppression).

import numpy as np
np.set_printoptions(precision=3, suppress=True)
arr = np.array([0.12345678, 100.0/3.0])
print(arr) # Output: [ 0.123 33.333]
np.set_printoptions(edgeitems=2) # Show only first/last 2 items for long arrays
print(np.arange(20))


Python tip:
Avoid explicit Python loops when working with NumPy arrays for significant performance gains (vectorization).

import numpy as np
arr = np.arange(1_000_000)

# Slow (Python loop)
# result_loop = []
# for x in arr:
# result_loop.append(x * 2)

# Fast (NumPy vectorized)
result_vec = arr * 2
print("Vectorized operation is much faster.")


Python tip:
Use arr.clip() as an instance method for np.clip().

import numpy as np
arr = np.array([1, 10, 3, 15, 6])
clipped_arr = arr.clip(3, 10)
print(clipped_arr)


Python tip:
Use np.append() to add values to the end of an array. It always returns a new array.

import numpy as np
arr = np.array([1, 2, 3])
appended_arr = np.append(arr, [4, 5])
print(appended_arr) # Output: [1 2 3 4 5]


Python tip:
Convert between column and row vectors using reshape() or np.newaxis.
import numpy as np
row_vec = np.array([1, 2, 3]) # (3,)
col_vec_1 = row_vec.reshape(-1, 1) # (3, 1)
col_vec_2 = row_vec[:, np.newaxis] # (3, 1)
print(col_vec_1)


Python tip:
Use np.moveaxis() to move an axis of an array to a new position.

import numpy as np
arr = np.zeros((1, 2, 3, 4)) # Example shape
# Move axis 0 to position 2
moved_arr = np.moveaxis(arr, 0, 2) # From (1,2,3,4) to (2,3,1,4)
print(moved_arr.shape)


Python tip:
Use np.swapaxes() to swap two axes of an array.

import numpy as np
arr = np.zeros((2, 3, 4))
swapped_arr = np.swapaxes(arr, 0, 1) # Swap axis 0 and 1
print(swapped_arr.shape) # From (2,3,4) to (3,2,4)


Python tip:
Use np.stack() to join arrays along a new axis.

import numpy as np
a = np.array([1, 2])
b = np.array([3, 4])
stacked_new_axis = np.stack((a, b), axis=-1) # (2, 2)
print(stacked_new_axis)


Python tip:
Use np.tile() to repeat an array n times to create a larger array.

import numpy as np
a = np.array([1, 2])
tiled_a = np.tile(a, 3) # Repeat [1,2] three times -> [1,2,1,2,1,2]
print(tiled_a)


Python tip:
Use np.resize() to change the shape of an array. If new size is larger, it fills with repetitions.

import numpy as np
arr = np.array([1, 2])
resized_arr = np.resize(arr, (2, 3)) # Resizes to 2x3, filling with repetitions
print(resized_arr)


Python tip:
Convert angles from degrees to radians using np.deg2rad().

import numpy as np
degrees = np.array([0, 90, 180])
radians = np.deg2rad(degrees)
print(radians) # Output: [0. 1.57079633 3.14159265] (0, pi/2, pi)


Python tip:
Convert angles from radians to degrees using np.rad2deg().

import numpy as np
radians = np.array([0, np.pi/2, np.pi])
degrees = np.rad2deg(radians)
print(degrees) # Output: [ 0. 90. 180.]


Python tip:
Use np.around() for rounding, similar to np.round().

import numpy as np
arr = np.array([1.5, 2.7, 3.1])
around_arr = np.around(arr)
print(around_arr) # Output: [2. 3. 3.]


Python tip:
Use np.sqrt() for element-wise square root.

import numpy as np
arr = np.array([1, 4, 9, 16])
sqrt_arr = np.sqrt(arr)
print(sqrt_arr) # Output: [1. 2. 3. 4.]


Python tip:
Use np.power() for element-wise exponentiation.

import numpy as np
arr = np.array([2, 3, 4])
power_arr = np.power(arr, 2) # arr squared
print(power_arr) # Output: [ 4 9 16]


Python tip:
Use np.mod() for element-wise modulus.

import numpy as np
arr = np.array([10, 11, 12])
mod_arr = np.mod(arr, 3) # Remainder when divided by 3
print(mod_arr) # Output: [1 2 0]


Python tip:
Use np.absolute() for element-wise absolute value, same as np.abs().

import numpy as np
arr = np.array([-1, -2, 0, 3])
abs_arr = np.absolute(arr)
print(abs_arr) # Output: [1 2 0 3]


Python tip:
Use np.sign() to get the sign of each element (-1 for negative, 0 for zero, 1 for positive).

import numpy as np
arr = np.array([-5, 0, 3])
sign_arr = np.sign(arr)
print(sign_arr) # Output: [-1 0 1]


Python tip:
Use np.true_divide() for float division, even with integers.

import numpy as np
result = np.true_divide(7, 3)
print(result) # Output: 2.3333333333333335


Python tip:
Use np.floor_divide() for integer division (truncates towards negative infinity).

import numpy as np
result = np.floor_divide(7, 3)
print(result) # Output: 2
result_neg = np.floor_divide(-7, 3)
print(result_neg) # Output: -3


Python tip:
Use np.divmod() to get both quotient and remainder from division.
import numpy as np
quotient, remainder = np.divmod(10, 3)
print(f"Quotient: {quotient}, Remainder: {remainder}") # Output: Quotient: 3, Remainder: 1


Python tip:
Convert array elements to boolean using np.array.astype(bool).

import numpy as np
arr = np.array([0, 1, -5, 0.0, 0.1])
bool_arr = arr.astype(bool)
print(bool_arr) # Output: [False True True False True]


Python tip:
Generate a sequence of values in a specific range and step for floating-point numbers using np.r_.

import numpy as np
seq = np.r_[0:10:0.5] # Start at 0, stop at 10, step 0.5
print(seq)


Python tip:
Convert a value to a NumPy array for consistent operations using np.asarray().

import numpy as np
my_list = [1, 2, 3]
arr = np.asarray(my_list)
print(arr)


Python tip:
Use np.unique(return_counts=True) to get unique elements and their frequencies.

import numpy as np
arr = np.array([1, 1, 2, 3, 2, 4, 1, 5])
unique_elements, counts = np.unique(arr, return_counts=True)
print(f"Unique elements: {unique_elements}")
print(f"Counts: {counts}")


Python tip:
Access rows and columns of 2D arrays directly by slicing.

import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
first_row = matrix[0, :] # All columns of the first row
second_col = matrix[:, 1] # All rows of the second column
print(f"First row: {first_row}")
print(f"Second column: {second_col}")


Python tip:
Use np.diagflat() to create a diagonal array from a flattened input.

import numpy as np
arr = np.array([[1,2],[3,4]])
diag_matrix = np.diagflat(arr)
print(diag_matrix)


Python tip:
Get the trace of a square matrix (sum of main diagonal elements) using np.trace().

import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
trace = np.trace(matrix)
print(trace) # Output: 15 (1 + 5 + 9)


Python tip:
Compute the Kronecker product of two arrays using np.kron().

import numpy as np
a = np.array([1, 2])
b = np.array([3, 4])
kronecker_product = np.kron(a, b)
print(kronecker_product) # Output: [3 4 6 8]


Python tip:
Use np.linalg.matrix_power() to raise a square matrix to the (integer) power n.

import numpy as np
matrix = np.array([[1, 2], [3, 4]])
matrix_squared = np.linalg.matrix_power(matrix, 2)
print(matrix_squared)


Python tip:
Check for finite values (not NaN or Inf) using np.isfinite().

import numpy as np
arr = np.array([1, np.nan, np.inf, -np.inf, 5])
finite_arr = np.isfinite(arr)
print(finite_arr) # Output: [ True False False False True]


Python tip:
Use np.around() to round to a specified number of decimals.

import numpy as np
val = 123.4567
rounded_val = np.around(val, decimals=2)
print(rounded_val) # Output: 123.46


Python tip:
Generate a Pascal matrix using np.pascal(). (Note: np.pascal() is not a standard NumPy function. If you need it, you'd implement it manually or use SciPy.)

import numpy as np
# This is a conceptual tip as np.pascal doesn't exist.
# You would usually implement it manually:
def pascal_matrix(n):
matrix = np.zeros((n, n), dtype=int)
for i in range(n):
for j in range(n):
if i == 0 or j == 0:
matrix[i, j] = 1
else:
matrix[i, j] = matrix[i-1, j] + matrix[i, j-1]
return matrix

print(pascal_matrix(4))


Python tip:
Use np.convolve() to compute the discrete linear convolution of two 1D arrays.

import numpy as np
a = np.array([1, 2, 3])
b = np.array([0, 1, 0.5])
convolution = np.convolve(a, b)
print(convolution) # Output: [0. 1. 2.5 4. 1.5]
Python tip:
Use np.polyval() to evaluate a polynomial at specific values.

import numpy as np
poly_coeffs = np.array([3, 0, 1]) # Represents 3x^2 + 0x + 1
x_values = np.array([0, 1, 2])
y_values = np.polyval(poly_coeffs, x_values)
print(y_values) # Output: [ 1 4 13] (3*0^2+1, 3*1^2+1, 3*2^2+1)


Python tip:
Use np.polyfit() to find the coefficients of a polynomial that best fits a set of data points.

import numpy as np
x = np.array([0, 1, 2, 3])
y = np.array([0, 0.8, 0.9, 0.1])
coefficients = np.polyfit(x, y, 2) # Fit a 2nd degree polynomial
print(coefficients)


Python tip:
Use np.clip() to limit values in an array to a specified range, as an instance method.

import numpy as np
arr = np.array([1, 10, 3, 15, 6])
clipped_arr = arr.clip(min=3, max=10)
print(clipped_arr)


Python tip:
Use np.squeeze() to remove single-dimensional entries from the shape of an array.

import numpy as np
arr = np.zeros((1, 3, 1, 4))
squeezed_arr = np.squeeze(arr) # Removes axes of length 1
print(squeezed_arr.shape) # Output: (3, 4)


Python tip:
Create a new array with an inserted axis using np.expand_dims().

import numpy as np
arr = np.array([1, 2, 3]) # Shape (3,)
expanded_arr = np.expand_dims(arr, axis=0) # Add a new axis at position 0
print(expanded_arr.shape) # Output: (1, 3)


Python tip:
Use np.ptp() (peak-to-peak) to find the range (max - min) of an array.

import numpy as np
arr = np.array([1, 5, 2, 8, 3])
peak_to_peak = np.ptp(arr)
print(peak_to_peak) # Output: 7 (8 - 1)


Python tip:
Use np.prod() to calculate the product of array elements.

import numpy as np
arr = np.array([1, 2, 3, 4])
product = np.prod(arr)
print(product) # Output: 24 (1 * 2 * 3 * 4)


Python tip:
Use np.allclose() to compare two arrays for equality within a tolerance.

import numpy as np
a = np.array([1.0, 2.0])
b = np.array([1.00000000001, 2.0])
print(np.allclose(a, b)) # Output: True


Python tip:
Use np.array_split() to split an array into N approximately equal sub-arrays.

import numpy as np
arr = np.arange(7)
split_arr = np.array_split(arr, 3) # Split into 3 parts
print(split_arr)


#NumPyTips #PythonNumericalComputing #ArrayManipulation #DataScience #MachineLearning #PythonTips #NumPyForBeginners #Vectorization #LinearAlgebra #StatisticalAnalysis

━━━━━━━━━━━━━━━
By: @DataScienceM ✨
πŸ€–πŸ§  DeepAgent: A New Era of General AI Reasoning and Scalable Tool-Use Intelligence

πŸ—“οΈ 09 Nov 2025
πŸ“š AI News & Trends

Artificial intelligence has rapidly progressed from simple assistants to advanced reasoning systems capable of complex problem-solving. As tasks demand more autonomy, adaptability and real-world interaction, the AI field has entered the era of intelligent agent systems. These agents are expected not just to answer questions, but to think, plan, search, act and interact across digital ...

#GeneralAI #ArtificialIntelligence #AIReasoning #IntelligentAgents #ScalableAI #ToolUseAI
❀1
πŸ€–πŸ§  PokeeResearch: Advancing Deep Research with AI and Web-Integrated Intelligence

πŸ—“οΈ 09 Nov 2025
πŸ“š AI News & Trends

In the modern information era, the ability to research fast, accurately and at scale has become a competitive advantage for businesses, researchers, analysts and developers. As online data expands exponentially, traditional search engines and manual research workflows are no longer sufficient to gather reliable insights efficiently. This need has fueled the rise of AI research ...

#AIResearch #DeepResearch #WebIntelligence #ArtificialIntelligence #ResearchAutomation #DataAnalysis
πŸ€–πŸ§  Pico-Banana-400K: The Breakthrough Dataset Advancing Text-Guided Image Editing

πŸ—“οΈ 09 Nov 2025
πŸ“š AI News & Trends

Text-guided image editing has rapidly evolved with powerful multimodal models capable of transforming images using simple natural-language instructions. These models can change object colors, modify lighting, add accessories, adjust backgrounds or even convert real photographs into artistic styles. However, the progress of research has been limited by one crucial bottleneck: the lack of large-scale, high-quality, ...

#TextGuidedEditing #MultimodalAI #ImageEditing #AIResearch #ComputerVision #DeepLearning
❀1
πŸ€–πŸ§  Concerto: How Joint 2D-3D Self-Supervised Learning Is Redefining Spatial Intelligence

πŸ—“οΈ 09 Nov 2025
πŸ“š AI News & Trends

The world of artificial intelligence is rapidly evolving and self-supervised learning has become a driving force behind breakthroughs in computer vision and 3D scene understanding. Traditional supervised learning relies heavily on labeled datasets which are expensive and time-consuming to produce. Self-supervised learning, on the other hand, extracts meaningful patterns without manual labels allowing models to ...

#SelfSupervisedLearning #ComputerVision #3DSceneUnderstanding #SpatialIntelligence #AIResearch #DeepLearning
πŸ“Œ Data Culture Is the Symptom, Not the Solution

πŸ—‚ Category: DATA SCIENCE

πŸ•’ Date: 2025-11-10 | ⏱️ Read time: 22 min read

Your data investments are failing, and the common advice to "build a better data culture" might be wrong. This article posits that a flawed data culture is not the root cause but a symptom of a more fundamental, often overlooked issue. To achieve real ROI from data initiatives, organizations must look beyond cultural fixes and address the hidden problems that truly undermine success.

#DataCulture #DataStrategy #DataAnalytics #DataROI
πŸ“Œ Make Python Up to 150Γ— Faster with C

πŸ—‚ Category: PROGRAMMING

πŸ•’ Date: 2025-11-10 | ⏱️ Read time: 14 min read

Dramatically accelerate your Python applicationsβ€”up to 150x fasterβ€”by strategically offloading performance-critical code to C. This practical guide shows how to seamlessly integrate C with your existing Python projects, supercharging your code's bottlenecks without abandoning the Python ecosystem. Achieve significant performance gains where they matter most.

#Python #CProgramming #PerformanceOptimization #Coding
πŸ“Œ Why Storytelling With Data Matters for Business and Data Analysts

πŸ—‚ Category: DATA SCIENCE

πŸ•’ Date: 2025-11-10 | ⏱️ Read time: 7 min read

Data is the engine of modern business, but raw information alone doesn't drive action. The key is data storytelling: the art of weaving complex data into a compelling narrative. For data analysts and business leaders, this skill is no longer optional; it's essential for translating insights into clear, actionable strategies. Mastering data storytelling is crucial for harnessing the true power of information and shaping the future of any organization.

#DataStorytelling #DataAnalysis #BusinessIntelligence #DataViz
❀3
πŸ“Œ Does More Data Always Yield Better Performance?

πŸ—‚ Category: DATA SCIENCE

πŸ•’ Date: 2025-11-10 | ⏱️ Read time: 9 min read

Exploring and challenging the conventional wisdom of β€œmore data β†’ better performance” by experimenting with…

#DataScience #AI #Python
❀2
πŸ“Œ Do You Really Need GraphRAG? A Practitioner’s Guide Beyond the Hype

πŸ—‚ Category: LARGE LANGUAGE MODELS

πŸ•’ Date: 2025-11-11 | ⏱️ Read time: 15 min read

Go beyond the hype with this practitioner's guide to GraphRAG. This article offers a critical perspective on the advanced RAG technique, exploring essential design best practices, common challenges, and key learnings from real-world implementation. It provides a framework to help you decide if GraphRAG is the right solution for your specific needs, moving past the buzz to focus on practical application.

#GraphRAG #RAG #AI #KnowledgeGraphs #LLM
πŸ€–πŸ§  The Transformer Architecture: How Attention Revolutionized Deep Learning

πŸ—“οΈ 11 Nov 2025
πŸ“š AI News & Trends

The field of artificial intelligence has witnessed a remarkable evolution and at the heart of this transformation lies the Transformer architecture. Introduced by Vaswani et al. in 2017, the paper β€œAttention Is All You Need” redefined the foundations of natural language processing (NLP) and sequence modeling. Unlike its predecessors – recurrent and convolutional neural networks, ...

#TransformerArchitecture #AttentionMechanism #DeepLearning #NaturalLanguageProcessing #NLP #AIResearch
πŸ€–πŸ§  The Transformer Architecture: How Attention Revolutionized Deep Learning

πŸ—“οΈ 11 Nov 2025
πŸ“š AI News & Trends

The field of artificial intelligence has witnessed a remarkable evolution and at the heart of this transformation lies the Transformer architecture. Introduced by Vaswani et al. in 2017, the paper β€œAttention Is All You Need” redefined the foundations of natural language processing (NLP) and sequence modeling. Unlike its predecessors – recurrent and convolutional neural networks, ...

#TransformerArchitecture #AttentionMechanism #DeepLearning #NaturalLanguageProcessing #NLP #AIResearch
πŸ€–πŸ§  BERT: Revolutionizing Natural Language Processing with Bidirectional Transformers

πŸ—“οΈ 11 Nov 2025
πŸ“š AI News & Trends

In the ever-evolving landscape of artificial intelligence and natural language processing (NLP), BERT (Bidirectional Encoder Representations from Transformers) stands as a monumental breakthrough. Developed by researchers at Google AI in 2018, BERT introduced a new way of understanding the context of language by using deep bidirectional training of the Transformer architecture. Unlike previous models that ...

#BERT #NaturalLanguageProcessing #TransformerArchitecture #BidirectionalLearning #DeepLearning #AIStrategy
❀1