-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python-first steps
307 lines (247 loc) · 6.88 KB
/
Python-first steps
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
"""
Sources: https://learn.udacity.com/courses/ud170/
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime
def construct_plot(dx = range(-10, 11)):
print(f'dx: {dx}')
dy = [10 ** i for i in dx]
print(f'dy: {dy}')
plt.scatter(dx, dy)
plt.xlabel('axa X')
plt.ylabel('axa Y')
plt.title('plot demo')
print('Afisare plot in SciView')
# plt.show()
print('Salvare imagine in format .jpg')
current_time = datetime.datetime.now().strftime("%Y-%m-%d_%H_%M_%S")
print(f'Data curenta: {current_time}')
plt.savefig('./grafice/' + current_time + '.jpg')
def dictionaries():
dic={'a':['mare', 'soare', 4], 'b':[10, 30, 40]}
print(f'Intregul dictionar: {dic}')
list_Key =list(dic.keys())
print(f"cheia de la indxul 0: {list_Key[0]}")
print(f"valorile de la cheia a si de index 2 dictionar: {dic['a'][1]}")
print("*********************************************")
print("Adaugarea unei valori la valorile cheii 'a'")
dic['a'].append("new value")
print(f'noul dictionar: {dic}')
print("*********************************************")
print("inserare pe o pozitie dorita in b: ")
dic['b'].insert( 1, 4)
v = list(dic.values())
print(f'New values: {v[1]}')
def functii_numpy():
list = [7, 1181, 5]
print(list)
print(f'Suma:{np.sum(list)}')
print(f'length:{np.size(list)} ')
print(f'Media: {np.mean(list)}')
print(f'Deviatia standaard: {np.std(list)}')
print(f'Min:{np.min(list)}')
print(f'Max:{np.max(list)}')
def histograma():
list=[20, 2, 67]
plt.hist(list, bins=8)
plt.show()
def numpy_function():
array1 = np.array(['white','green', 'cyan', 'black', 'orange'])
print(f'array generate from np.array: {array1}')
print("******************* Accesare element ******************")
print(f'[0:2]{array1[0:2]}')
print(f'[:2]{array1[:2]}')
print(f'[1:2]{array1[1:2]}')
print(f'[2:]{array1[2:]}')
print(f'[:]{array1[:]}')
def numpy_element_types():
print(np.array(['sun', 'clouds']).dtype) # >U6
print(np.array([1, 2]).dtype) # int32
print(np.array([1.87, 2.33]).dtype) # float64
print(np.array([True, False]).dtype) # bool
def multiplying():
print(f'multilying in Pyuhon: {[1, 2]*4}')
print(f'multilying in NUMPY: {np.array([1, 2])*4}')
def add():
print(f'Add in Python: {[1, 2]+[10, 20]}')
print(f'Add in NUMPY: {np.array([1, 2])+np.array([10, 20])}')
def arithmetic_opperation():
a = np.array([3, 5, 7])
b = np.array([2, 4, 2])
print(a+b)
print(a-b)
print(a/b)
print(a*b)
print(a**b)
def arithmetic_opperation_scalar():
a = np.array([3, 5, 7])
b = 2
print(a+b)
print(a-b)
print(a/b)
print(a*b)
print(a**b)
def logic():
a = np.array([1, 2, 3, 4, 5])
b = np.array([5, 4, 3, 2, 1])
print(a > b)
print(a < b)
print(a == b)
print(a == b)
print(a != b)
def logic2():
a = np.array([1, 2, 3, 4])
b = 2
print(a > b)
print(a < b)
print(a == b)
print(a == b)
print(a != b)
def index_arrays():
a = np.array([1, 2, 3, 4])
b = np.array([True, True, False, False])
print(a[b])
print(a[a>3])
def comp():
a = np.array([3, 5, 7])
b = np.array([2, 4, 2])
a+=b
print(a)
a = np.array([3, 5, 7])
b = np.array([2, 4, 2])
a= a+b
print(a)
def tablou_bidimensional():
vector_bi = np.array([
[1, 2, 3, 4, 5],
[10, 20, 30, 40, 50],
[100, 200, 300, 400, 500],
[800, 900, 1000, 1100, 1200]])
print(f'Intregul tablou bidimendional{vector_bi}\n')
print(f'Preluarea elemntului de pe linia 1, coloana 2: {vector_bi[1, 2]}\n')
print(f'Toate elementele de pe linia 1: {vector_bi[1, :]}\n')
print(f'Liniile 1-->3, elementele de pe indexul 1-->2-1:\n {vector_bi[1:3, 1:2]}\n')
def op_aritmetice_array_bidimensionale():
a = np.array([
[1, 2],
[3, 4]
])
b = np.array([
[10, 20],
[30, 40]
])
print(f'Adunarea matricilor:\n {a+b} ')
print(f'Scaderea matricilor:\n {a-b} ')
print(f'Inmultirea:\n {a*b} ')
print(f'Inmultirea matricilor:\n {np.matmul(a,b)} ')
def dataFrame_function():
tabel=pd.DataFrame(
{
'an': [2002, 2000],
'varsta': [21, 23]
}
)
print(tabel)
print(f'Accesare inregistrare cu index:1:\n {tabel.loc[1]}')
print(f'Accesare toate val:\n {tabel.loc[:]}')
def dataFrame_sum():
tabel=pd.DataFrame(
{
'an': [2002, 2000],
'varsta': [21, 23]
}
)
print(f'{tabel.sum()}')
print(tabel.values.sum())
print(tabel.applymap(tabel.apply(np.mean)))
def serii():
a = np.array([
[[11, 12], [13, 14]],
[[21, 22], [23, 24]]
])
print(a)
def dimensiune_array():
zerodimensional = np.array(1)
unidimensional = np.array([1, 2, 3, 4])
bidimensional = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8]
])
tridimensional = np.array([
[
[1, 2, 3, 4],
[5, 6, 7, 8]],
[
[10, 20, 30, 40],
[50, 60, 70, 80]]
])
print(f'dimensiune1: {zerodimensional.ndim}')
print(f'dimensiune2: {unidimensional.ndim}')
print(f'dimensiune3: {bidimensional.ndim}')
print(f'dimensiune4: {tridimensional.ndim}')
def ndim_func():
multidimensiional = np.array([1,2], ndmin=5)
print(multidimensiional)
print(multidimensiional.ndim)
def concatenare():
l = np.append(np.array([17, -9]), np.array([8]))
print(l)
def conversie():
a = np.array(['1', '2'], dtype='i')
print(a)
b = np.array([2, 4], dtype='S')
print(b)
def copy_view():
old = np.array([1, 2])
new = old.copy()
new[0]=100
print("Folosire: copy()")
print(old)
print(new)
new_view = old.view()
new_view[0]=23
print("\nFolosire: view()")
print(new_view)
print(old)
def shape():
a = np.array([[1, 2], [2, 4]])
print(a.shape)
ne = a.reshape(-1)
print(ne)
def iterare():
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
for i in np.nditer(arr):
print(i)
def concatenare():
a = np.array([1, 2])
b = np.array([3, 4])
r = np.concatenate((a, b))
print(r)
v = np.stack((a, b), axis=1)
def split_hsplit():
a = np.array([[1, 2],[3, 4],[5, 6], [7, 8]])
print(np.array_split(a, 2))
print(np.hsplit(a, 2))
def search():
a = np.array([1, 2, 2, 5, 8, 2])
rez = np.where(a%2 == 0)
print(rez)
def sort_func():
a = np.array([1, 'z', 10, 2, 'a',-1])
v = np.sort(a)
print(v)
def serii():
a = [2.3, 3.9, 98.8]
new = pd.Series(a, index=["inaltime", "masa", "others"])
print(new)
print(f'\ninaltime: {new["inaltime"]}')
def pd_to_string():
data = {"a":[1, 2],
"b": [3, 4]
}
a = pd.DataFrame(data)
print(a)
print(a.to_string())
pd_to_string()