forked from karanpreet8082/Data-Science-with-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpy.py
More file actions
342 lines (184 loc) · 222 KB
/
numpy.py
File metadata and controls
342 lines (184 loc) · 222 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# -*- coding: utf-8 -*-
"""Numpy.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/154SW6ldF3r2LRSeV96GxRMIBNeeSLal_
# **Numpy**

##**NumPy** is a Python package. It stands for '**Numerical Python**'. It is a library consisting of multidimensional array.
##Some of python’s leading package rely on NumPy as a fundamental piece of their infrastructure (examples include scikit-learn, SciPy, pandas, and tensorflow).
## Why use Numpy?
* Numpy provided efficient storage
* Provides better ways of handling data for processing
* It is faster.
* Easy to learn
* Use less memory to store data
## Lets import numpy library.
"""
# Statement to import the numpy library
import numpy as np
""""""
# One-dimensional array of some prime numbers
prime_array = np.array([2,3,5,7,11])
print(prime_array)
type(prime_array)
prime_array.dtype
prime_array.shape
# This array has only one row there is no column as it is one dimensional array
# We can also change the datatype of an array
prime_array = np.array([2,3,5,7,11], np.int8)
prime_array.dtype
# Get the elements at the second position
prime_array[2]
# If it is a two dimensional array
myarr = np.array([[1, 3, 5], [2, 4, 6]])
myarr.shape
myarr.dtype
# TO get the position of an array
myarr[0, 2]
myarr[1,2]
# TO change the position of an array
myarr[1,2] = 7
myarr[1, 2]
myarr
# Create a two-dimensional array
another_array = np.array([[1,2],[3,4]])
print(another_array)
# We can also create 3 dimensional array
myarr = np.array([[[1]]])
myarr
myarr.shape
"""# ***Numpy Methods and Attributes***
## Conversion from other Python Structure example lists, tuples.
"""
# Creating array from the list
listarray = np.array([[1, 2, 3], [8, 4, 8], [4, 0, 9]])
listarray
listarray.dtype
listarray.shape
listarray.size
# We can also create array with the help of Dictionary
Dict = np.array({39, 41, 85})
Dict
# You can see here the type is object which not very efficient.
# Always remember that when we have to work with integers or a float values we use array.
"""## Instrinsic numpy array creation object example arange, ones, zeros
### By using Zeros function in numpy
"""
# Here we will use some function of numpy to built array
zeros = np.zeros((2, 5))
# This function will give you an array of zeros values
# It will have 2 rows and 5 columns.
zeros
zeros.shape
zeros.dtype
zeros.size
"""### By Using Arange method """
# Using arange function
# This function will give you numpy array.
# This function is similar to the range function in python.
Range = np.arange(15)
# This function will give you set of number from 0 to n-1
Range
"""### By using linspace method"""
# Using linspace function
lspace = np.linspace(1, 50, 10)
# This function will give 10 equally linearly spaced elements between 1 to 50.
lspace
lspace1 = np.linspace(1, 4, 4)
lspace1
"""### By using empty and empty_like method
This method will give you random values between the number of rows and columns
"""
emp = np.empty((4, 6))
# It will give you array of 4 rows and 6 columns
# This array will contains random values.
emp
# We can also create using empty_like method
emp_like = np.empty_like(lspace)
# Here it will give you new array of emp_like whose values is same as that of lspace array.
emp_like
"""### Identity method"""
# This method will give you an identity matrix
id = np.identity(45)
# Here it will give you 45 by 45 identity matrix
id
id.shape
id.size
id.dtype
"""### We can also break the array or in other words change the order of an array by using the reshape method.
### Note here that only those arrays can change their shape who having proper order.
"""
# Let's take an array of arr
arr = np.arange(99)
# It will give you array of 99 elements.
arr
# we can break it into 3*33 or 33*3 that will give you 99 elements
# Basically the product should be same as that of the original
# If this condition does not satisfy then it will give an error.
arr.reshape(3, 33)
# But our shape of our ;arr' does not change.
arr.shape
# For changing the shape of 'arr' we have to assign this
arr = arr.reshape(3, 33)
arr.shape
# TO make arr array as a 1 D array we can use ravel function
arr.ravel()
# But the arr is still b3 by 33 matrix
# To make it 1 D array we have to assign the arr values
arr = arr.ravel()
arr.shape
"""## **Numpy Axis**
NumPy axes are the directions along the rows and columns. Just like coordinate systems, NumPy arrays also have axes.
Axis in numpy array starts with 0(zero).
1 D array has only one axis that is axis 0.
2 D array has two axis that is axis 0 and axis 1.
Similarly for 3 D array. For more about axis click [here](https://www.sharpsightlabs.com/blog/numpy-axes-explained/)
"""
# Lets take an array
x = [[1, 2, 3], [4, 5, 6], [7, 1, 0]]
ar = np.array(x)
ar
ar.sum(axis=0) # axis 0 will add them in vertical direction
ar.sum(axis=1) # axis 1 will add them in horizontal direction
"""### If we want to create a transpose of an array """
ar
# Using T we can create the transpose of an array
ar.T
"""### If we want each and every element of the array then we can use flat function"""
ar.flat
for item in ar.flat:
print(item)
# It will give row-wise
"""### To see the number of dimensions of an array we can use ndim function."""
ar.ndim
ar.nbytes # It will give you the total bytes consume.
"""### To get the index of where your maximum and minimun elements lies we can use argmax and argmin function"""
# Let take a 1D array
one = np.array([1, 5 , 2, 493, 45, 17])
one.argmax()
# The maximum elemet lies in the 3rd index. In python counting begins from 0.
one.argmin()
"""### Similarly we have argsort method. It will give you are index values."""
one.argsort()
# This will give you the index values
# It shows that the lowest element is at index 0 then another lowest element is at index 2 and so on.
# At highest element is at index 3
"""### For 2 D array argmin, argmax and argsort method."""
ar
ar.argmax()
# At index 6 is our highest element.
# First it will make the array as 1 D array and then it will count
ar.argmin()
# Similarly at index 8 we have out lowest element.
# But if we provide the axis
ar.argmax(axis=0)
# It shows that in axis 0 that is in vertical direction
# We have our highest element at 2nd index that is 7
# THen in second column we have our highest value at index 1 that is 5 and so on.
# Similarly for axis 1
ar.argmax(axis=1)
# Similarly for argsort
ar.argsort(axis=0)
ar.argsort(axis=1)
"""#You can access the official documentation of numpy by clicking [here](https://docs.scipy.org/doc//numpy-1.17.0/reference/arrays.ndarray.html)"""