cs224n Python Review 20
cs224n Python Review 20
Python Introduction
Plan for Today
● Intro to Python
● Installing Python
● Python Syntax
● Numpy
● Python Demo
Intro to Python
What is Python?
● Pros: ● Cons:
○ Easy to understand and write, ○ Python can be slow
very similar to English
○ Not great for mobile
○ Works across systems development
(Windows, Mac, Linux)
○ Dynamically typed?
○ Object Oriented
○ Dynamically typed?
Installing
Python
Installing and Running Python
● Download from Anaconda:
https://www.anaconda.com/distribution/
○ Includes Python, as well as several packages for scientific computing
● In your terminal, start up the Anaconda installation of
Python:
○ conda activate
● Because Python is a scripting language, you can try it out
right in the terminal; just type: python
● Follow instructions on Assign1 to create ‘environments’
○ Help keep your projects separated so there aren’t conflicting installations!
Check Your Installation
Which
environment I
am using (this is
the default)
Python in the
terminal! This will
be helpful for
Numpy when you
want to test
broadcasting
(more on this
later)
Writing Programs
● For longer tasks, probably want to write in a program that
you can run on command
● To write programs, people often use IDEs
○ Pycharm (can get professional version for free since you are a student!)
Integers in [0, a)
Boolean
statements
Integers from 1
(inclusive) to b
(exclusive),
counting by 2
Instance variable
Does something
with the instance
To use a class
Instantiate a class,
get an instance
● When two ndarrays are the same dimension and you use
the python multiplication operator (*) you get component-
wise multiplication, not matrix multiplication!
● When we get to neural networks, we will see that this
Hadamard product is very important
Ndarray – Component-Wise
Multiplication (same shape,
example 1)
Ndarray – np.dot
○ One of them is a 1
● The size of the resulting array is the maximum along each
dimension
● Note: the two ndarrays do not need to have the same
number of dimensions
Broadcasting – Example 1
(easiest)
● In this case, we add a scalar to an ndarray
● Numpy automatically adds the scalar to every single
element
Broadcasting – Example 2
(medium)
Broadcasting – Example 3
(hardest)
● From the np.matmul() documentation:
○ If either argument is N-D, N > 2, it is treated as a stack of matrices
residing in the last two indexes and broadcast accordingly.
● What will be the dimension of the output for a call with
the following shapes?
○ (1, 5, 6), (6, 7) ○ (3, 4, 5, 6), (6, 7)
scipy.linalg.eig Get eigen value (Read documentation on eigh and numpy equivalent)
array.dtype Check data type of array (for precision, for weird behavior)
Python Documentation
Numpy Reference
Download Pycharm