Multidimensional arrays with Numpy ▫ Characterized by a set of axes and a shape ▫ The axes of an array define its dimensions ▫ a (row) vector has 1 axis
Previous PDF | Next PDF |
[PDF] Array computing in Python
Arrays ▹ multidimensional rectangular data container ▹ all elements have the same type ▹ compact data layout, compatible with C/Fortran ▹ efficient
[PDF] Python Arrays
19 mar 2018 · We can also create a multidimensional array in Python A multidimensional array is an array within an array This means an array holds different arrays inside it Here, we have 4 elements and each elements hold another 2 sub-elements
[PDF] 27 Two-Dimensional Arrays Visualizing Rows and - Cornell CS
19 avr 2016 · Turns out that base Python is not very handy for 2D array manipulations The numpy module makes up for this We w i ll learn just enough numpy
[PDF] A Type System for Multidimensional Arrays - Harvard DASH
10 avr 2020 · Initially designed as a strong, dynamically typed language, recent additions to Python have introduced a static type checker; however, the types
[PDF] Numpy - DataBase and Data Mining Group
Multidimensional arrays with Numpy ▫ Characterized by a set of axes and a shape ▫ The axes of an array define its dimensions ▫ a (row) vector has 1 axis
[PDF] Introduction to Python Data Analysis - Yale Center for Research
Get values from N-dimensional array NumPy provides many ways to extract data from arrays # Print single element of 2D array print a[0,0] # a scalar, not an
[PDF] Python Numpy (1) - Intro to multi-dimensional array & numerical
29 jan 2018 · Why Numpy? • the core library for scientific computing in Python • multi- dimensional array object • math tools for working with these arrays
[PDF] NumPy Matrices
Numpy arrays are underlying to many packages dedicated to scientific computing in Python • Note that a matrix is actually a 2 dimensional array To go further
[PDF] Python lab 3: 2D arrays and plotting - University of York
11 fév 2011 · NumPy is designed to handle large arrays of data efficiently, so to 2D arrays work the same way, so if we create a 2D array of random
[PDF] Vectors Matrices And Multidimensional Arrays - DITP
7 sept 2020 · Python: Vectors, Matrices and Arrays with NumPy – Linux Hint All variables of all data types in MATLAB are multidimensional arrays A vector
[PDF] multifamily energy efficiency rebate program
[PDF] multigraph
[PDF] multilayer switch configuration
[PDF] multilevel feedback queue implementation
[PDF] multilevel feedback queue scheduling tutorialspoint
[PDF] multilevel feedback queue scheduling code in java
[PDF] multilevel feedback queue scheduling program in c++
[PDF] multilevel inverter block diagram
[PDF] multilevel inverter ppt
[PDF] multilevel inverter project report
[PDF] multilevel inverter switching pattern
[PDF] multilevel inverter thesis
[PDF] multilevel inverters syllabus
[PDF] multilevel queue scheduling
DataBase and Data Mining Group Andrea Pasini, Elena Baralis
Data Science Lab
Numpy: Numerical Python
Introduction to Numpy
Numpy (Numerical Python)
Store and operate on densedata buffers
Efficientstorage and operations
Features
Multidimensional arrays
Slicing/indexing
Math and logic operations
Applications
Computation with vectors and matrices
Provides fundamental Python objects for data science algorithmsInternally used by scikit-learn and SciPy
2Introduction to Numpy
Summary
Numpy and computation efficiency
Numpy arrays
Computationwith Numpy arrays
Broadcasting
AccessingNumpy arrays
Working with arrays, other functionalities
3Introduction to Numpy
arrayis the main object provided by NumpyCharacteristics
Fixed Type
All its elements have the same type
Multidimensional
Allows representing vectors, matrices and n-dimensional arrays 4Introduction to Numpy
Numpy arrays vs Python lists:
Also Python lists allow defining multidimensional
arraysE.g. my_2d_list = [[3.2, 4.0], [2.4, 6.2]]
Numpy advantages:
Higher flexibilityof indexing methods and operationsHigher efficiencyof operations
5Introduction to Numpy
Since lists can contain heterogeneous data types,
they keep overheadinformationE.g. my_heterog_list = [0.86, 'a', 'b', 4]
6Python List
header (list size, attributes)0x568900
0x568948
0x568980
0x5689f0
PyObject
header (object type, reference count, size) value: 0.86PyObject
header (object type, reference count, size) value: 'a'Introduction to Numpy
Characteristics of numpy arrays
Fixed-type(no overhead)
Contiguousmemory addresses (faster indexing)
E.g. my_numpy_array = np.array([0.67, 0.45, 0.33]) 7NumpyArray
header (list size, attributes) data 0.67 0.45 0.33Introduction to Numpy
Numpy data types
Numpy defines its own data types
Numerical types
int8, int16, int32, int64 uint8, ... , uint64 float16, float32, float64Boolean values
bool 8Multidimensional arrays
Collections of elements organized along an
arbitrary number of dimensionsMultidimensional arrays can be represented with
Python lists
Numpy arrays
9 x0 x1 x2Multidimensional arrays with Pythonlists
Examples:
7 13 12 8 14 3 9 15 4561218
Multidimensional arrays
10 123456
2D matrix
3D array
vector list1 = [1, 2, 3]list2 = [[1,2,3], [4,5,6]] list3 = [[[1,2,3], [4,5,6]], [[7,8,9], [10,11,12]], [13,14,15], [16,17,18]]] 123Multidimensional arrays
Multidimensional arrays with Numpy
Can be directly created from Python lists
Examples:
11 importnumpyasnp arr1 = np.array([1, 2, 3]) importnumpyasnp arr2 = np.array([[[1,2,3], [4,5,6]], [[7,8,9], [10,11,12]], [[13,14,15], [16,17,18]]]) 1237 13 12 8 14 3 9 15 45612
18
Multidimensional arrays
Multidimensional arrays with Numpy
Characterized by a set of axesand a shape
Theaxesof an array define its dimensions
a (row) vector has 1 axis (1 dimension) a 2D matrix has 2 axes (2 dimensions) a ND array has N axes 12 x0 x12D matrix
3D arrayvector
x0x0 x1 x2Multidimensional arrays
Multidimensional arrays with Numpy
Axes can be numbered with negative values
Axis -1 is always along the row
13 x-1 x-2 x-1 x0 x12D matrix
3D arrayvector
x0x0 x1 x2 x-1 x-2 x-3Multidimensional arrays
Multidimensional arrays with Numpy
Theshapeof a Numpy array is a tuple that specifies the number of elements along each axisExamples:
14 shape = (2, 3)shape = (3, 2, 3)shape = (3,) x1x0x1x0x2 heightwidthheightwidthdepth x0 width x0 x12D matrix
3D arrayvector
x0x0 x1 x2Multidimensional arrays
Column vector vs row vector
15 shape = (3,) e.g. np.array([0.1, 0.2, 0.3]) [0.1] [0.2] [0.3] shape = (3, 1) e.g. np.array([[0.1], [0.2], [0.3]])Column vector is a 2D matrix!
Numpy arrays
Creation from list:
np.array(my_list, dtype=np.float16)Data type inferred if not specified
Creation from scratch:
np.zeros(shape)Array with all 0 of the given shape
np.ones(shape)Array with all 1 of the given shape
np.full(shape, value) Array with all elements to the specified value, with the specified shape 16Numpy arrays
Creation from scratch: examples
17 np.ones((2,3)) [[1, 1, 1], [1, 1, 1]]Out[1]:
In [1]:
[[1.1], [1.1]]Out[2]:
In [2]:np.full((2,1)), 1.1)
Numpy arrays
Creation from scratch:
np.linspace(0, 1, 11)Generates 11 samples from 0 to 1 (included)
Out: [0.0, 0.1, ... , 1.0]
np.arange(1, 7, 2) Generates numbers from 1 to 7 (excluded), with step 2Out: [1, 3, 5]
np.random.normal(mean, std, shape)Generates random data with normal distribution
np.random.random(shape)Random data uniformly distributed in [0, 1]
18Numpy arrays
Main attributes of a Numpy array
Consider the array
x = np.array([[2, 3, 4],[5,6,7]]) x.ndim:number of dimensions of the arrayOut: 2
x.shape: tuple with the array shapeOut: (2,3)
x.size: array size (product of the shape values)Out: 2*3=6
19Computation on Numpy
Summary:
Universal functions(Ufuncs):
Binaryoperations (+,-,*,...)
Unaryoperations (exp(),abs(),...)
Aggregatefunctions
Sorting
Algebraicoperations (dot product, inner product)
20Computation on Numpy
Universal functions(Ufuncs): element-wise
operationsBinaryoperations with arrays of the same shape
+, -, *, /, % (modulus), // (floor division), ** (exponentiation) 21Computation on Numpy
Example:
22x=np.array([[1,1],[2,2]]) y=np.array([[3, 4],[6, 5]]) x*y [[3, 4], [12, 10]]Out[1]:
In [1]:
11 2234
65*=
1*31*4
2*62*5
341210=
Computation on Numpy
Universal functions(Ufuncs):
Unaryoperations
np.abs(x) np.exp(x), np.log(x), np.log2(x), np.log10(x) np.sin(x), cos(x), tan(x), arctan(x), ... They apply the operation separately to each element of the array 23Computation on Numpy
Example:
Note: original array (x) is not modified
24x=np.array([[1,1],[2,2]]) np.exp(x) [[2.718, 2.718],[7.389, 7.389]]Out[1]:
In [1]:
11 22e^1e^1 e^2e^2