-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorial 8-Python Numpy Arrays.py
More file actions
125 lines (121 loc) · 3.39 KB
/
Tutorial 8-Python Numpy Arrays.py
File metadata and controls
125 lines (121 loc) · 3.39 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
-------------------------------------------------------------------------Tutorial 8-Python Numpy Arrays-------------------------------------------------------------------------------------------
#Numpy Tutorials
#NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python
##################################################################################What is an array########################################################################3
#An array is a data structure that stores values of same data type. In Python, this is the main difference between arrays and lists. While python lists can contain values corresponding to different data types, arrays in python can only contain values corresponding to same data type
## import the library
import numpy as np
lst=[1,2,3,4]
arr=np.array(lst)
type(arr)
numpy.ndarray
arr.shape
(4,)
lst1=[1,2,3,4,5]
lst2=[2,3,4,5,6]
lst3=[3,4,5,6,7]
arr1=np.array([lst1,lst2,lst3])
arr1
array([[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7]])
arr1.shape
(3, 5)
arr
array([1, 2, 3, 4])
#indexing
arr[3]
4
arr[3]=5
arr
array([1, 2, 3, 5])
arr[-1]
5
arr[:-1]
array([1, 2, 3])
arr[::-3]
array([5, 1])
arr1
array([[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7]])
arr1[:,3:].shape
(3, 2)
arr1[:,1]
array([2, 3, 4])
arr1[1:,1:3]
array([[3, 4],
[4, 5]])
arr1[1:,3:]
array([[5, 6],
[6, 7]])
##EDA
arr
array([1, 2, 3, 5])
arr[arr<2]
array([1])
arr1
array([[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7]])
arr1.reshape(5,3)
array([[1, 2, 3],
[4, 5, 2],
[3, 4, 5],
[6, 3, 4],
[5, 6, 7]])
##mechanism to create an array
np.arange(1,20,2).reshape(2,5)
array([[ 1, 3, 5, 7, 9],
[11, 13, 15, 17, 19]])
np.arange(1,20,2).reshape(2,5,1)
array([[[ 1],
[ 3],
[ 5],
[ 7],
[ 9]],
[[11],
[13],
[15],
[17],
[19]]])
arr *arr
array([ 1, 4, 9, 25])
arr1 * arr1
array([[ 1, 4, 9, 16, 25],
[ 4, 9, 16, 25, 36],
[ 9, 16, 25, 36, 49]])
np.ones((5,3))
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
np.zeros((4,5))
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
np.random.randint(10,50,4).reshape(2,2)
array([[42, 24],
[42, 27]])
np.random.randn(5,6)
array([[-1.81506768, -0.18073426, 1.50531807, 0.04701715, 0.00345232,
-1.89048526],
[-1.19316745, -0.8222509 , -0.11386853, -1.47422866, 2.28291196,
0.02731563],
[ 0.4027173 , 0.02478252, -1.44510603, -0.99842738, -0.07634666,
-0.81335324],
[ 0.9692424 , 1.41109806, -0.33335031, 1.18717669, 0.59522992,
0.88923165],
[ 1.10478558, -0.44639422, 0.06939313, 0.64813885, 0.54669759,
-0.19369955]])
np.random.random_sample((4,7))
array([[0.13871895, 0.58456466, 0.31541441, 0.67910295, 0.1837086 ,
0.08103746, 0.50976945],
[0.67483078, 0.41252643, 0.0877961 , 0.92338101, 0.25762097,
0.55850649, 0.26534539],
[0.05930126, 0.53209293, 0.90407746, 0.71743787, 0.89905166,
0.22360872, 0.10365036],
[0.36820358, 0.30483507, 0.49569278, 0.7850211 , 0.96484935,
0.72614993, 0.83010606]])