CS224N Python Introduction - Stanford University

3y ago
89 Views
15 Downloads
3.29 MB
47 Pages
Last View : 20d ago
Last Download : 3m ago
Upload by : Philip Renner
Transcription

CS224NPython Introduction

Plan for Today Intro to PythonInstalling PythonPython SyntaxNumpyPython Demo

Intro to Python

What is Python? General-purpose, high-level scripting language Used for a wide variety of purposes including networkingand web applications Most popular in the scientific community for its ease ofuse

Pros vs Cons Pros: Easy to understand and write,very similar to English Works across systems(Windows, Mac, Linux) Object Oriented Really great standard library Dynamically typed? Cons: Python can be slow Not great for mobiledevelopment Dynamically typed?

InstallingPython

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 ofPython: conda activate Because Python is a scripting language, you can try it outright 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 InstallationWhichenvironment Iam using (this isthe default)Python in theterminal! This willbe helpful forNumpy when youwant to testbroadcasting(more on thislater)

Writing Programs For longer tasks, probably want to write in a program thatyou can run on command To write programs, people often use IDEs Pycharm (can get professional version for free since you are a student!) Sublime (after modification with plugins) VSCode (after modification with plugins) IDEs include lots of nice bells and whistles like codecompletion, syntax checking and a debugger If you choose to just use a text editor, you can run yourprogram from the terminal by using the command:python filename .py

Basic Python

Basic data structures None of these types have a fixed type: can containanything Sets will remove duplicates; only one copy of ‘four’

More on Lists Can easily create 2D arrays and then index into them List comprehensions are a slick way to create lists

Sorting Lists Sorted function lets you sort a list Has additional ‘key’ parameter to which you can pass afunction that tells sorted how to compare For more details, look up ‘lambda functions’

Functions, Loops and Control FlowIntegers in [0, a)BooleanstatementsIntegers from 1(inclusive) to b(exclusive),counting by 2If called from command line

ClassesInitialize the class toget an instance usingsome parametersInstance variableDoes somethingwith the instance

To use a classInstantiate a class,get an instanceCall an instance method

Numpy & Scipy

What is Numpy? What is Scipy? Numpy – package for vector and matrix multiplicationScipy – package for scientific and technical computingThe main advantage of numpy and scipy are their speedSpeed comes from efficient memory representation andlow-level machine instructions

Ndarray Most important structure in numpyCan only contain one type of valueExtremely fastUsed to represent vectors, matrices, tensorsCalling myArray.shape will return a tuple of integers thatrepresent the shape of the ndarray – very important!

Ndarray – Addition (same shape) When two Ndarrays are the same shape, addition actsexactly as you would expect: component wise! We will discuss the case when they are not the sameshape later

Ndarray – Addition (same shape,example 1)

Ndarray – Addition (same shape,example 2)

Ndarray – Component-WiseMultiplication (same shape) When two ndarrays are the same dimension and you usethe python multiplication operator (*) you get componentwise multiplication, not matrix multiplication! When we get to neural networks, we will see that thisHadamard product is very important

Ndarray – Component-WiseMultiplication (same shape,example 1)

Ndarray – np.dot Vector-Vector, Matrix-Vector and Matrix-Matrix productsare calculated using np.dot() As with most numpy functions, they behave differentlydepending on the shapes of the input; we will look at themost common uses

Ndarray – np.dot with two vectors When the two inputs to np.dot() are both 1d vectors, thenthe result is the standard dot product

Ndarray – np.dot with matrix andvector In this case, np.dot() acts as matrix-vector multiplication Note that dimensions matter!

Ndarray – np.dot with two matrices Here, we have standard matrix multiplication However, numpy documentation says that it is preferableto use np.matmul()

Ndarray – np.dot with twomatrices (example)

Broadcasting In math, operations like dot products and matrix additionrequire the samec dimensions. In numpy, this is not thecase Up until now, we have used 1d and 2d ndarrays,representing vectors and matrices, and numpy acts as wewould expect However, the operations we have described work evenwhen the two inputs do not have ‘standard’ shapes, givenby a set of very specific rules

General Broadcasting Rules Write out the shapes of each ndarray Starting from the back, that dimension has compatiblevalues if either: They are the same value, or One of them is a 1 The size of the resulting array is the maximum along eachdimension Note: the two ndarrays do not need to have the samenumber of dimensions

Broadcasting – Example 1(easiest) In this case, we add a scalar to an ndarray Numpy automatically adds the scalar to every singleelement

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 matricesresiding in the last two indexes and broadcast accordingly. What will be the dimension of the output for a call withthe following shapes? (1, 5, 6), (6, 7) (3, 4, 5, 6), (6, 7) (3, 5, 6), (6, 7) (3, 4, 5, 6), (4, 6, 7) (3, 5, 6), (3, 6, 7) (3, 4, 5, 6), (1, 4, 6, 7)

Broadcasting – Example 3(hardest, one answer) Take the fifth example, the shapes are (3, 4, 5, 6) and (4,6, 7) According to the documentation, the last two dimensionsrepresent matrices, so we take those out and broadcastthe rest: (3, 4) and (4,) Using our broadcasting rules, the result of broadcastingthese shapes will be (3, 4) Matrix multiplication results in a matrix of shape (5, 7) Our output will have shape (3, 4, 5, 7)

Mathematical Functions onNdarrays Numpy has a wide array of mathematical functions thatyou can apply to arrays

Mathematical Functions onNdarrays – cont. Some functions, like sum and max, can be applied along agiven axis Applying along that dimension gets rid of that dimension,and replaces it with the function applied across thatdimension

Numpy Speed – Dot Product

Numpy Speed – Applying a Function

Popular usage, read before use!Python CommandDescriptionscipy.linalg.invInverse of matrix (numpy as equivalent)scipy.linalg.eigGet eigen value (Read documentation on eigh and numpy equivalent)scipy.spatial.distanceCompute pairwise distancenp.matmulMatrix multiplynp.zerosCreate a matrix filled with zeros (Read on np.ones)np.arangeStart, stop, step size (Read on np.linspace)np.identityCreate an identity matrixnp.vstackVertically stack 2 arrays (Read on np.hstack)

Your friend for debuggingPython CommandDescriptionarray.shapeGet shape of numpy arrayarray.dtypeCheck data type of array (for precision, for weird behavior)type(stuff)Get type of a variableimport pdb; pdb.set trace()Set a breakpoint f’My name is {name}’)Easy way to construct a message

Advice If you are unsure how an operation will work on ndarraysof a certain shape, try it out! Create random matrices that have the shape you arelooking at, do the operation, and check the shape of theoutput Python scripting in the terminal is great for this!

Plotting

More Tools Scatter plotLine plotBar plot (Histogram)3D plot

Plotting Functions

Python Live Demo in PyCharm Calculate the eigenvector associated with the dominanteigenvalue Use the power iteration method: 𝑏"# &'( &'( (If not at live session, can download the code from coursewebsite)

LinksPython DocumentationNumpy ReferenceCS 231N Python TutorialDownload Pycharm

Includes Python, as well as several packages for scientific computing In your terminal, start up the Anaconda installation of Python: condaactivate Because Python is a scripting language, you can try it out right in the terminal; just type: python

Related Documents:

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

Python 2 versus Python 3 - the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code .

Domain Adversarial Training for QA Systems Stanford CS224N Default Project Mentor: Gita Krishna Danny Schwartz Brynne Hurst Grace Wang Stanford University Stanford University Stanford University deschwa2@stanford.edu brynnemh@stanford.edu gracenol@stanford.edu Abstract In this project, we exa

SEISMIC: A Self-Exciting Point Process Model for Predicting Tweet Popularity Qingyuan Zhao Stanford University qyzhao@stanford.edu Murat A. Erdogdu Stanford University erdogdu@stanford.edu Hera Y. He Stanford University yhe1@stanford.edu Anand Rajaraman Stanford University anand@cs.stanford.edu Jure Leskovec Stanford University jure@cs.stanford .

Python is readable 5 Python is complete—"batteries included" 6 Python is cross-platform 6 Python is free 6 1.3 What Python doesn't do as well 7 Python is not the fastest language 7 Python doesn't have the most libraries 8 Python doesn't check variable types at compile time 8 1.4 Why learn Python 3? 8 1.5 Summary 9

site "Python 2.x is legacy, Python 3.x is the present and future of the language". In addition, "Python 3 eliminates many quirks that can unnecessarily trip up beginning programmers". However, note that Python 2 is currently still rather widely used. Python 2 and 3 are about 90% similar. Hence if you learn Python 3, you will likely

There are currently two versions of Python in use; Python 2 and Python 3. Python 3 is not backward compatible with Python 2. A lot of the imported modules were only available in Python 2 for quite some time, leading to a slow adoption of Python 3. However, this not really an issue anymore. Support for Python 2 will end in 2020.

ISO-14001 ELEMENTS: 4.2 EMS-MANUAL ENVIRONMENTAL MANUAL REVISION DATE: ORIGINAL CREATION: AUTHORIZATION: 11/10/2012 01/01/2008 11/10/2012 by: by: by: Bart ZDROJOWY Dan CRONIN Noel CUNNINGHAM VER. 1.3 ISO 14001 CONTROLLED DOCUMENT WATERFORD CARPETS LTD PAGE 7 OF 17 Environmental Policy The General Management of Waterford Carpets Limited is committed to pollution prevention and environmental .