Dr. Sampath Jayarathna Old Dominion University

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

Lists Introduction to Computing Science and Programming I.
Structured Data Types and Encapsulation Mechanisms to create new data types: –Structured data Homogeneous: arrays, lists, sets, Non-homogeneous: records.
MATH 224 – Discrete Mathematics
Lists in Python.
Introduction. 2COMPSCI Computer Science Fundamentals.
Scientific Computing with NumPy & SciPy NumPy Installation and Documentation  Not much on the home page—don’t buy the guide, it’s.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Working with Arrays in MATLAB
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Introduction to python programming
String and Lists Dr. José M. Reyes Álamo.
PH2150 Scientific Computing Skills
Numpy (Numerical Python)
Expressions and Assignment
Array, Strings and Vectors
Containers and Lists CIS 40 – Introduction to Programming in Python
Arrays in PHP are quite versatile
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Scripts & Functions Scripts and functions are contained in .m-files
Java Review: Reference Types
Searching.
Python NumPy AILab Batselem Jagvaral 2016 March.
CSE Social Media & Text Analytics
Python Review.
Numerical Computing in Python
Introduction to Python
Bryan Burlingame 03 October 2018
Use of Mathematics using Technology (Maltlab)
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
PH2150 Scientific Computing Skills
Chapter 8 Arrays Objectives
CS190/295 Programming in Python for Life Sciences: Lecture 6
Transforming Data (Python®)
During the last lecture we had a discussion on Data Types, Variables & Operators
4. sequence data type Rocky K. C. Chang 16 September 2018
PHP.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Information Redundancy Fault Tolerant Computing
Numpy (Numerical Python)
JavaScript Arrays.
String and Lists Dr. José M. Reyes Álamo.
Numpy (Numerical Python)
Lecture Set 9 Arrays, Collections and Repetition
APACHE MXNET By Beni Mulyana.
MATLAB Programming Indexing Copyright © Software Carpentry 2011
Python-NumPy Tutorial
Data Types and Data Structures
Topics Sequences Introduction to Lists List Slicing
Variables, Lists, and Objects
Python Primer 1: Types and Operators
MATLAB Programming Basics Copyright © Software Carpentry 2011
15-110: Principles of Computing
Chapter 8 Arrays Objectives
Topics Sequences Lists Copying Lists Processing Lists
Topics Basic String Operations String Slicing
Dr. Sampath Jayarathna Cal Poly Pomona
R Course 1st Lecture.
Topics Sequences Introduction to Lists List Slicing
Dr. Sampath Jayarathna Old Dominion University
Topics Basic String Operations String Slicing
Working with Arrays in MATLAB
Lecture 2: Bits, Bytes, Ints
Python Debugging Session
Topics Basic String Operations String Slicing
INTRODUCING PYTHON PANDAS:-SERIES
Introduction to Computer Science
Presentation transcript:

Dr. Sampath Jayarathna Old Dominion University CS 795/895 Introduction to Data Science Lecture 3- NumPy Dr. Sampath Jayarathna Old Dominion University Credit for some of the slides in this lecture goes to Jianhua Ruan UTSA

Numpy Numpy array creation Array access and operations

NumPy Stands for Numerical Python Is the fundamental package required for high performance computing and data analysis NumPy is so important for numerical computations in Python is because it is designed for efficiency on large arrays of data. It provides ndarray for creating multiple dimensional arrays Standard math functions for fast operations on entire arrays of data without having to write loops Internally stores data in a contiguous block of memory, independent of other built-in Python objects, use much less memory than built-in Python sequences.

NumPy ndarray vs list One of the key features of NumPy is its N-dimensional array object, or ndarray, which is a fast, flexible container for large datasets in Python. Whenever you see “array,” “NumPy array,” or “ndarray” in the text, with few exceptions they all refer to the same thing: the ndarray object. NumPy-based algorithms are generally 10 to 100 times faster (or more) than their pure Python counterparts and use significantly less memory. import numpy as np my_arr = np.arange(1000000) my_list = list(range(1000000))

ndarray ndarray is used for storage of homogeneous data i.e., all elements the same type Every array must have a shape and a dtype Supports convenient slicing, indexing and efficient vectorized computation Arrays are important because they enable you to express batch operations on data without writing any for loops. We call this vectorization. data1 = [6, 7.5, 8, 0, 1] arr1 = np.array(data1) data2 = [[1, 2, 3, 4], [5, 6, 7, 8]] #list of lists arr2 = np.array(data2) print(arr2.ndim) #2 Print(arr2.shape) # (2,4)

Creating ndarrays array = np.array([[0,1,2],[2,3,4]]) [[0 1 2] [2 3 4]] array = np.zeros((2,3)) [[0. 0. 0.] [0. 0. 0.]] array = np.ones((2,3)) [[1. 1. 1.] [1. 1. 1.]] array = np.eye(3) [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] array = np.arange(0, 10, 2) [0, 2, 4, 6, 8] array = np.random.randint(0, 10, (3,3)) [[6 4 3] [1 5 6] [9 8 5]] array = np.eye(3, dtype=int) arange is an array-valued version of the built-in Python range function

Arithmatic with NumPy Arrays Any arithmetic operations between equal-size arrays applies the operation element-wise: arr = np.array([[1., 2., 3.], [4., 5., 6.]]) print(arr) [[1. 2. 3.] [4. 5. 6.]] print(arr * arr) [[ 1. 4. 9.] [16. 25. 36.]] print(arr - arr) [[0. 0. 0.] [0. 0. 0.]] np.array(list)

Arithmatic with NumPy Arrays Arithmetic operations with scalars propagate the scalar argument to each element in the array: Comparisons between arrays of the same size yield boolean arrays: arr = np.array([[1., 2., 3.], [4., 5., 6.]]) print(arr) [[1. 2. 3.] [4. 5. 6.]] print(arr **2) [[ 1. 4. 9.] [16. 25. 36.]] arr2 = np.array([[0., 4., 1.], [7., 2., 12.]]) print(arr2) [[ 0. 4. 1.] [ 7. 2. 12.]] print(arr2 > arr) [[False True False] [ True False True]]

Indexing and Slicing One-dimensional arrays are simple; on the surface they act similarly to Python lists: arr = np.arange(10) print(arr) # [0 1 2 3 4 5 6 7 8 9] print(arr[5]) #5 print(arr[5:8]) #[5 6 7] arr[5:8] = 12 print(arr) #[ 0 1 2 3 4 12 12 12 8 9]

Indexing and Slicing As you can see, if you assign a scalar value to a slice, as in arr[5:8] = 12, the value is propagated (or broadcasted) to the entire selection. An important first distinction from Python’s built-in lists is that array slices are views on the original array. This means that the data is not copied, and any modifications to the view will be reflected in the source array. arr = np.arange(10) print(arr) # [0 1 2 3 4 5 6 7 8 9] arr_slice = arr[5:8] print(arr_slice) # [5 6 7] arr_slice[1] = 12345 print(arr) # [ 0 1 2 3 4 5 12345 7 8 9] arr_slice[:] = 64 print(arr) # [ 0 1 2 3 4 64 64 64 8 9] As NumPy has been designed to be able to work with very large arrays, you could imagine performance and memory problems if NumPy insisted on always copying data. If you want a copy of a slice of an ndarray instead of a view, you will need to explicitly copy the array—for example, arr[5:8].copy().

Indexing In a two-dimensional array, the elements at each index are no longer scalars but rather one-dimensional arrays: Thus, individual elements can be accessed recursively. But that is a bit too much work, so you can pass a comma-separated list of indices to select individual elements. So these are equivalent: arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr2d[2]) # [7 8 9] print(arr2d[0][2]) # 3 print(arr2d[0, 2]) #3

Slicing Consider the two-dimensional array from before, arr2d. Slicing this array is a bit different: You can pass multiple slices just like you can pass multiple indexes (like in Pandas DataFrame): Of course, assigning to a slice expression assigns to the whole selection: arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr2d[:2]) [[1 2 3] [4 5 6]] print(arr2d[:2, 1:]) [[2 3] [5 6]] arr2d[:2, 1:] = 0 print(arr2d) [[1 0 0] [4 0 0] [7 8 9]]