# int
a = 1
# float
b = 3.0
Если один из операндов - float, то результат также float
# int
a = 1
# float
b = 3.0
c = a + b
print(c)
4.0
a = 4
# Так не стоит делать
b, c = 5, 6.
a ** b
1024
# Деление
a = 17 / 3
print(a)
# Целочисленное деление
a = 17 // 3
print(a)
# Остаток от деления
a = 17 % 3
print(a)
5.666666666666667 5 2
s = "asxasxdc"
a = "bbb"
s + a
'asxasxdcbbb'
a * 3
'bbbbbbbbb'
print(s[3])
print(s[:3])
print(s[4:])
a asx sxdc
a = 4
b = 10 / 3.
s = "a: {0} | b: {1:.2f}".format(a, b)
print(s)
a: 4 | b: 3.33
a = list()
a.append(1)
a.append(2)
a.append("sdcd")
print(a)
[1, 2, 'sdcd']
b = ["xaxs", "fff", "kek"]
a.extend(b)
print(a)
[1, 2, 'sdcd', 'xaxs', 'fff', 'kek']
a.remove("xaxs")
print(a)
[1, 2, 'sdcd', 'fff', 'kek']
len(a)
5
t_example = (0, 7, 42)
t_example
(0, 7, 42)
t_example[0] = 4
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-82-cb48f2a4d32c> in <module>() ----> 1 t_example[0] = 4 TypeError: 'tuple' object does not support item assignment
set_example = set([1, 2, 3, 2, 1, "asx", "asx"])
set_example
{1, 2, 3, 'asx'}
set_example.add(42)
set_example
{1, 2, 3, 42, 'asx'}
dict_example = dict()
dict_example["universe_number"] = 42
dict_example[0] = 1
dict_example
{'universe_number': 42, 0: 1}
dict_example["universe_number"]
42
a = 2
b = 3
if a > b:
print("Yippee")
else:
print("Bingo")
Bingo
if a > b:
print("Yippee1")
elif a < b:
print("Yippee2")
else:
print("Yippee3")
Yippee2
c = 3
if a < b and not b < c:
print("Yippee")
else:
print("Bingo")
Yippee
$range$ - функция, позволяющая получить элементы арифметической прогрессии
list(range(0, 10, 2))
[0, 2, 4, 6, 8]
list(range(5))
[0, 1, 2, 3, 4]
for i in range(4):
print(i)
0 1 2 3
Можно итерироваться по другим спискам (и не только!)
raw_list = ["a", "b", 1, 42, 666, [100, 1]]
for l in raw_list:
print(l)
a b 1 42 666 [100, 1]
i = 0
while i < 4:
print(i)
i += 1
0 1 2 3
i = 0
while i < 100:
i += 1
if i == 2:
continue
if i == 5:
break
print(i)
1 3 4
def pow(x, power):
return x ** power
pow(2, 4)
16
def pow_named(x, power=1, bound=100):
res = x ** power
if res < bound:
print(res)
else:
print(bound)
pow_named(3, 10, 100)
100
pow_named(3, 10, 1000000)
59049
pow_named(3, 10)
100
Именованные арументы можно менять местами
pow_named(3, bound=10, power=3)
10
import numpy as np
s = np.array([1, 2, 3])
A = np.array([
[1, 3, 0],
[0, 5, 0],
[0, 1, -4],
])
print(s)
print(s[:2])
print(s[:3])
[1 2 3] [1 2] [1 2 3]
print(A.mean())
print(A.max())
print(A.min())
print(A.sum())
print((A > 2).any(axis=0))
0.6666666666666666 5 -4 6 [False True False]
x = np.array([1, 0, 1])
y = np.array([0, 1, 0])
np.linalg.norm(x - y)
1.7320508075688772
A = np.array([
[1, 1, 0],
[0, 1, 0],
[0, 1, 1],
])
b = np.array([2, 5, 9])
np.matmul(A, b)
array([ 7, 5, 14])
A = np.array([
[1, 1, 0],
[0, 1, 0],
[0, 1, 1],
])
B = np.array([
[3, 4, 5],
[4, 9, 3],
[4, 4, 1],
])
A * B
array([[3, 4, 0], [0, 9, 0], [0, 4, 1]])
A @ B
array([[ 7, 13, 8], [ 4, 9, 3], [ 8, 13, 4]])
A * 3
array([[3, 3, 0], [0, 3, 0], [0, 3, 3]])
A.transpose()
array([[1, 0, 0], [1, 1, 1], [0, 0, 1]])
A.trace()
3
$$ Ax = b$$
$$ \begin{bmatrix} 1 & 1 & 0 \\ 0 & 1 & 0 \\ 0 & 1 & 1 \\ \end{bmatrix} \ x = \ \begin{bmatrix} 2 \\ 5 \\ 9 \\ \end{bmatrix}$$
np.linalg.solve(A, b)
array([-3., 5., 4.])
np.matmul(A, np.linalg.solve(A, b))
array([2., 5., 9.])
B
array([[3, 4, 5], [4, 9, 3], [4, 4, 1]])
np.linalg.det(B)
-77.00000000000001
np.linalg.svd(B)
(array([[-0.49232178, -0.86625851, -0.08494381], [-0.76794208, 0.38634941, 0.51088071], [-0.40973677, 0.31674962, -0.85544459]]), array([13.25647609, 3.13617665, 1.85209012]), array([[-0.46676664, -0.79355274, -0.39038821], [ 0.06811497, 0.4078562 , -0.91050188], [-0.88175351, 0.45158318, 0.13632085]]))
x = np.array([1, 2, 3])
print(np.sin(x))
print(np.exp(x))
print(np.arccosh(x))
[0.84147098 0.90929743 0.14112001] [ 2.71828183 7.3890561 20.08553692] [0. 1.3169579 1.76274717]
import pandas as pd
data = pd.read_csv("./data/student-por.csv", sep=";")
data.head()
school | sex | age | address | famsize | Pstatus | Medu | Fedu | Mjob | Fjob | ... | famrel | freetime | goout | Dalc | Walc | health | absences | G1 | G2 | G3 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | GP | F | 18 | U | GT3 | A | 4 | 4 | at_home | teacher | ... | 4 | 3 | 4 | 1 | 1 | 3 | 4 | 0 | 11 | 11 |
1 | GP | F | 17 | U | GT3 | T | 1 | 1 | at_home | other | ... | 5 | 3 | 3 | 1 | 1 | 3 | 2 | 9 | 11 | 11 |
2 | GP | NaN | 15 | U | LE3 | T | 1 | 1 | at_home | other | ... | 4 | 3 | 2 | 2 | 3 | 3 | 6 | 12 | 13 | 12 |
3 | GP | F | 15 | U | GT3 | T | 4 | 2 | health | services | ... | 3 | 2 | 2 | 1 | 1 | 5 | 0 | 14 | 14 | 14 |
4 | GP | F | 16 | U | GT3 | T | 3 | 3 | other | other | ... | 4 | 3 | 2 | 1 | 2 | 5 | 0 | 11 | 13 | 13 |
5 rows × 33 columns
data["G4"] = data["G1"] + data["G2"]
data.head()
school | sex | age | address | famsize | Pstatus | Medu | Fedu | Mjob | Fjob | ... | freetime | goout | Dalc | Walc | health | absences | G1 | G2 | G3 | G4 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | GP | F | 18 | U | GT3 | A | 4 | 4 | at_home | teacher | ... | 3 | 4 | 1 | 1 | 3 | 4 | 0 | 11 | 11 | 11 |
1 | GP | F | 17 | U | GT3 | T | 1 | 1 | at_home | other | ... | 3 | 3 | 1 | 1 | 3 | 2 | 9 | 11 | 11 | 20 |
2 | GP | NaN | 15 | U | LE3 | T | 1 | 1 | at_home | other | ... | 3 | 2 | 2 | 3 | 3 | 6 | 12 | 13 | 12 | 25 |
3 | GP | F | 15 | U | GT3 | T | 4 | 2 | health | services | ... | 2 | 2 | 1 | 1 | 5 | 0 | 14 | 14 | 14 | 28 |
4 | GP | F | 16 | U | GT3 | T | 3 | 3 | other | other | ... | 3 | 2 | 1 | 2 | 5 | 0 | 11 | 13 | 13 | 24 |
5 rows × 34 columns
data = data.dropna()
data.head()
school | sex | age | address | famsize | Pstatus | Medu | Fedu | Mjob | Fjob | ... | freetime | goout | Dalc | Walc | health | absences | G1 | G2 | G3 | G4 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | GP | F | 18 | U | GT3 | A | 4 | 4 | at_home | teacher | ... | 3 | 4 | 1 | 1 | 3 | 4 | 0 | 11 | 11 | 11 |
1 | GP | F | 17 | U | GT3 | T | 1 | 1 | at_home | other | ... | 3 | 3 | 1 | 1 | 3 | 2 | 9 | 11 | 11 | 20 |
3 | GP | F | 15 | U | GT3 | T | 4 | 2 | health | services | ... | 2 | 2 | 1 | 1 | 5 | 0 | 14 | 14 | 14 | 28 |
4 | GP | F | 16 | U | GT3 | T | 3 | 3 | other | other | ... | 3 | 2 | 1 | 2 | 5 | 0 | 11 | 13 | 13 | 24 |
6 | GP | M | 16 | U | LE3 | T | 2 | 2 | other | other | ... | 4 | 4 | 1 | 1 | 3 | 0 | 13 | 12 | 13 | 25 |
5 rows × 34 columns
Аналогично срезам в numpy
data.loc[:5, :"Pstatus"]
school | sex | age | address | famsize | Pstatus | |
---|---|---|---|---|---|---|
0 | GP | F | 18 | U | GT3 | A |
1 | GP | F | 17 | U | GT3 | T |
3 | GP | F | 15 | U | GT3 | T |
4 | GP | F | 16 | U | GT3 | T |
data.groupby("age").mean()
Medu | Fedu | traveltime | studytime | failures | famrel | freetime | goout | Dalc | Walc | health | absences | G1 | G2 | G3 | G4 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
age | ||||||||||||||||
15 | 2.765766 | 2.513514 | 1.522523 | 2.009009 | 0.036036 | 4.000000 | 3.261261 | 2.855856 | 1.378378 | 2.000000 | 3.486486 | 2.891892 | 11.756757 | 11.819820 | 12.108108 | 23.576577 |
16 | 2.505682 | 2.363636 | 1.573864 | 1.869318 | 0.119318 | 3.835227 | 3.130682 | 3.181818 | 1.397727 | 2.232955 | 3.534091 | 2.943182 | 11.721591 | 11.636364 | 11.988636 | 23.357955 |
17 | 2.491620 | 2.240223 | 1.553073 | 1.899441 | 0.217877 | 4.022346 | 3.206704 | 3.262570 | 1.553073 | 2.424581 | 3.670391 | 4.083799 | 11.519553 | 11.849162 | 12.268156 | 23.368715 |
18 | 2.492857 | 2.314286 | 1.614286 | 2.014286 | 0.285714 | 3.928571 | 3.071429 | 3.350000 | 1.564286 | 2.385714 | 3.392857 | 4.178571 | 11.200000 | 11.450000 | 11.771429 | 22.650000 |
19 | 2.031250 | 1.843750 | 1.593750 | 1.843750 | 0.875000 | 3.718750 | 3.468750 | 3.343750 | 1.781250 | 2.156250 | 3.562500 | 4.187500 | 9.031250 | 9.312500 | 9.531250 | 18.343750 |
20 | 1.833333 | 1.333333 | 1.833333 | 1.666667 | 0.833333 | 3.500000 | 3.166667 | 2.666667 | 1.333333 | 2.500000 | 3.666667 | 6.833333 | 10.333333 | 12.000000 | 12.000000 | 22.333333 |
21 | 2.500000 | 2.500000 | 1.500000 | 2.500000 | 2.000000 | 4.000000 | 3.000000 | 2.500000 | 3.000000 | 1.500000 | 4.500000 | 10.500000 | 9.000000 | 11.000000 | 11.000000 | 20.000000 |
22 | 3.000000 | 1.000000 | 1.000000 | 1.000000 | 3.000000 | 5.000000 | 4.000000 | 5.000000 | 5.000000 | 5.000000 | 1.000000 | 12.000000 | 7.000000 | 8.000000 | 5.000000 | 15.000000 |
import matplotlib.pyplot as plt
x = np.linspace(-5, 5, 100)
y = x ** 2.
plt.figure(figsize=(10, 7))
plt.plot(x, y)
plt.title("$y = x^2$")
plt.xlabel("x")
plt.ylabel("x")
plt.show();
t = np.linspace(0, 2 * np.pi, 100)
plt.figure(figsize=(7, 7))
plt.plot(np.sin(2 * t), np.cos(3 * t))
plt.show();