π€π§ Chandra OCR: The Future of Document Understanding and Layout-Aware Text Extraction
ποΈ 08 Nov 2025
π AI News & Trends
Optical Character Recognition (OCR) has evolved far beyond simply converting scanned text into digital characters. With the rise of artificial intelligence and large language models, the industry is shifting toward intelligent document understanding where structure, context and visual elements matter as much as the text itself. In this landscape, Chandra emerges as a breakthrough solution. ...
#ChandraOCR #DocumentUnderstanding #LayoutAwareText #OpticalCharacterRecognition #AIDocumentProcessing #IntelligentOCR
ποΈ 08 Nov 2025
π AI News & Trends
Optical Character Recognition (OCR) has evolved far beyond simply converting scanned text into digital characters. With the rise of artificial intelligence and large language models, the industry is shifting toward intelligent document understanding where structure, context and visual elements matter as much as the text itself. In this landscape, Chandra emerges as a breakthrough solution. ...
#ChandraOCR #DocumentUnderstanding #LayoutAwareText #OpticalCharacterRecognition #AIDocumentProcessing #IntelligentOCR
π€π§ LMCache: Accelerating LLM Inference With Next-Generation KV Cache Technology
ποΈ 08 Nov 2025
π AI News & Trends
As large language models (LLMs) continue to scale in size and complexity, organizations face an increasingly critical challenge: serving models efficiently in real-world applications. While LLM capabilities are rapidly evolving, the bottleneck of inference performance remains a major limitation especially when dealing with long-context workloads or high-traffic enterprise environments. This is where LMCache steps in. ...
#LMCache #LLMInference #KVCache #LargeLanguageModels #AIAcceleration #NextGenTechnology
ποΈ 08 Nov 2025
π AI News & Trends
As large language models (LLMs) continue to scale in size and complexity, organizations face an increasingly critical challenge: serving models efficiently in real-world applications. While LLM capabilities are rapidly evolving, the bottleneck of inference performance remains a major limitation especially when dealing with long-context workloads or high-traffic enterprise environments. This is where LMCache steps in. ...
#LMCache #LLMInference #KVCache #LargeLanguageModels #AIAcceleration #NextGenTechnology
π€π§ Dify: A Powerful #1 Production-Ready Platform for Building Advanced LLM Applications
ποΈ 08 Nov 2025
π AI News & Trends
The rapid growth of AI has made large language models (LLMs) an essential component for automation, content creation, data intelligence and workflow optimization. But moving AI concepts from prototype to production has traditionally required significant engineering effort, infrastructure planning and model-orchestration expertise. Dify changes that entirely. Dify is an open-source platform designed to help developers, ...
#Dify #LLMApplications #ProductionReady #AIPower #LargeLanguageModels #OpenSourcePlatform
ποΈ 08 Nov 2025
π AI News & Trends
The rapid growth of AI has made large language models (LLMs) an essential component for automation, content creation, data intelligence and workflow optimization. But moving AI concepts from prototype to production has traditionally required significant engineering effort, infrastructure planning and model-orchestration expertise. Dify changes that entirely. Dify is an open-source platform designed to help developers, ...
#Dify #LLMApplications #ProductionReady #AIPower #LargeLanguageModels #OpenSourcePlatform
π€π§ Open WebUI: The Most Powerful Self-Hosted AI Platform for Local and Private LLMs
ποΈ 09 Nov 2025
π AI News & Trends
In the rapidly evolving landscape of artificial intelligence, the ability to run large language models securely and efficiently has become a major priority for developers, enterprises and privacy-focused users. While cloud-based AI services are convenient, they rely heavily on remote servers, internet access and third-party control. This is where Open WebUI stands out as a ...
#OpenWebUI #SelfHostedAI #PrivateLLMs #LocalAI #AISecurity #OpenSourcePlatform
ποΈ 09 Nov 2025
π AI News & Trends
In the rapidly evolving landscape of artificial intelligence, the ability to run large language models securely and efficiently has become a major priority for developers, enterprises and privacy-focused users. While cloud-based AI services are convenient, they rely heavily on remote servers, internet access and third-party control. This is where Open WebUI stands out as a ...
#OpenWebUI #SelfHostedAI #PrivateLLMs #LocalAI #AISecurity #OpenSourcePlatform
π€π§ Generative AI for Beginners: A Complete Guide to Microsoftβs Free Course
ποΈ 09 Nov 2025
π AI News & Trends
Generative AI has rapidly shifted from an emerging technology to a foundation of modern digital innovation. From automated writing assistants and AI chatbots to image generators and intelligent search engines, generative AI is transforming industries and shaping the future of work. Whether you are a student, a budding developer or a technology enthusiast, learning generative ...
#GenerativeAI #BeginnersGuide #MicrosoftAI #FreeCourse #AIEducation #DigitalInnovation
ποΈ 09 Nov 2025
π AI News & Trends
Generative AI has rapidly shifted from an emerging technology to a foundation of modern digital innovation. From automated writing assistants and AI chatbots to image generators and intelligent search engines, generative AI is transforming industries and shaping the future of work. Whether you are a student, a budding developer or a technology enthusiast, learning generative ...
#GenerativeAI #BeginnersGuide #MicrosoftAI #FreeCourse #AIEducation #DigitalInnovation
π€π§ Generative AI for Beginners: A Complete Guide to Microsoftβs Free Course
ποΈ 09 Nov 2025
π AI News & Trends
Generative AI has rapidly shifted from an emerging technology to a foundation of modern digital innovation. From automated writing assistants and AI chatbots to image generators and intelligent search engines, generative AI is transforming industries and shaping the future of work. Whether you are a student, a budding developer or a technology enthusiast, learning generative ...
#GenerativeAI #BeginnersGuide #MicrosoftAI #FreeCourse #AIEducation #DigitalInnovation
ποΈ 09 Nov 2025
π AI News & Trends
Generative AI has rapidly shifted from an emerging technology to a foundation of modern digital innovation. From automated writing assistants and AI chatbots to image generators and intelligent search engines, generative AI is transforming industries and shaping the future of work. Whether you are a student, a budding developer or a technology enthusiast, learning generative ...
#GenerativeAI #BeginnersGuide #MicrosoftAI #FreeCourse #AIEducation #DigitalInnovation
π LLM-Powered Time-Series Analysis
π Category: LARGE LANGUAGE MODELS
π Date: 2025-11-09 | β±οΈ Read time: 9 min read
Explore the next frontier of time-series analysis by leveraging the power of Large Language Models. This article, the second in a series, delves into practical prompting strategies for advanced model development. Learn how to effectively guide LLMs to build more sophisticated and accurate forecasting and analysis solutions, moving beyond basic applications to unlock new capabilities in this critical data science domain.
#LLMs #TimeSeriesAnalysis #PromptEngineering #DataScience #AI
π Category: LARGE LANGUAGE MODELS
π Date: 2025-11-09 | β±οΈ Read time: 9 min read
Explore the next frontier of time-series analysis by leveraging the power of Large Language Models. This article, the second in a series, delves into practical prompting strategies for advanced model development. Learn how to effectively guide LLMs to build more sophisticated and accurate forecasting and analysis solutions, moving beyond basic applications to unlock new capabilities in this critical data science domain.
#LLMs #TimeSeriesAnalysis #PromptEngineering #DataScience #AI
β€2
π How to Build Your Own Agentic AI System Using CrewAI
π Category: AGENTIC AI
π Date: 2025-11-09 | β±οΈ Read time: 12 min read
This article provides a step-by-step guide on developing a custom Agentic AI system using the CrewAI framework. Discover how to define roles, tasks, and tools for multiple AI agents, enabling them to work together autonomously to solve complex problems. This tutorial is ideal for developers looking to build sophisticated, multi-agent AI applications and explore the future of autonomous systems.
#CrewAI #AgenticAI #AIAgents #AIdevelopment
π Category: AGENTIC AI
π Date: 2025-11-09 | β±οΈ Read time: 12 min read
This article provides a step-by-step guide on developing a custom Agentic AI system using the CrewAI framework. Discover how to define roles, tasks, and tools for multiple AI agents, enabling them to work together autonomously to solve complex problems. This tutorial is ideal for developers looking to build sophisticated, multi-agent AI applications and explore the future of autonomous systems.
#CrewAI #AgenticAI #AIAgents #AIdevelopment
π Python Pillow: Your Image Journey Begins
π’ Unlock Python image processing! Learn to open and display images effortlessly with the powerful Pillow library.
β‘ Tap to unlock the complete answer and gain instant insight.
βββββββββββββββ
By: @DataScienceM β¨
π’ Unlock Python image processing! Learn to open and display images effortlessly with the powerful Pillow library.
β‘ Tap to unlock the complete answer and gain instant insight.
βββββββββββββββ
By: @DataScienceM β¨
Telegraph
Python Pillow: Your Image Journey Begins
Python tip:To get started with image processing, use the Pillow library to open and display images. from PIL import Image# Make sure you have an image file (e.g., 'example.jpg') in the same directorytry: img = Image.open('example.jpg') print(f"Image format:β¦
β€2
100 essential NumPy tips for beginners
Python tip:
Import NumPy with the standard alias
Python tip:
Create a NumPy array from a Python list or tuple using
Python tip:
Initialize an array filled with zeros using
Python tip:
Initialize an array filled with ones using
Python tip:
Create an empty array with
Python tip:
Generate a sequence of numbers with
Python tip:
Generate evenly spaced numbers over a specified interval using
Python tip:
Create an array filled with a specific constant value using
Python tip:
Create an identity matrix (square matrix with ones on the main diagonal) using
Python tip:
Create a new array of zeros with the same shape and data type as an existing array using
Python tip:
Check the shape (dimensions) of an array using the
Python tip:
Check the number of dimensions of an array using the
Python tip:
Get the total number of elements in an array using the
Python tip:
Determine the data type of elements in an array using the
Python tip:
Specify the data type when creating an array for memory efficiency or specific operations.
Python tip:
Access individual elements using square brackets
Python tip:
For 2D arrays, use
Python tip:
Import NumPy with the standard alias
np for convenience.import numpy as np
print(np.__version__)
Python tip:
Create a NumPy array from a Python list or tuple using
np.array().import numpy as np
my_list = [1, 2, 3, 4, 5]
arr = np.array(my_list)
print(arr)
Python tip:
Initialize an array filled with zeros using
np.zeros(), specifying the shape.import numpy as np
zeros_array = np.zeros((3, 4)) # 3 rows, 4 columns
print(zeros_array)
Python tip:
Initialize an array filled with ones using
np.ones(), specifying the shape and optionally dtype.import numpy as np
ones_array = np.ones((2, 3), dtype=int)
print(ones_array)
Python tip:
Create an empty array with
np.empty(). Its initial content is random and depends on memory.import numpy as np
empty_array = np.empty((2, 2))
print(empty_array)
Python tip:
Generate a sequence of numbers with
np.arange(), similar to Python's range().import numpy as np
sequence = np.arange(0, 10, 2) # start, stop (exclusive), step
print(sequence)
Python tip:
Generate evenly spaced numbers over a specified interval using
np.linspace().import numpy as np
evenly_spaced = np.linspace(0, 10, 5) # start, stop (inclusive), number of samples
print(evenly_spaced)
Python tip:
Create an array filled with a specific constant value using
np.full().import numpy as np
full_array = np.full((2, 2), 7)
print(full_array)
Python tip:
Create an identity matrix (square matrix with ones on the main diagonal) using
np.eye() or np.identity().import numpy as np
identity_matrix = np.eye(3) # 3x3 identity matrix
print(identity_matrix)
Python tip:
Create a new array of zeros with the same shape and data type as an existing array using
np.zeros_like().import numpy as np
original_array = np.array([[1, 2], [3, 4]])
like_zeros = np.zeros_like(original_array)
print(like_zeros)
Python tip:
Check the shape (dimensions) of an array using the
.shape attribute.import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape) # Output: (2, 3)
Python tip:
Check the number of dimensions of an array using the
.ndim attribute.import numpy as np
arr_1d = np.array([1, 2, 3])
arr_2d = np.array([[1, 2], [3, 4]])
print(arr_1d.ndim) # Output: 1
print(arr_2d.ndim) # Output: 2
Python tip:
Get the total number of elements in an array using the
.size attribute.import numpy as np
arr = np.zeros((3, 4))
print(arr.size) # Output: 12 (3 * 4)
Python tip:
Determine the data type of elements in an array using the
.dtype attribute.import numpy as np
arr_int = np.array([1, 2, 3])
arr_float = np.array([1.0, 2.0])
print(arr_int.dtype) # Output: int64 (or int32 depending on system)
print(arr_float.dtype) # Output: float64
Python tip:
Specify the data type when creating an array for memory efficiency or specific operations.
import numpy as np
arr = np.array([1, 2, 3], dtype=np.int8)
print(arr.dtype)
Python tip:
Access individual elements using square brackets
[] with zero-based indexing.import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr[0]) # Output: 10
print(arr[2]) # Output: 30
Python tip:
For 2D arrays, use
[row_index, col_index] to access elements.import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(matrix[0, 0]) # Output: 1
print(matrix[1, 2]) # Output: 6
Python tip:
Slice arrays to extract sub-arrays using
Python tip:
Slice rows and columns in 2D arrays:
Python tip:
Use
Python tip:
Reshape an array to a new shape using the
Python tip:
Use
Python tip:
Flatten an array into a 1D array using
Python tip:
Convert an array to a different data type using
Python tip:
Perform element-wise arithmetic operations directly on arrays.
Python tip:
Multiply arrays element-wise, not matrix multiplication, using
Python tip:
Perform matrix multiplication using the
Python tip:
Broadcasting allows operations on arrays of different shapes if compatible.
Python tip:
Broadcasting also works between arrays with compatible dimensions.
Python tip:
Use
Python tip:
Concatenate arrays along an existing axis using
Python tip:
Stack arrays vertically (row-wise) using
Slice arrays to extract sub-arrays using
[start:stop:step]. stop is exclusive.import numpy as np
arr = np.arange(10) # [0, 1, ..., 9]
slice_arr = arr[2:7:2] # elements from index 2 to 6, with step 2
print(slice_arr) # Output: [2, 4, 6]
Python tip:
Slice rows and columns in 2D arrays:
[row_slice, col_slice].import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
sub_matrix = matrix[0:2, 1:3] # rows 0 and 1, columns 1 and 2
print(sub_matrix)
Python tip:
Use
-1 for negative indexing to access elements from the end of the array.import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[-1]) # Output: 50 (last element)
print(arr[-3:]) # Output: [30, 40, 50] (last three elements)
Python tip:
Reshape an array to a new shape using the
.reshape() method. The new shape must have the same total number of elements.import numpy as np
arr = np.arange(12) # 12 elements
reshaped_arr = arr.reshape(3, 4) # 3 rows, 4 columns
print(reshaped_arr)
Python tip:
Use
-1 in reshape() to let NumPy automatically calculate one dimension.import numpy as np
arr = np.arange(12)
reshaped_arr = arr.reshape(3, -1) # 3 rows, NumPy calculates 4 columns
print(reshaped_arr)
Python tip:
Flatten an array into a 1D array using
.ravel() or .flatten(). .ravel() returns a view where possible, .flatten() always returns a copy.import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
flat_arr = matrix.ravel()
print(flat_arr)
Python tip:
Convert an array to a different data type using
.astype().import numpy as np
arr_float = np.array([1.5, 2.7, 3.1])
arr_int = arr_float.astype(int)
print(arr_int) # Output: [1 2 3]
Python tip:
Perform element-wise arithmetic operations directly on arrays.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # Addition
print(a * b) # Multiplication
Python tip:
Multiply arrays element-wise, not matrix multiplication, using
*.import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(a * b) # Element-wise product
Python tip:
Perform matrix multiplication using the
@ operator (Python 3.5+) or np.dot().import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
matrix_product = a @ b # or np.dot(a, b)
print(matrix_product)
Python tip:
Broadcasting allows operations on arrays of different shapes if compatible.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
scalar = 10
print(arr + scalar) # Scalar is broadcast to all elements
Python tip:
Broadcasting also works between arrays with compatible dimensions.
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]]) # shape (2, 3)
b = np.array([10, 20, 30]) # shape (3,)
print(a + b) # b is broadcast across rows of a
Python tip:
Use
np.newaxis (or None) to add a new dimension for broadcasting.import numpy as np
vec = np.array([1, 2, 3]) # shape (3,)
col_vec = vec[:, np.newaxis] # shape (3, 1)
print(col_vec)
Python tip:
Concatenate arrays along an existing axis using
np.concatenate().import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
c = np.concatenate((a, b), axis=0) # Concatenate vertically
print(c)
Python tip:
Stack arrays vertically (row-wise) using
np.vstack() or np.row_stack().import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
stacked_vert = np.vstack((a, b))
print(stacked_vert)
Python tip:
Stack arrays horizontally (column-wise) using
Python tip:
Split an array into multiple sub-arrays using
Python tip:
Use
Python tip:
Calculate the sum of all elements in an array using
Python tip:
Calculate the sum along a specific axis (
Python tip:
Find the minimum and maximum values in an array using
Python tip:
Find the index of the minimum/maximum value using
Python tip:
Calculate the mean (average) of array elements using
Python tip:
Calculate the median of array elements using
Python tip:
Calculate the standard deviation using
Python tip:
Calculate the variance using
Python tip:
Sort an array using
Python tip:
Sort an array in-place using the
Python tip:
Get the unique elements of an array using
Python tip:
Generate random floating-point numbers in
Python tip:
Generate random integers within a specified range using
Python tip:
Set a random seed using
Stack arrays horizontally (column-wise) using
np.hstack() or np.column_stack().import numpy as np
a = np.array([[1], [2]])
b = np.array([[3], [4]])
stacked_horz = np.hstack((a, b))
print(stacked_horz)
Python tip:
Split an array into multiple sub-arrays using
np.split().import numpy as np
arr = np.arange(12).reshape(2, 6)
split_arr = np.split(arr, 3, axis=1) # Split into 3 arrays along column axis
print(split_arr)
Python tip:
Use
np.where() for conditional element selection, similar to a ternary operator.import numpy as np
arr = np.array([1, 5, 2, 8, 3])
result = np.where(arr > 4, arr * 10, arr) # if > 4, multiply by 10, else keep original
print(result) # Output: [ 1 50 2 80 3]
Python tip:
Calculate the sum of all elements in an array using
np.sum().import numpy as np
arr = np.array([[1, 2], [3, 4]])
total_sum = np.sum(arr)
print(total_sum) # Output: 10
Python tip:
Calculate the sum along a specific axis (
axis=0 for columns, axis=1 for rows).import numpy as np
arr = np.array([[1, 2], [3, 4]])
sum_rows = np.sum(arr, axis=1) # Sum of each row
sum_cols = np.sum(arr, axis=0) # Sum of each column
print(f"Row sums: {sum_rows}")
print(f"Column sums: {sum_cols}")
Python tip:
Find the minimum and maximum values in an array using
np.min() and np.max().import numpy as np
arr = np.array([1, 5, 2, 8, 3])
print(np.min(arr)) # Output: 1
print(np.max(arr)) # Output: 8
Python tip:
Find the index of the minimum/maximum value using
np.argmin() and np.argmax().import numpy as np
arr = np.array([1, 5, 2, 8, 3])
print(np.argmin(arr)) # Output: 0 (index of 1)
print(np.argmax(arr)) # Output: 3 (index of 8)
Python tip:
Calculate the mean (average) of array elements using
np.mean().import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(np.mean(arr)) # Output: 3.0
Python tip:
Calculate the median of array elements using
np.median().import numpy as np
arr = np.array([1, 5, 2, 8, 3])
print(np.median(arr)) # Output: 3.0
Python tip:
Calculate the standard deviation using
np.std().import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(np.std(arr))
Python tip:
Calculate the variance using
np.var().import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(np.var(arr))
Python tip:
Sort an array using
np.sort(). It returns a sorted copy, leaving the original array unchanged.import numpy as np
arr = np.array([3, 1, 4, 1, 5, 9, 2])
sorted_arr = np.sort(arr)
print(sorted_arr)
print(arr) # Original array is unchanged
Python tip:
Sort an array in-place using the
.sort() method.import numpy as np
arr = np.array([3, 1, 4, 1, 5, 9, 2])
arr.sort() # Sorts the array itself
print(arr)
Python tip:
Get the unique elements of an array using
np.unique().import numpy as np
arr = np.array([1, 1, 2, 3, 2, 4, 1, 5])
unique_elements = np.unique(arr)
print(unique_elements) # Output: [1 2 3 4 5]
Python tip:
Generate random floating-point numbers in
[0.0, 1.0) using np.random.rand().import numpy as np
random_floats = np.random.rand(3, 2) # 3 rows, 2 columns
print(random_floats)
Python tip:
Generate random integers within a specified range using
np.random.randint().import numpy as np
random_ints = np.random.randint(0, 10, size=(2, 3)) # integers between 0 (inclusive) and 10 (exclusive)
print(random_ints)
Python tip:
Set a random seed using
np.random.seed() for reproducible results.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
Python tip:
Get the indices that would sort an array using
Python tip:
Use
Python tip:
Replace specific values in an array using
Python tip:
Use
Python tip:
Generate a histogram of non-negative integers using
Python tip:
Use
Python tip:
Calculate the correlation coefficient between two arrays using
Python tip:
Calculate the covariance matrix using
Python tip:
Use
Python tip:
Check if two arrays are equal element-wise using
Python tip:
Use
Python tip:
Use
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
Python tip:
Convert radians to degrees and vice versa using
Python tip:
Use
Python tip:
Replace elements satisfying a condition with another value using
Python tip:
Use
Python tip:
Use
Python tip:
Initialize an array using a function applied to its indices with
Python tip:
Access a contiguous block of memory for performance using
Python tip:
Use
Python tip:
Compute the cumulative sum of array elements using
Python tip:
Compute the cumulative product of array elements using
Python tip:
Check if an array contains any element that evaluates to true using
Python tip:
Check if all elements in an array evaluate to true using
Python tip:
Use
Python tip:
Use
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
Python tip:
Access the real part of complex numbers using
Python tip:
Access the imaginary part of complex numbers using
Python tip:
Calculate the element-wise difference between a sorted array and itself with a specified lag using
Python tip:
Perform element-wise maximum of two arrays using
Python tip:
Perform element-wise minimum of two arrays using
Python tip:
Use
Python tip:
Create a sequence of numbers with specific number of samples from
Python tip:
Use
Python tip:
Use
Python tip:
Use
Python tip:
Avoid explicit Python loops when working with NumPy arrays for significant performance gains (vectorization).
Python tip:
Use
Python tip:
Use
Python tip:
Convert between column and row vectors using
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
Python tip:
Use
Python tip:
Use
Python tip:
Use
Python tip:
Create a new array with an inserted axis using
Python tip:
Use
Python tip:
Use
Python tip:
Use
Python tip:
Use
#NumPyTips #PythonNumericalComputing #ArrayManipulation #DataScience #MachineLearning #PythonTips #NumPyForBeginners #Vectorization #LinearAlgebra #StatisticalAnalysis
βββββββββββββββ
By: @DataScienceM β¨
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 β¨