CSC Advanced Scientific Computing, Fall Numpy

Similar documents
Problem Based Learning 2018

Introduction to NumPy

DSC 201: Data Analysis & Visualization

Phys Techniques of Radio Astronomy Part 1: Python Programming LECTURE 3

Table of Contents. Preface... xxi

NumPy and SciPy. Lab Objective: Create and manipulate NumPy arrays and learn features available in NumPy and SciPy.

Tentative NumPy Tutorial

Part VI. Scientific Computing in Python. Alfredo Parra : Scripting with Python Compact Max-PlanckMarch 6-10,

DSC 201: Data Analysis & Visualization

Programming for Engineers in Python

NumPy. Daniël de Kok. May 4, 2017

Python for Data Analysis. Prof.Sushila Aghav-Palwe Assistant Professor MIT

Chapter 5 : Informatics Practices. Class XII ( As per CBSE Board) Numpy - Array. New Syllabus Visit : python.mykvs.in for regular updates

Implement NN using NumPy

NumPy Primer. An introduction to numeric computing in Python

Lezione 6. Installing NumPy. Contents

NumPy quick reference

DSC 201: Data Analysis & Visualization

DSC 201: Data Analysis & Visualization

NumPy is suited to many applications Image processing Signal processing Linear algebra A plethora of others

IAP Python - Lecture 4

Python for Finance. Control Flow, data structures and first application (part 2) Andras Niedermayer

Python Pandas- II Dataframes and Other Operations

The NumPy Array: A Structure for Efficient Numerical Computation

DSC 201: Data Analysis & Visualization

Part VI. Scientific Computing in Python. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

Exercise: Introduction to NumPy arrays

Introducing Python Pandas

Introduction to Data Science. Introduction to Data Science with Python. Python Basics: Basic Syntax, Data Structures. Python Concepts (Core)

Handling arrays in Python (numpy)

DATA STRUCTURE AND ALGORITHM USING PYTHON

Python for Data Analysis

Short Introduction to Python Machine Learning Course Laboratory

Practical Numpy and Matplotlib Intro

stft Documentation Release Nils Werner

DSC 201: Data Analysis & Visualization

DSC 201: Data Analysis & Visualization

Introduction to NumPy

Numpy fast array interface

Python Crash Course Numpy, Scipy, Matplotlib

Lab: Numerics with Numpy. Day 1. Copyright Oliver Serang, 2018 University of Montana Department of Computer Science

Introduction to Artificial Neural Networks and Deep Learning

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

NumPy User Guide. Release dev7335. Written by the NumPy community

Programming for Data Science Syllabus

Python Advance Course via Astronomy street. Sérgio Sousa (CAUP) ExoEarths Team (

NumPy User Guide. Release Written by the NumPy community

LECTURE 19. Numerical and Scientific Packages

LECTURE 22. Numerical and Scientific Packages

NumPy User Guide. Release dev7072. Written by the NumPy community

NumPy. Arno Proeme, ARCHER CSE Team Attributed to Jussi Enkovaara & Martti Louhivuori, CSC Helsinki

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Data Science with Python Course Catalog

Chapter 1 : Informatics Practices. Class XII ( As per CBSE Board) Advance operations on dataframes (pivoting, sorting & aggregation/descriptive

(DRAFT) PYTHON FUNDAMENTALS II: NUMPY & MATPLOTLIB

Concepts of Database Management Eighth Edition. Chapter 2 The Relational Model 1: Introduction, QBE, and Relational Algebra

Python Working with files. May 4, 2017

PYTHON NUMPY TUTORIAL CIS 581

The SciPy Stack. Jay Summet

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

NumPy. Computational Physics. NumPy

Parakeet. A Runtime Compiler for Numerical Python

Introductory Scientific Computing with Python

Paraphrase Identification; Numpy; Scikit-Learn

Python Numpy (1) Intro to multi-dimensional array & numerical linear algebra. Harry Lee January 29, 2018 CEE 696

Computer Science 121. Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans

Series. >>> import numpy as np >>> import pandas as pd

Chapter 2. Python Programming for Physicists. Soon-Hyung Yook. March 31, Soon-Hyung Yook Chapter 2 March 31, / 52

SciPy. scipy [ and links on course web page] scipy arrays

Pandas. Data Manipulation in Python

Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

NumPy User Guide. Release Written by the NumPy community

1.3b Type Conversion

CT 229. Java Syntax 26/09/2006 CT229

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Derek Bridge School of Computer Science and Information Technology University College Cork

Overview of List Syntax

Graphs and transformations, Mixed Exercise 4

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Twister: Language Reference Manual

Fathom Dynamic Data TM Version 2 Specifications

Introduction to Computer Science. Homework 1

SECTION II: LANGUAGE BASICS

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

COMP1730/COMP6730 Programming for Scientists. Sequence types, part 2

Introduction to Matlab

DATA SCIENCE INTRODUCTION QSHORE TECHNOLOGIES. About the Course:

MATLAB GUIDE UMD PHYS401 SPRING 2011

Index. bfs() function, 225 Big data characteristics, 2 variety, 3 velocity, 3 veracity, 3 volume, 2 Breadth-first search algorithm, 220, 225

Course May 18, Advanced Computational Physics. Course Hartmut Ruhl, LMU, Munich. People involved. SP in Python: 3 basic points

2-9 Operations with Complex Numbers

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

CS 253. January 14, 2017

NumPy User Guide. Release Written by the NumPy community

Set Theory in Computer Science. Binary Numbers. Base 10 Number. What is a Number? = Binary Number Example

Modules and Clients 1 / 21

CSE I-Sem)

Variable and Data Type I

CSC 2400: Computer Systems. Bit- Level vs. Logical Opera<ons. Bit- Level Operators. Common to C and Java &,, ~, ^, <<, >>

The Beginning g of an Introduction to R Dan Nettleton

Transcription:

CSC 223 - Advanced Scientific Computing, Fall 2017 Numpy

Numpy Numpy (Numerical Python) provides an interface, called an array, to operate on dense data buffers. Numpy arrays are at the core of most Python scientific libraries.

The Numpy Array Type The Numpy array type is similar to a Python list, but all elements must be the same type. The numpy array function is used to construct arrays Example: construct from a list import numpy as np np. array ([1, 2, 3, 4]) Example: set the element type import numpy as np np. array ([1, 2, 3, 4], dtype = float32 )

Creating Numpy Arrays zeros: create an array of zeros ones: create an array of ones full: create an array filled with a specified value arange: create an array from a Python range linspace: create an array of evenly spaced values random.random: create an array of random values between 0 and 1

Array Creation Examples Create an array with even numbers from 0 to 10 np. arange (0, 10, 2) Create a 3 3 array of random values np. random. random ((3,3)) Create a 2 5 array filled with integers with value 7 np. full ((2,5), 7, dtype = int )

Some Array Data Types bool : Boolean stored as a byte int8: Byte (-128 to 127) int16, int32, int64: Integers float16, float32, float64: Floating point numbers complex64, complex128: Complex numbers

Basic Array Manipulations Attributes of arrays Indexing arrays Slicing arrays Reshaping arrays Joining and splitting arrays

Array Attributes ndim: the number of dimensions shape: the size of each dimension size: the total size of the array dtype: the data type of the array itemsize: the size in bytes of each element nbytes: the total size in bytes

Array Indexing Numpy arrays can be indexed like Python lists. Numpy arrays with multiple dimensions can be indexed with a tuple Example: x = np. array ([[1,2,3], [4,5,6], [7,8,9]]) x [0,0] # get 1 x [1,2] # get 6 x[2, -1] # get 9

Array Slicing Numpy arrays can be sliced like Python lists: x[ start : stop : step ] Numpy arrays with multiple dimensions can be sliced in each dimension Example: x = np. array ([[1,2,3], [4,5,6], [7,8,9]]) >>> x [0:2, 0:2] array ([[1,2], [4,5]]) >>> x[2, :] # get the third row array ([7,8,9]])

Copy an Array Array slices return views (shallow copies) into arrays The copy method can be used to create a deep copy of an array

Array Manipulation reshape: change the shape of an array concatenate: concatenate arrays hstack: horizontally stack arrays vstack: vertically stack arrays split: split an array hsplit: split an array horizontally vsplit: split an array vertically

Universal Functions Numpy has universal functions (UFuncs) that can perform operations on entire arrays Some ufuncs: absolute add or + divide or / multiply or * power or ** subtract or - For arrays of the same size, binary operations are performed element-wise

Aggregations all: check if all elements are true any: check if any elements are true argmax: index of maximum element argmin: index of minimum element max: max value mean: mean value median: median min: min value percentile: rank-based statistics prod: product of elements std: standard deviation sum: sum of elements var: variance

Multi-Dimensional Aggregations In a multi-dimensional array, aggregations can be performed along a row or a column. The axis argument specifies the axis along which the aggregate is computed Example: x = np. array ([[1,2,3], [4,5,6]]) >>> sum (x, axis =0) array ([5, 7, 9]) >>> sum (x, axis =1) array ([6, 15])

Sorting Arrays The sort function returns a sorted array: sort(x) The sort method does an in place sort: x.sort() argsort returns indices of the sorted elements Multidimensional arrays can be sorted along axes: sort(x, axis=1)

Broadcasting Broadcasting allows ufuncs to be applied to arrays of different sizes. Rules of broadcasting: 1 If the two arrays differ in dimensions, the shape of the one with fewer dimensions is padded with ones on its left side. 2 If the shape of the two arrays does not match any dimension, the array with shape equal to one in that dimension is stretched to match the other shape. 3 If sizes disagree in any dimension and neither is equal to 1, then an error is raised.

Broadcasting Example >>> A = np. ones ( (2,3) ) >>> b = np. arange (3) >>> A + b array ([[ 1., 2., 3.], [ 1., 2., 3.]]) # A: shape (2,3) # b: shape (3,) -> (1,3) -> (2,3)

Comparison operators equal or == not equal or!= less or < less equal or <= greater or > greater equal or >= Boolean operators bitwise and or & bitwise or or bitwise xor or ^ bitwise not or ~ Boolean Arrays

Boolean Operation Example x = np. random. random ( (10,2) ) # number of entries greater than 0.5 np.sum (x > 0.5) # count entries between values np.sum ( (x > 0.25) & (x < 0.75) )

Boolean Masks An array can be indexed with a boolean expression The result is a one dimensional array with the values that satisfy the expression Example: >>> x = np. array ([[1,2,3], [4,5,6], [7,8,9]]) >>> x[x < 3] array ([1, 2])

Fancy Indexing Fancy indexing allows indexing of an array by passing in an array of indices Fancy indexing can be combined with slicing Example: >>> x = np. array ([[1,2,3], [4,5,6], [7,8,9]]) >>> row = np. array ([0, 1, 2]) >>> col = np. array ([2, 1, 0]) >>> x[row, col ] array ([3, 5, 7])