-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumPy.py
207 lines (168 loc) · 4.17 KB
/
NumPy.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# -*- coding: utf-8 -*-
"""SFDS_0002_Exp1.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1EVps-M6p5pL2mPU7V7EYkQymR5GK9pyY
"""
import numpy as np
a1=np.array([2,4,6,8,10])
a2=np.array([3,5,7,9,11])
sum=a1+a2
print("The addition of two numpy arrays:",sum)
import numpy as np #1D array
a1=np.array([2,4,6,8,10])
print(a1)
import numpy as np #2D array
a1=np.array([[1,2,3],[4,5,6]])
print(a1)
import numpy as np
a1=np.array([2,4,6,8,10])
print(a1)
a2=np.zeros((2,3))
print(a2)
a3=np.ones((2,3))
print(a3)
a4=np.arange(0,10,2)
print(a4)
a5=np.linspace(0,1,5)
print(a5)
a6=np.random.rand(2,2)
print(a6)
"""# Array Multiplication"""
import numpy as np
a1=np.array([2,4,6,8,10])
a2=np.array([3,5,7,9,11])
m1=np.dot(a1,a2)
print(m1)
m2=np.matmul(a1,a2)
print(m2)
m3=np.multiply(a1,a2)
print(m3)
m4=a1*a2
print(m4)
"""# MATHEMATICAL FUNCTIONS
---
**bold text**
"""
import numpy as np
a=np.pi/4
sinval=np.sin(a)
print(sinval)
cosval=np.cos(a)
print(cosval)
tanval=np.tan(a)
print(tanval)
"""# **Statistical Measures**"""
import numpy as np
a1=np.array([2,4,6,8,10])
me=np.mean(a1)
med=np.median(a1)
mini=np.min(a1)
maxi=np.max(a1)
stdev=np.std(a1)
vari=np.var(a1)
s=np.sum(a1)
p=np.prod(a1)
print(me)
print(med)
print(mini)
print(maxi)
print(s)
print(p)
print(stdev)
print(vari)
"""# **Broadcast**"""
import numpy as np
a1=np.array([1,2,3,4,5])
a1+=5
print(a1)
"""# **Indexing and Slicing**"""
import numpy as np
a1=np.array([1,2,3,4,5])
x=a1[4]
print(x)
y=a1[2:4]
print(y)
print(a1[-4:-2])
"""# **Linear Algebra**"""
import numpy as np #2D array
a1=np.array([[1,2],[3,4]])
a2=np.array([[2,4],[6,8]])
r=np.linalg.inv(a1)@a2
print(r)
"""OPERATION:ADDITION"""
import numpy as np #1D
a1=np.array([1,2,3,4,5,6,7,8,9,10])
print("Original Array:",a1)
a2=np.copy(a1)
print("Copied Array:",a2)
import numpy as np #2D
a1=np.array([[1,2,3,4],[4,3,2,1]])
print("Original Array:",a1)
a2=np.copy(a1)
print("Copied Array:",a2)
import numpy as np #1D
a1=np.array([1,2,3,4,5,6,7,8,9,10])
print("Original Array:",a1)
a2=a1
print("Assigned:",a2)
"""Resizing a numpy array"""
import numpy as np
a=np.array([2,4,6,8,10,12])
print("Resize:",a)
r=np.resize(a,(3,4))
print("Resized:",r)
import numpy as np
a=np.array([2,4,6,8,10,12])
print("Resize:",a)
a.resize(3,4)
print(a)
"""Numpy Flatten"""
import numpy as np
l1 = [1, 2, 3]
l2 = [5, 6, 7]
a = np.array([l1, l2])
print(a.flatten())
"""Dealing with missing values and fill in with with missing values
Data missing is an common issue that data analyist and scientist face when working with dataset.These missing values can occur due to various reasons such as data collection errors,sensor malfunctions or because the data was not available at the time of measurement
In numpy missing values a represented using a special value "np.nan(NotANumber)this value represents invalid or missing data and can be easily identified within an array . np.nanis a floating point value so any array containing it will be coerced to floating point data type
Identifying missing values
np.isnan() is a method that returns a boolean array highlighting the positions of missing values
"""
import numpy as np
a=np.array([1,2,np.nan,4,np.nan,5])
miss=np.isnan(a)
print(miss)
#removing missing values
a2=a[~np.isnan(a)]
print(a2)
"""To handle missing values is to remove from the data set.np.isnan function along with boolean indexing is used to remove missing values
~ represents to create a boolean array that selects the elements that are not NaN
Replacing missing values
"""
import numpy as np
a=np.array([1,2,np.nan,4,np.nan,5])
replace=np.nan_to_num(a,nan=-1)
print(replace)
"""to replace nan values with other specified values we can use np.nan_to_num() function.In this case we replace the missing values with -1 which can be any value of choice"""
import numpy as np
a=np.array([1,2,np.nan,4,np.nan,5])
s=np.sum(a)
means=np.nanmean(a)
print(s)
print(means)
import numpy as np
a=np.array([[1,2,np.nan,5],[4,np.nan,6,np.nan]])
miss=np.isnan(a)
print(miss)
#removing missing values
a2=a[~np.isnan(a)]
print(a2)
#replacing
replace=np.nan_to_num(a,nan=-1)
print(replace)
#sum and mean
s=np.sum(a)
means=np.nanmean(a)
print(s)
print(means)