-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.py
More file actions
93 lines (68 loc) · 2.08 KB
/
operations.py
File metadata and controls
93 lines (68 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import numpy as np
def create_dataset():
"""
Create a sample dataset (2D array) for operations.
Rows = samples
Columns = features
"""
np.random.seed(42) # reproducibility
data = np.random.randint(1, 100, size=(5, 4)) # 5x4 dataset
return data
def mathematical_operations(data):
"""
Perform element-wise mathematical operations.
"""
addition = data + 10
subtraction = data - 5
multiplication = data * 2
division = data / 2
# Advanced operations
square = np.square(data)
sqrt = np.sqrt(data)
return addition, subtraction, multiplication, division, square, sqrt
def axis_operations(data):
"""
Perform operations along rows and columns.
axis=0 → column-wise
axis=1 → row-wise
"""
# Sum
col_sum = np.sum(data, axis=0)
row_sum = np.sum(data, axis=1)
# Mean
col_mean = np.mean(data, axis=0)
row_mean = np.mean(data, axis=1)
return col_sum, row_sum, col_mean, row_mean
def statistical_operations(data):
"""
Perform statistical analysis.
"""
mean = np.mean(data)
median = np.median(data)
std_dev = np.std(data)
variance = np.var(data)
min_val = np.min(data)
max_val = np.max(data)
return mean, median, std_dev, variance, min_val, max_val
def main():
data = create_dataset()
print("Dataset:\n", data)
print("\n--- Mathematical Operations ---")
add, sub, mul, div, sq, root = mathematical_operations(data)
print("Add 10:\n", add)
print("Multiply by 2:\n", mul)
print("Square:\n", sq)
print("\n--- Axis-wise Operations ---")
col_sum, row_sum, col_mean, row_mean = axis_operations(data)
print("Column Sum:", col_sum)
print("Row Sum:", row_sum)
print("Column Mean:", col_mean)
print("Row Mean:", row_mean)
print("\n--- Statistical Operations ---")
mean, median, std, var, min_v, max_v = statistical_operations(data)
print("Mean:", mean)
print("Median:", median)
print("Standard Deviation:", std)
print("Variance:", var)
print("Min:", min_v)
print("Max:", max_v)