Debug School

rakesh kumar
rakesh kumar

Posted on

How to apply numpy and list comprhension on 2D or 3D data

Convert 2d array to 1d array using list comprehension
How to square each element in a 2D array
How to square each element after flattening 2d array
Filter elements greater than 0.5 in a 2D array
How to convert predicted probabilities into predicted labels
Finding most prominent features or peak values in your dataset
Finding the minimum value helps identify the least significant or baseline values in your dataset
How to normalize features, centering them around their average
How to measure of central tendency, especially with skewed data or outlier
How calculating total counts or feature combinations in ML preprocessing
How to spread or variability in the dataset, essential for feature scaling
How to Variance quantifies how much the data points deviate from the mean
How to identify which feature is most or least significant in each sample
How Cumulative sums is helpful for time-series analysis
How percentile is used in outlier detection
Add 10 to each element of array

Convert 2d array to 1d array using list comprehension

array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Enter fullscreen mode Exit fullscreen mode

Using List Comprehension

flattened_array = [item for row in array for item in row]
print(flattened_array)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Enter fullscreen mode Exit fullscreen mode

Using a Nested for Loop

flattened_array = []
for row in array:
    for item in row:
        flattened_array.append(item)

print(flattened_array)
Enter fullscreen mode Exit fullscreen mode

Output:

 [1, 2, 3, 4, 5, 6, 7, 8, 9]
Both methods will produce the same flattened array [1, 2, 3, 4, 5, 6, 7, 8, 9].
Enter fullscreen mode Exit fullscreen mode

How to square each element in a 2D array

Given Example Data
Using the same 2D array example:

array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Enter fullscreen mode Exit fullscreen mode

Using List Comprehension

squared_array = [[element**2 for element in row] for row in array]
print(squared_array)  # Output: [[1, 4, 9], [16, 25, 36], [49, 64, 81]]
Enter fullscreen mode Exit fullscreen mode

Using a Nested for Loop

squared_array = []
for row in array:
    squared_row = []
    for element in row:
        squared_row.append(element**2)
    squared_array.append(squared_row)


print(squared_array) 
Enter fullscreen mode Exit fullscreen mode

Output:

 [[1, 4, 9], [16, 25, 36], [49, 64, 81]]
Both methods will yield the same squared array [[1, 4, 9], [16, 25, 36], [49, 64, 81]].
Enter fullscreen mode Exit fullscreen mode

How to square each element after flattening 2d array

Given Example Data

array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Enter fullscreen mode Exit fullscreen mode

Using List Comprehension
Flatten and then square each element:

flattened_array = [item for row in array for item in row]
squared_array = [item**2 for item in flattened_array]
print(squared_array)  # Output: [1, 4, 9, 16, 25, 36, 49, 64, 81]
Enter fullscreen mode Exit fullscreen mode

Using a Nested for Loop

# Flattening the array
flattened_array = []
for row in array:
    for item in row:
        flattened_array.append(item)

# Squaring each element
squared_array = []
for item in flattened_array:
    squared_array.append(item**2)

print(squared_array)  # Output: [1, 4, 9, 16, 25, 36, 49, 64, 81]
Enter fullscreen mode Exit fullscreen mode

Filter elements greater than 0.5 in a 2D array

array = [[0.1, 0.6, 0.3], [0.7, 0.2, 0.9], [0.4, 0.8, 0.05]]
Enter fullscreen mode Exit fullscreen mode

Using List Comprehension

filtered_elements = [element for row in array for element in row if element > 0.5]
print(filtered_elements)  # Output: [0.6, 0.7, 0.9, 0.8]
Enter fullscreen mode Exit fullscreen mode

Using a Nested for Loop

filtered_elements = []
for row in array:
    for element in row:
        if element > 0.5:
            filtered_elements.append(element)

print(filtered_elements)  # 
Enter fullscreen mode Exit fullscreen mode

Output:

[0.6, 0.7, 0.9, 0.8]
Both methods will produce the same filtered list [0.6, 0.7, 0.9, 0.8].
Enter fullscreen mode Exit fullscreen mode

How to convert predicted probabilities into predicted labels

Given Example Data
Let's assume y_predicted is a list of lists, representing the predicted probabilities for each class in a multi-class classification problem:

import numpy as np

y_predicted = [
    [0.1, 0.3, 0.6],  # Prediction for the first sample
    [0.8, 0.1, 0.1],  # Prediction for the second sample
    [0.2, 0.5, 0.3]   # Prediction for the third sample
]
Enter fullscreen mode Exit fullscreen mode

Using List Comprehension

y_predicted_labels = [np.argmax(i) for i in y_predicted]
print(y_predicted_labels)  # Output: [2, 0, 1]
Enter fullscreen mode Exit fullscreen mode

Using a for Loop

y_predicted_labels = []
for i in y_predicted:
    y_predicted_labels.append(np.argmax(i))

print(y_predicted_labels)  # Output: [2, 0, 1]
Enter fullscreen mode Exit fullscreen mode

In both methods, np.argmax finds the index of the maximum value in each list (i.e., the predicted class), resulting in [2, 0, 1] for this example data.

Numpy aggreagate command

Given Example Data

import numpy as np

data = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
Enter fullscreen mode Exit fullscreen mode

Finding most prominent features or peak values in your dataset

np.max() - Finding Maximum Values
In ML, finding the maximum value can help identify the most prominent features or peak values in your dataset.

Using List Comprehension:

max_values = [np.max(row) for row in data]
print(max_values)  # Output: [3, 6, 9]
Enter fullscreen mode Exit fullscreen mode

Explanation: The maximum value of each row is found, which could represent the strongest feature in each data sample.

Finding the minimum value helps identify the least significant or baseline values in your dataset

Finding the minimum value helps identify the least significant or baseline values in your dataset.

Using List Comprehension:


min_values = [np.min(row) for row in data]
print(min_values)  # Output: [1, 4, 7]
Enter fullscreen mode Exit fullscreen mode

Explanation: This identifies the smallest value in each row, useful for understanding the lower bound of feature ranges.

How to normalize features, centering them around their average

np.mean() - Calculating Mean
The mean is often used to normalize features, centering them around their average.

Using List Comprehension:

mean_values = [np.mean(row) for row in data]
print(mean_values)  # Output: [2.0, 5.0, 8.0]
Enter fullscreen mode Exit fullscreen mode

Explanation: Each row's average value represents the central tendency of the features in that data sample.

How to measure of central tendency, especially with skewed data or outlier

The median is a robust measure of central tendency, especially with skewed data or outliers.

Using List Comprehension:

median_values = [np.median(row) for row in data]
print(median_values)  # Output: [2.0, 5.0, 8.0]
Enter fullscreen mode Exit fullscreen mode

Explanation: This gives the middle value of each row, providing a measure of centrality less affected by extreme values.

How calculating total counts or feature combinations in ML preprocessing

.
Summing values can be useful for calculating total counts or feature combinations in ML preprocessing.

Using List Comprehension:

sum_values = [np.sum(row) for row in data]
print(sum_values)  # Output: [6, 15, 24]
Enter fullscreen mode Exit fullscreen mode

Explanation: The sum provides an aggregate measure of all features for each data sample.

How to spread or variability in the dataset, essential for feature scaling

Using List Comprehension:

std_values = [np.std(row) for row in data]
print(std_values)  # Output: [0.816496580927726, 0.816496580927726, 0.816496580927726]
Enter fullscreen mode Exit fullscreen mode

How to Variance quantifies how much the data points deviate from the mean

.
Variance quantifies how much the data points deviate from the mean.

Using List Comprehension:

variance_values = [np.var(row) for row in data]
print(variance_values)  # Output: [0.6666666666666666, 0.6666666666666666, 0.6666666666666666]
Enter fullscreen mode Exit fullscreen mode

How to identify which feature is most or least significant in each sample

Using List Comprehension:

argmax_indices = [np.argmax(row) for row in data]
argmin_indices = [np.argmin(row) for row in data]
print(argmax_indices)  # Output: [2, 2, 2]
print(argmin_indices)  # Output: [0, 0, 0]
Enter fullscreen mode Exit fullscreen mode

Explanation: argmax points to the most significant feature (last column), and argmin to the least (first column) for each row.

How Cumulative sums is helpful for time-series analysis

Using List Comprehension:

cumulative_sums = [np.cumsum(row).tolist() for row in data]
print(cumulative_sums)  # Output: [[1, 3, 6], [4, 9, 15], [7, 15, 24]]
Enter fullscreen mode Exit fullscreen mode

.

How percentile is used in outlier detection

ercentiles help understand the distribution, often used in outlier detection.

Using List Comprehension:

percentile_75 = [np.percentile(row, 75) for row in data]
print(percentile_75)  # Output: [2.5, 5.5, 8.5]
Enter fullscreen mode Exit fullscreen mode

Explanation: The 75th percentile shows the value below which 75% of the data falls in each row.

Add 10 to each element of array

added_10_2d = [[item + 10 for item in row] for row in array_2d]
# Output: [[13, 18, 11], [14, 17, 15], [19, 12, 16]]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)