Fakultet u Zagrebu
Fakultet elektrotehnike i računarstva

Strojno učenje 2019/2020

http://www.fer.unizg.hr/predmet/su


Laboratorijska vježba 0: Uvod u Python

Verzija: 1.1
Zadnji put ažurirano: 27. 9. 2019.

(c) 2015-2019 Jan Šnajder, Domagoj Alagić

Objavljeno: 30. 9. 2019.
Rok za predaju: N/A


1. Python

1.1. Liste


In [1]:
xs = [5, 6, 2, 3]
xs


Out[1]:
[5, 6, 2, 3]

In [2]:
xs[0]


Out[2]:
5

In [3]:
xs[-1]


Out[3]:
3

In [4]:
xs[2] = 10
xs


Out[4]:
[5, 6, 10, 3]

In [5]:
xs[0] = "a book"
xs


Out[5]:
['a book', 6, 10, 3]

In [6]:
xs[1] = [3, 4]
xs


Out[6]:
['a book', [3, 4], 10, 3]

In [7]:
xs += [99, 100]
xs


Out[7]:
['a book', [3, 4], 10, 3, 99, 100]

In [8]:
xs.extend([22, 33])
xs


Out[8]:
['a book', [3, 4], 10, 3, 99, 100, 22, 33]

In [9]:
xs[-1]


Out[9]:
33

In [10]:
xs.pop()


Out[10]:
33

In [11]:
xs


Out[11]:
['a book', [3, 4], 10, 3, 99, 100, 22]

In [12]:
len(xs)


Out[12]:
7

In [13]:
xs[0:2]


Out[13]:
['a book', [3, 4]]

In [14]:
xs[2:]


Out[14]:
[10, 3, 99, 100, 22]

In [15]:
xs[:3]


Out[15]:
['a book', [3, 4], 10]

In [16]:
xs[:-2]


Out[16]:
['a book', [3, 4], 10, 3, 99]

In [17]:
for el in xs:
    print(el)


a book
[3, 4]
10
3
99
100
22

In [18]:
for idx, el in enumerate(xs):
    print(idx, el)


0 a book
1 [3, 4]
2 10
3 3
4 99
5 100
6 22

In [19]:
for idx in range(len(xs)):
    print(idx)


0
1
2
3
4
5
6

In [20]:
for idx in range(2, len(xs)):
    print(idx)


2
3
4
5
6

In [21]:
for idx in range(0, len(xs), 2):
    print(idx)


0
2
4
6

In [22]:
xs = []
for x in range(10):
    xs.append(x ** 2)
xs


Out[22]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [23]:
[x ** 2 for x in range(10)]


Out[23]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [24]:
[x ** 2 for x in range(10) if x % 2 == 0]


Out[24]:
[0, 4, 16, 36, 64]

In [25]:
[x ** 2 if x % 2 == 0 else 512 for x in range(10)]


Out[25]:
[0, 512, 4, 512, 16, 512, 36, 512, 64, 512]

In [26]:
for a, b in zip([1, 2, 3], [4, 5, 6]):
    print(a, b)


1 4
2 5
3 6

In [27]:
for a, b in zip([1, 2, 3], [4, 5, 6, 7]):
    print(a, b)


1 4
2 5
3 6

In [28]:
a_list, b_list = zip(*[(1, 4), (2, 3), (5, 6)])
print(a_list)
print(b_list)


(1, 2, 5)
(4, 3, 6)

1.2. Stringovi


In [29]:
cool_name = "Miyazaki"
cool_name


Out[29]:
'Miyazaki'

In [30]:
cool_name + " is " + "great"


Out[30]:
'Miyazaki is great'

In [31]:
num_people = 200000

In [32]:
cool_name + " has " + num_people + " citizens"


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-e40dee72ca0e> in <module>
----> 1 cool_name + " has " + num_people + " citizens"

TypeError: can only concatenate str (not "int") to str

In [33]:
cool_name + " has " + str(num_people) + " citizens"


Out[33]:
'Miyazaki has 200000 citizens'

In [34]:
len(cool_name)


Out[34]:
8

In [35]:
print("{0} has {1} citizens".format(cool_name, num_people))


Miyazaki has 200000 citizens

1.3. Razredi


In [36]:
class Product:

    def __init__(self, product_name=None, tags=None, price=0.0):
        self.product_name = product_name
        self.tags = [] if tags is None else tags
        self.price = price
        
    vat = 0.25 

    def product_price(self, with_pdv=False):
        if with_pdv:
            return self.price * (1 + self.vat)
        else:
            return self.price
    
    def contains_tag(self, tag):
        return (tag in self.tags)

In [37]:
prod = Product(product_name="toilet paper", tags=["health", "toilet", "fresh"], price=10)
print(prod.product_price(with_pdv=True))
print(prod.product_price(with_pdv=False))


12.5
10

In [38]:
prod.contains_tag("fresh")


Out[38]:
True

In [39]:
prod.contains_tag("money")


Out[39]:
False

In [40]:
prod1 = Product(product_name="toilet paper", price=10)
prod2 = Product(product_name="toothbrush", price=10)
Product.vat = 0.5
prod1.vat = 0.3

In [41]:
prod1.product_price(with_pdv=True)


Out[41]:
13.0

In [42]:
prod2.product_price(with_pdv=True)


Out[42]:
15.0

2. NumPy


In [43]:
import numpy as np

2.1. Polja


In [44]:
a = np.array([1, 2, 3])
a


Out[44]:
array([1, 2, 3])

In [45]:
c = np.array([1, 2, 3], dtype=np.float64)
c


Out[45]:
array([1., 2., 3.])

In [46]:
a.shape


Out[46]:
(3,)

In [47]:
b = np.array([[1, 3, 4], [2, 3, 5]])
b


Out[47]:
array([[1, 3, 4],
       [2, 3, 5]])

In [48]:
b.shape


Out[48]:
(2, 3)

In [49]:
b[1, 2]


Out[49]:
5

In [50]:
b[0:1, 2] # Notice the difference


Out[50]:
array([4])

In [51]:
b[:, 2]


Out[51]:
array([4, 5])

In [52]:
b[0:2, 2]


Out[52]:
array([4, 5])

In [53]:
d = np.array([[1, 2, 3], [4, 5, 6]])
d


Out[53]:
array([[1, 2, 3],
       [4, 5, 6]])

In [54]:
np.append(d, 1)


Out[54]:
array([1, 2, 3, 4, 5, 6, 1])

In [55]:
np.append(d, [1, 2])


Out[55]:
array([1, 2, 3, 4, 5, 6, 1, 2])

In [56]:
d


Out[56]:
array([[1, 2, 3],
       [4, 5, 6]])

In [57]:
to_add = np.array([[1], [2]])
to_add


Out[57]:
array([[1],
       [2]])

In [58]:
d + to_add


Out[58]:
array([[2, 3, 4],
       [6, 7, 8]])

In [59]:
np.hstack([d, to_add])


Out[59]:
array([[1, 2, 3, 1],
       [4, 5, 6, 2]])

In [60]:
np.vstack([d, to_add])


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-60-d1a30321b432> in <module>
----> 1 np.vstack([d, to_add])

~\Anaconda3\lib\site-packages\numpy\core\shape_base.py in vstack(tup)
    281     """
    282     _warn_for_nonsequence(tup)
--> 283     return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
    284 
    285 

ValueError: all the input array dimensions except for the concatenation axis must match exactly

In [61]:
to_add2 = np.array([2, 3, 4])
to_add2


Out[61]:
array([2, 3, 4])

In [62]:
result = np.vstack([d, to_add2])
result


Out[62]:
array([[1, 2, 3],
       [4, 5, 6],
       [2, 3, 4]])

In [63]:
np.sqrt(result)


Out[63]:
array([[1.        , 1.41421356, 1.73205081],
       [2.        , 2.23606798, 2.44948974],
       [1.41421356, 1.73205081, 2.        ]])

In [64]:
result * 2


Out[64]:
array([[ 2,  4,  6],
       [ 8, 10, 12],
       [ 4,  6,  8]])

In [65]:
result + result


Out[65]:
array([[ 2,  4,  6],
       [ 8, 10, 12],
       [ 4,  6,  8]])

In [66]:
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
v = np.array([1,2])
w = np.array([5,3])
print(x)
print(y)
print(v)
print(w)


[[1 2]
 [3 4]]
[[5 6]
 [7 8]]
[1 2]
[5 3]

In [67]:
v.dot(w)


Out[67]:
11

In [68]:
np.dot(v, w)


Out[68]:
11

In [69]:
x.dot(v)


Out[69]:
array([ 5, 11])

In [70]:
np.linalg.inv(x)


Out[70]:
array([[-2. ,  1. ],
       [ 1.5, -0.5]])

In [71]:
np.linalg.det(x)


Out[71]:
-2.0000000000000004

In [72]:
np.linalg.norm(x)


Out[72]:
5.477225575051661

In [73]:
x


Out[73]:
array([[1, 2],
       [3, 4]])

In [74]:
np.max(x)


Out[74]:
4

In [75]:
np.max(x, axis=0)


Out[75]:
array([3, 4])

In [76]:
np.max(x, axis=1)


Out[76]:
array([2, 4])

In [77]:
p = np.array([2, 3, 1, 6, 4, 5])
b = ["a", "b", "c", "d", "e", "f"]

In [78]:
np.max(p)


Out[78]:
6

In [79]:
np.argmax(p)


Out[79]:
3

In [80]:
b[np.argmax(p)]


Out[80]:
'd'

In [81]:
np.mean(p)


Out[81]:
3.5

In [82]:
np.var(p)


Out[82]:
2.9166666666666665

In [83]:
np.linspace(1, 10, 100)


Out[83]:
array([ 1.        ,  1.09090909,  1.18181818,  1.27272727,  1.36363636,
        1.45454545,  1.54545455,  1.63636364,  1.72727273,  1.81818182,
        1.90909091,  2.        ,  2.09090909,  2.18181818,  2.27272727,
        2.36363636,  2.45454545,  2.54545455,  2.63636364,  2.72727273,
        2.81818182,  2.90909091,  3.        ,  3.09090909,  3.18181818,
        3.27272727,  3.36363636,  3.45454545,  3.54545455,  3.63636364,
        3.72727273,  3.81818182,  3.90909091,  4.        ,  4.09090909,
        4.18181818,  4.27272727,  4.36363636,  4.45454545,  4.54545455,
        4.63636364,  4.72727273,  4.81818182,  4.90909091,  5.        ,
        5.09090909,  5.18181818,  5.27272727,  5.36363636,  5.45454545,
        5.54545455,  5.63636364,  5.72727273,  5.81818182,  5.90909091,
        6.        ,  6.09090909,  6.18181818,  6.27272727,  6.36363636,
        6.45454545,  6.54545455,  6.63636364,  6.72727273,  6.81818182,
        6.90909091,  7.        ,  7.09090909,  7.18181818,  7.27272727,
        7.36363636,  7.45454545,  7.54545455,  7.63636364,  7.72727273,
        7.81818182,  7.90909091,  8.        ,  8.09090909,  8.18181818,
        8.27272727,  8.36363636,  8.45454545,  8.54545455,  8.63636364,
        8.72727273,  8.81818182,  8.90909091,  9.        ,  9.09090909,
        9.18181818,  9.27272727,  9.36363636,  9.45454545,  9.54545455,
        9.63636364,  9.72727273,  9.81818182,  9.90909091, 10.        ])

In [84]:
w = np.array([[1, 2, 3], [4, 5, 6]])

In [85]:
w > 2


Out[85]:
array([[False, False,  True],
       [ True,  True,  True]])

In [86]:
w[w>2]


Out[86]:
array([3, 4, 5, 6])

In [87]:
w = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([0, 0, 0, 1, 0, 0, 1, 0])

In [88]:
w[y==1]


Out[88]:
array([4, 7])

In [89]:
list(p)


Out[89]:
[2, 3, 1, 6, 4, 5]

In [90]:
p.tolist()


Out[90]:
[2, 3, 1, 6, 4, 5]

3. Matplotlib


In [92]:
import matplotlib.pyplot as plt
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [93]:
plt.plot([1,2,3,4,5], [4,5,5,7,3])


Out[93]:
[<matplotlib.lines.Line2D at 0x1889381d748>]

In [94]:
plt.plot(np.array([1,2,3,4,5]), np.array([4,5,5,7,3]))


Out[94]:
[<matplotlib.lines.Line2D at 0x188938df630>]

In [95]:
plt.plot([4,5,5,7,3])


Out[95]:
[<matplotlib.lines.Line2D at 0x1889394f978>]

In [96]:
plt.plot([4,5,5,7,3], 'x');



In [97]:
def f(x): return x**2

In [98]:
xs = linspace(0,100); xs


Out[98]:
array([  0.        ,   2.04081633,   4.08163265,   6.12244898,
         8.16326531,  10.20408163,  12.24489796,  14.28571429,
        16.32653061,  18.36734694,  20.40816327,  22.44897959,
        24.48979592,  26.53061224,  28.57142857,  30.6122449 ,
        32.65306122,  34.69387755,  36.73469388,  38.7755102 ,
        40.81632653,  42.85714286,  44.89795918,  46.93877551,
        48.97959184,  51.02040816,  53.06122449,  55.10204082,
        57.14285714,  59.18367347,  61.2244898 ,  63.26530612,
        65.30612245,  67.34693878,  69.3877551 ,  71.42857143,
        73.46938776,  75.51020408,  77.55102041,  79.59183673,
        81.63265306,  83.67346939,  85.71428571,  87.75510204,
        89.79591837,  91.83673469,  93.87755102,  95.91836735,
        97.95918367, 100.        ])

In [99]:
f(xs)


Out[99]:
array([0.00000000e+00, 4.16493128e+00, 1.66597251e+01, 3.74843815e+01,
       6.66389005e+01, 1.04123282e+02, 1.49937526e+02, 2.04081633e+02,
       2.66555602e+02, 3.37359434e+02, 4.16493128e+02, 5.03956685e+02,
       5.99750104e+02, 7.03873386e+02, 8.16326531e+02, 9.37109538e+02,
       1.06622241e+03, 1.20366514e+03, 1.34943773e+03, 1.50354019e+03,
       1.66597251e+03, 1.83673469e+03, 2.01582674e+03, 2.20324865e+03,
       2.39900042e+03, 2.60308205e+03, 2.81549354e+03, 3.03623490e+03,
       3.26530612e+03, 3.50270721e+03, 3.74843815e+03, 4.00249896e+03,
       4.26488963e+03, 4.53561016e+03, 4.81466056e+03, 5.10204082e+03,
       5.39775094e+03, 5.70179092e+03, 6.01416077e+03, 6.33486047e+03,
       6.66389005e+03, 7.00124948e+03, 7.34693878e+03, 7.70095793e+03,
       8.06330696e+03, 8.43398584e+03, 8.81299459e+03, 9.20033319e+03,
       9.59600167e+03, 1.00000000e+04])

In [100]:
plt.plot(xs, f(xs))


Out[100]:
[<matplotlib.lines.Line2D at 0x18893a3a048>]

In [101]:
plt.plot(xs, f(xs), 'r+');



In [102]:
plt.plot(xs, 1 - f(xs), 'b', xs, f(xs)/2 - 1000, 'r--');



In [103]:
plt.plot(xs, f(xs), label='f(x)')
plt.plot(xs, 1 - f(xs), label='1-f(x)')
plt.legend(loc="center right")
plt.show()



In [104]:
plt.plot(xs, f(xs), label='f(x)')
plt.plot(xs, 1 - f(xs), label='1-f(x)')
plt.legend(loc="best")
plt.show()



In [105]:
plt.plot(xs, f(xs), label='f(x)')
plt.plot(xs, 1 - f(xs), label='1-f(x)')
plt.legend(loc="best")
plt.xlim([20, 80])
plt.show()



In [106]:
plt.plot(xs, f(xs), label=r'$\lambda^{3}$')
plt.plot(xs, 1 - f(xs), label=r'$\mathcal{N}$')
plt.legend(loc="best")
plt.xlim([20, 80])
plt.xlabel("hello")
plt.title("woohoo")
plt.show()



In [107]:
def f_1(x):
    return np.sqrt((1 - (np.abs(x) - 1)**2))
def f_2(x):
    return -3 * np.sqrt(1 - np.sqrt((np.abs(x)/2)))

x = np.linspace(-2, 2, 1000)
plt.plot(x, f_1(x), 'b-', label="bolja polovica")
plt.plot(x, f_2(x), 'r-', label="dobra polovica")
plt.xlim([-3, 3])
plt.ylim([-3, 1.5])
plt.xlabel("x")
plt.ylabel("y")
plt.legend(loc="lower right")
plt.show()



In [108]:
plt.scatter([0, 1, 2, 0], [4, 5, 2, 1])
plt.show()



In [109]:
plt.scatter([0,1,2,0], [4, 5, 2, 1], s=200, marker='s');



In [110]:
for c in 'rgb':
    plt.scatter(np.random.random(100), np.random.random(100), s=200, alpha=0.5, marker='o', c=c)



In [111]:
plt.subplot(2,1,1)
plt.plot(xs, f(xs), label='f(x)', c="g")
plt.legend(loc="center right")

plt.subplot(2,1,2)
plt.plot(xs, f(xs), label='f(x)', c="r")
plt.legend(loc="center right")
plt.show()



In [112]:
plt.subplot(2,3,1)
plt.plot(xs, f(xs), label='f(x)', c="g")
plt.legend(loc="center right")

plt.subplot(2,3,2)
plt.plot(xs, f(xs), label='f(x)', c="r")
plt.legend(loc="center right")

plt.subplot(2,3,3)
plt.plot(xs, f(xs), label='f(x)', c="m")
plt.legend(loc="center right")

plt.subplot(2,3,4)
plt.plot(xs, f(xs), label='f(x)', c="c")
plt.legend(loc="center right")

plt.subplot(2,3,5)
plt.plot(xs, f(xs), label='f(x)', c="y")
plt.legend(loc="center right")

plt.subplot(2,3,6)
plt.plot(xs, f(xs), label='f(x)', c="w")
plt.legend(loc="center right")
plt.show()