-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33.Numpy.py
More file actions
174 lines (143 loc) · 5.09 KB
/
33.Numpy.py
File metadata and controls
174 lines (143 loc) · 5.09 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
=========================
NumPy Detailed Notes
=========================
NumPy (Numerical Python) is a fundamental library for numerical and scientific computing.
It provides powerful n-dimensional arrays, mathematical functions, and tools for data analysis.
------------------------
🔑 Installation:
------------------------
pip install numpy
------------------------
📌 Import Syntax:
------------------------
import numpy as np
------------------------
🎯 Key Features:
------------------------
- Fast, memory-efficient multi-dimensional arrays
- Mathematical operations on entire arrays (vectorization)
- Broadcasting support
- Linear algebra, Fourier transform, random number generation
- Used in ML, AI, Data Science, Scientific Computing
"""
import numpy as np
# ===================================
# 1️⃣ Creating Arrays
# ===================================
# From Python list
arr1 = np.array([1, 2, 3])
print("Array from list:", arr1)
# Multi-dimensional array
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print("\n2D Array:\n", arr2)
# Special arrays
zeros = np.zeros((3, 3)) # 3x3 matrix filled with zeros
ones = np.ones((2, 4)) # 2x4 matrix filled with ones
identity = np.eye(3) # Identity matrix
range_array = np.arange(0, 10, 2) # Start=0, Stop=10, Step=2
linspace_array = np.linspace(0, 1, 5) # 5 values between 0 & 1
print("\nZeros:\n", zeros)
print("\nIdentity:\n", identity)
print("\nRange Array:", range_array)
print("\nLinspace Array:", linspace_array)
# ===================================
# 2️⃣ Array Attributes
# ===================================
print("\nShape:", arr2.shape)
print("Dimensions:", arr2.ndim)
print("Size:", arr2.size)
print("Data Type:", arr2.dtype)
print("Item Size (bytes):", arr2.itemsize)
# ===================================
# 3️⃣ Reshaping and Flattening
# ===================================
reshaped = np.arange(12).reshape(3, 4)
print("\nReshaped 3x4:\n", reshaped)
flattened = reshaped.flatten()
print("\nFlattened Array:", flattened)
# ===================================
# 4️⃣ Vectorized Operations
# ===================================
a = np.array([1, 2, 3, 4, 5])
b = np.array([10, 20, 30, 40, 50])
print("\nAddition:", a + b)
print("Subtraction:", b - a)
print("Multiplication:", a * b)
print("Division:", b / a)
# Broadcasting Example
print("\nBroadcasting Example:\n", a + 5)
# ===================================
# 5️⃣ Universal Functions (ufuncs)
# ===================================
print("\nSquare Root:", np.sqrt(a))
print("Exponential:", np.exp(a))
print("Sine:", np.sin(a))
print("Log:", np.log(a))
# ===================================
# 6️⃣ Indexing & Slicing
# ===================================
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("\nElement at (0,0):", matrix[0, 0])
print("Second Row:", matrix[1])
print("Last Column:", matrix[:, 2])
print("Submatrix:\n", matrix[0:2, 1:])
# Modify elements
matrix[0, 0] = 100
matrix[:, 1] = 200
print("\nModified Matrix:\n", matrix)
# ===================================
# 7️⃣ Statistical Functions
# ===================================
data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
mean = np.mean(data)
median = np.median(data)
std_dev = np.std(data)
variance = np.var(data)
print("\nMean:", mean)
print("Median:", median)
print("Standard Deviation:", std_dev)
print("Variance:", variance)
# Normalize data
normalized_data = (data - mean) / std_dev
print("\nNormalized Data:", normalized_data)
# Logical Operations
print("\nElements > 5:", data[data > 5])
print("Elements between 3 and 7:", data[(data >= 3) & (data <= 7)])
# ===================================
# 8️⃣ Random Module (Important for ML)
# ===================================
random_arr = np.random.rand(3, 3) # Random floats [0, 1)
randn_arr = np.random.randn(3, 3) # Normally distributed random numbers
randint_arr = np.random.randint(1, 10, size=(3, 3)) # Random integers
print("\nRandom Array:\n", random_arr)
print("\nNormal Distribution Array:\n", randn_arr)
print("\nRandom Integers:\n", randint_arr)
# ===================================
# 9️⃣ Practical Use Cases in ML/AI
# ===================================
# Example 1: Feature Scaling
features = np.array([10, 20, 30, 40, 50])
scaled = (features - np.min(features)) / (np.max(features) - np.min(features))
print("\nMin-Max Scaled Features:", scaled)
# Example 2: Mean Squared Error (MSE)
y_true = np.array([3.0, -0.5, 2.0, 7.0])
y_pred = np.array([2.5, 0.0, 2.1, 7.8])
mse = np.mean((y_true - y_pred) ** 2)
print("\nMean Squared Error:", mse)
# Example 3: Creating Dummy Dataset for ML
X = np.random.rand(100, 3) # 100 samples, 3 features
y = (X[:, 0] + X[:, 1] * 0.5 > 0.7).astype(int) # Binary classification labels
print("\nDummy Features Shape:", X.shape)
print("Dummy Labels Shape:", y.shape)
print("First 5 Labels:", y[:5])
"""
💡 Key ML/AI Use Cases of NumPy:
- Data Preprocessing (Scaling, Normalization)
- Vectorized Cost Functions & Gradients
- Handling Datasets Efficiently
- Generating Random Numbers for Model Initialization
- Used internally by Pandas, TensorFlow, Scikit-learn
"""