-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumpy.py
216 lines (142 loc) · 4.53 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
208
209
210
211
212
213
214
215
216
# Example Numpy Code by Mohammad fayzi
# Install Numpy:Install it using this command:
# pip
# pip3
pip install numpy
pip3 install numpy
# Import Numpy : show version numpy
# NumPy is usually imported under the np alias.
import numpy as np
np.__version__
# Create a new arrray by Numpy
arr = np.array([1, 2, 3, 4])
# print(arr)
print(arr.shape)
# Use a tuple to create a NumPy array:
arr_tuple = np.array((1 ,2 ,3 , 4, 5, 6))
print(arr_tuple)
# Create a NumPy ndarray Object
arr_2_D = np.array([[10, 20, 30],[200, 300, 400]])
print(arr_2_D)
# Get the Shape of an Array
print(arr_2_D.shape)
#Access Array Elements
# Array indexing is the same as accessing an array element
arr_l = np.array([1, 2, 3, 4])
print(arr_l[0])
# Slicing arrays
# Slicing in python means taking elements from one given index to another given index.
# We pass slice instead of index like this: [start:end].
# We can also define the step, like this: [start:end:step].
arr_b = np.array([[1, 2, 3],[6, 7, 8],[9, 10, 11]])
print(arr_b[1:2])
#Checking the Data Type of an Array
arr_b = np.array([[1, 2, 3],[6, 7, 8],[9, 10, 11]])
print(arr_b.dtype)
# Data Types in NumPy
# NumPy has some extra data types, and refer to data types with one character, like i for integers, u for unsigned integers etc.
# Below is a list of all data types in NumPy and the characters used to represent them.
# i - integer
# b - boolean
# u - unsigned integer
# f - float
# c - complex float
# m - timedelta
# M - datetime
# O - object
# S - string
# U - unicode string
# V - fixed chunk of memory for other type ( void )
# Create an array with data type String:
arr_c = np.array(['ali', 'mohammad','hasan'],dtype='S')
print(arr_c.dtype)
# Reshape Numpy
# Convert the following 1-D array with 12 elements into a 2-D array.
# The outermost dimension will have 4 arrays, each with 3 elements:
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3)
print(newarr)
# Convert the following 1-D array with 12 elements into a 3-D array.
# The outermost dimension will have 2 arrays that contains 3 arrays, each with 2 elements:
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(2, 3, 2)
print(newarr)
# Check if the returned array is a copy or a view:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
arr.reshape(2, 4).base
# Iterate on the elements of the following 2-D array:
arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in arr:
print(x)
# Joining NumPy Arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)
# Iterating Arrays
# Iterating means going through elements one by one.
# As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop of python.
# If we iterate on a 1-D array it will go through each element one by one.
arr = np.array([1, 2, 3, 4, 5])
for x in arr:
print(x)
# 2-D Array
arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in arr:
for y in x:
print(y)
# Split the 2-D array into three 2-D arrays.
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr[0])
print(newarr[1])
print(newarr[2])
# Searching Arrays
# You can search an array for a certain value, and return the indexes that get a match.
# To search an array, use the where() method.
arr = np.array([1, 2, 3, 4, 5, 4, 4])
x = np.where(arr == 4)
print(x)
# Sorting Arrays
arr = np.array([[3, 2, 4], [5, 0, 1]])
print(np.sort(arr))
# Filter Array
arr = np.array([41, 42, 43, 44])
filter_arr = arr > 42
newarr = arr[filter_arr]
print(filter_arr)
print(newarr)
#Generate Random Array
#Arange
rand_3 = np.arange(1, 20, step=2)
print(rand_3)
#Linspace
rand_4 = np.linspace(1, 2 ,num=10)
print(rand_4)
#import random
from numpy import random
#rand
rand_arr = random.rand(3, 5)
print(rand_arr)
#Randint
rand_1 = random.randint(100, size=4)
print(rand_1)
# Choice
x = random.choice([3, 5, 7, 9], size=(3, 5))
print(x)
#Operation Numpy Array
arr_1 = random.rand(2, 2)
arr_2 = random.rand(2,2)
print(np.add(arr_1, arr_2))
print(np.subtract(arr_1, arr_2))
print(np.sqrt(arr_1, arr_2))
print(np.divide(arr_1, arr_2))
#And (max, min, sum)
arr.max()
arr.min()
arr.sum()
#Sort
np.sort(arr)
#Join Array (Concat)
np.concatenate(arr_1, arr_2)