-
Notifications
You must be signed in to change notification settings - Fork 0
/
chap_numpy.py
300 lines (228 loc) · 5.93 KB
/
chap_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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 1 13:58:41 2020
@author: user
"""
--ch9--numpy
# number python
import numpy as np
# diffrence between a python list and a numpy array
# create 2 python lists with random numbers
lis1=[4,10,59,14,29]
lis2=[14,90,33,18,40]
lis1+lis2
arr1=np.array([4,10,59,14,29])
arr2=np.array([14,90,33,18,40])
arr1
arr2
type(arr1)
type(lis1)
type(arr2)
# add the 2 arrays - adds elements-wise and returns the individual sum
arr1+arr2
# to get all the functions of numpy
dir(np)
# concatenate arrays into one
np.concatenate([arr1,arr2])
# create multiple random integers
wt = np.random.randint(5,100,30)
len(wt)
print(wt)
# append values to an existing array
wt = np.append(wt,np.random.randint(40,75,12))
len(wt)
print(wt)
# element-wise arithmetic in a single line
wt+1.5
wt-2.5
# numpy does not support python functions
round(arr1/3,2)
np.round(arr1/3,2)
print(np.__version__)
# diffrence between python and numpy code
result = []
%timeit [i**2 for i in range(1,100001)]
num = np.arange(1,10001)
%timeit num**2
# area of circle
radius = np.array([1.2,1.4,2.3,4,5])
np.round(np.pi * radius**2,2)
# sequence of numbers
np.arange(5)
# 0 to 4
# 1-11 with diffrence of 4
np.arange(1,11,4)
# convert a 1-d array into a 2-d array
arr1 = np.arange(1,21)
print(arr1)
arr1.shape
# reshape() converts an array into a new shape by specifying the rows & columns
arr1 = arr1.reshape(4,5)
print(arr1)
arr1.ndim
arr1 = arr1.reshape(10,2)
print(arr1)
# when rows are not known and column are known, then how to reshape
arr2 = np.arange(1,45)
# assume that the number of columns=4
# the row parameter is given as -1 to indicate it is dynamic
arr2 = arr2.reshape(-1,4)
arr2.shape
# simillary we can fix rows and make columns dynamic
arr3=np.arange(1,25)
# fix rows=4
arr3=arr3.reshape(4,-1)
arr3
# all in a single line
np.arange(1,21).reshape(-1,5)
# crate 30 random numpy integers and convert it into a matrix having 5 columns
np.random.randint(100,250,30).reshape(-1,5)
# query on np array
arr4=np.random.randint(1,101,45)
# get all numbers > 70
arr4[arr4>70]
# get all the even numbers
arr4[arr4%2==0]
# transpose (rows become column, and colum becomes rows)
arr3
arr3.T
# random float numbers in numpy
# i) between 0-1 (used in probability)
np.random.ranf(5)
np.round(np.random.ranf(5),2)
# ii) between any 2 values
np.round(np.random.uniform(10,20,6),3)
# sequence of numbers between 2 float numbers
np.round(np.linspace(1,5,10),3)
# python object can be converted to numpy
list1=[1,4,19,20]
type(list1)
# convert python list to numpy
list1=np.asarray(list1)
type(list1)
# pre-defined array
# create an empty numpy array and initialise with random numbers (int/float)
np.empty([2,3],np.int32)
np.empty([2,3],np.int64)
np.empty([4,5],np.float32)
# create and initialise arrays with 0/1
np.zeros([4,6],np.int32)
np.zeros([4,6],np.float32)
np.ones([3,3],np.int32)
np.ones([3,3],np.float32)
# to get index positions of array matching a condition
# get the index positions of numbers that are >50
np.where(arr4>50)
arr4[2]
arr4[1]
# to get the actual values matching the index position
arr4[np.where(arr4>50)]
# a2)
# get all the numbers from arr4 that are between 40 and 70
ans=arr4[np.where(arr4>=40)and(arr4<=70)]
print(ans)
# ascending sort
np.sort(ans)
# descending sort
np.sort(-ans)
-1*np.sort(-ans)
# create unique values
pop = np.arange(1000)
# take a sample of 25 unique values from a population of 1000 numbers
samp = np.random.choice(pop,25,replace=False)
np.sort(samp)
# accesing the numpy array
dataset=np.random.randint(5,100,40).reshape(-1,5)
print(dataset)
# get row1,all column
dataset[0]
dataset[0,0]
dataset[0,0:4]
dataset[0:3,0:2]
dataset[0,(0,4)]
dataset[4,2]
np.where(dataset==93)
# changing values in array
dataset[0,0]=143
print(dataset)
# axis
# sum of values of the array
dataset
np.sum(dataset)
# axis=1--> row operation
# axis=0 -> column operation
np.sum(dataset,axis=1)
# to get the sum of the first 3 rows
np.sum(dataset,axis=1)[0:3]
np.sum(dataset[0:3],axis=1)
# column-wise sum
np.sum(dataset,axis=0)
# find the max. value of every row and column
np.max(dataset,axis=1)
np.max(dataset,axis=0)
# find the min. value of every row and column
np.min(dataset,axis=1)
np.min(dataset,axis=0)
# avg value
np.mean(dataset)
ap=np.mean(dataset,axis=1)
np.max(ap)
# some other numpy function
# repeating a value
a=np.array([1,2,3])
np.repeat(a,4)
# shuffle the dataset
np.random.shuffle(samp)
samp
# array broadcast
marks=np.random.randint(1,100,15).reshape(-1,5)
marks
weights=np.array([0.15,0.1,0.1,0.05,0.6])
marks*weights
# assignment
# create a price list of a store that has 20 items split into 4 categories
# price range from 1000-9000
price=np.random.randint(1000,9000,20).reshape(-1,4)
price
pre=np.array([1.5,1.5,1.5,1.5,])
price*pre
pre=np.array([0.10,0.10,0.10,0.10])
# create a 3x3 matrix and initialise it will 0
# generate 9 random number between 0-1 (with a 2 decimal precision) and fill the matrix column-wise
mat=np.zeros([3,3],np.float32)
mat
np.round(np.random.uniform(0,1,9),2)
--- 2/12/2020---numpy
# multi dimension
num=np.random.randint(15,200,30).reshape(-1,5)
num
num=np.random.randint(10,300,100).reshape(5,5,4)
print(num)
num.ndim
# mathmatical functions
arr1=np.array([12,4,5,4])
arr2=np.array([1,2,3,2])
np.add(arr1,arr2)
np.subtract(arr2,arr1)
np.multiply(arr1,arr2)
np.divide(arr1,arr2)
dir(np)
# set operations
arr1=np.array([16,5,14,7])
arr2=np.array([5,19,12,7])
arr1
arr2
# intersection
np.intersect1d(arr1,arr2)
# diffrence
# a-b
np.setdiff1d(arr1,arr2)
# b-a
np.setdiff1d(arr2,arr1)
###########################################
import scipy as sp
dir(sp)
from scipy import linalg
dir(linalg)
from scipy import stats
dir(stats)