Machine Learning
Real Machine Learning — simple, practical, and built on experience. Learn step by step with clear explanations and working code. Admin: @HusseinSheikho || @Hussein_Sheikho
نمایش بیشتر📈 تحلیل کانال تلگرام Machine Learning
کانال Machine Learning (@machinelearning9) در بخش زبانی انگلیسی بازیگری فعال است. در حال حاضر جامعه شامل 40 140 مشترک است و جایگاه 3 371 را در دسته فناوری و برنامهها و رتبه 230 را در منطقه سوريا دارد.
📊 شاخصهای مخاطب و پویایی
از زمان ایجاد در невідомо، پروژه رشد سریعی داشته و 40 140 مشترک جذب کرده است.
بر اساس آخرین دادهها در تاریخ 26 ژوئن, 2026، کانال فعالیت پایداری دارد. در ۳۰ روز گذشته تغییر اعضا برابر 429 و در ۲۴ ساعت گذشته برابر 20 بوده و همچنان دسترسی گستردهای حفظ شده است.
- وضعیت تأیید: تأیید نشده
- نرخ تعامل (ER): میانگین تعامل مخاطب 1.83% است و در ۲۴ ساعت نخست پس از انتشار، محتوا معمولاً 1.60% واکنش نسبت به کل مشترکان کسب میکند.
- دسترسی پستها: هر پست به طور میانگین 735 بازدید دریافت میکند. در اولین روز معمولاً 643 بازدید جمعآوری میشود.
- واکنشها و تعامل: مخاطبان بهطور فعال حمایت میکنند؛ میانگین واکنش به هر پست 2 است.
- علایق موضوعی: محتوا بر موضوعات کلیدی مانند distance, insidead, gpu, learning, degree تمرکز دارد.
📝 توضیح و سیاست محتوایی
نویسنده این فضا را محل بیان دیدگاههای شخصی توصیف میکند:
“Real Machine Learning — simple, practical, and built on experience.
Learn step by step with clear explanations and working code.
Admin: @HusseinSheikho || @Hussein_Sheikho”
به لطف بهروزرسانیهای پرتکرار (آخرین داده در تاریخ 27 ژوئن, 2026)، کانال همواره بهروز و دارای دسترسی بالاست. تحلیلها نشان میدهد مخاطبان بهطور فعال با محتوا تعامل دارند و آن را به نقطه اثرگذاری مهم در دسته فناوری و برنامهها تبدیل کردهاند.
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 ✨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]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.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.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]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]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)
اکنون در دسترس! پژوهش تلگرام ۲۰۲۵ — مهمترین بینشهای سال 
