terminal = false
nup_logo

Machine Learning with Python

Lecture 1. Introduction to Python


Alex Avdiushenko
October 3, 2023
monty_python

Main Goals of the course

  • Take the first step, after which you can continue self-study
  • Learn the main features of Python, its differences from other programming languages
  • Deeply understand basic concepts of machine learning and neural nets
  • Implement ML models using Python and PyTorch
  • Get acquainted with some typical interview tasks in IT companies and their solutions

Org information

Python Language IS

  • easy to start using
  • free and open source
  • (almost) portable
  • high-level
  • interpreted, not compiled
  • suited for REPL = Read Eval Print Loop
python_after_cpp

First example of Python code

# Pay attention to the indents: # they are part of the syntax! # So they highlight nested blocks of code for i in range(5): if not (i % 3 == 0): print(i**5)
python_interpretation

Another Example

Writing in Python is like writing pseudocode. Suppose you know, that

\[ e^x=\sum_{k=0}^\infty \frac{1}{k!}x^k = 1 + x + \frac{x^2}{2!} + \dots \]
def e(x): sum_, k, term = 1, 0, 1 while True: # yield is like "return", but creates generator object yield sum_ k += 1 term *= x / k sum_ += term x = 1 gen_e_x = e(x) [i for i in range(5)] [next(gen_e_x) for _ in range(5)]
value_of_e

Everything in Python is an object

Every object has

  • id — where it's located (~memory address)
  • type — a set of values and operations on these values
  • value — the value
print(id(1), ', ', id('1')) print(type(1), ', ', type('1')) # works only for system Python interpreter, not for a port of CPython to WebAssembly :) # a = 257; b = 257 # id(a) == id(257), id(a) == id(b), type(a) == type(1) a = 7; b = 7 id(a) == id(7), id(a) == id(b), type(a) == type(1) a is b # equivalent to id(a) == id(b)

Size of INT in the Python language

What do you think, how much memory does INT take?

  • In general, 1 bit represents only two values (it's 0 or 1)
  • 1 byte = 8 bits, and this already represents integers from 0 to 255, for example

Can it be depending on the platform:

  • 4 bytes (32 bits) or
  • 8 bytes (64 bits)?
# let's check! import sys def print_sizeof(x): return f'{x} — {sys.getsizeof(x)} bytes' print('\n'.join( ['Size of int in Python'] + [print_sizeof(x) for x in (0, 1, 10**10, 10**11, 10**30)]) )

Why so much and not the same?

isinstance(1, object)
Cpython open source realization

									struct _longobject {
										// macros with
										// 1. the object’s reference counter (8 bytes)
										// 2. and a pointer to the corresponding type object (8 bytes)
										// 3. and extension field ob_size (8 bytes)
										PyObject_VAR_HEAD
										// int value adds 0, 4 (32x) or 8 bytes (64x)
										digit ob_digit[1];
									};
								

Task example: Sum of two

Given a list of integers and a specified integer, return the indices of two numbers from the list, the sum of which equals the specified number (target).

It is assumed that there is exactly one solution and the same element cannot be used twice.

# YOUR CODE
def two_sum(nums, target): ans = [0, 1] return ans def test_sorted(): nums = [2, 7, 11, 15] target = 9 expected = [0, 1] output = two_sum(nums, target) assert output == expected def test_not_sorted(): nums = [2, -7, 11, 15] target = 4 expected = [1, 2] output = two_sum(nums, target) assert output == expected test_not_sorted()