Hello everyone, welcome to my talk Numpy Basics. I'm Jan Höhnig. I'm the PhD student at Computer
Science Chair of System Simulations at University of Erlangen. And today I will be talking a little
bit about Numpy Basics. First things first, the whole presentation is inside this github repository
and you can simply click on the binder launcher. It takes a few moments but after that you get your
own Jupyter Notebook and you can follow along or try out things for yourselves.
So as you can see everybody gets their own Jupyter Notebooks and they can start coding.
For the presentation itself it will be here. Today we will be covering Numpy Basics and a
little bit of SciPy and Mudbloodlib. Numpy is basically C-Speed at Python's convenience.
It's the package for scientific computing because it provides vast multi-dimensional arrays.
And the main reason we use it is because other scientific packages use it as well
like SciPy, Mudbloodlib, OpenCV, TensorFlow and many others. It is just a good basis for anything
which has to do with arrays, fields. There exists also drop and replacement like for CUDA or for the
NSC accelerators. This talk is interactive which means I will be executing the talk as we go and
which also means that questions if any should be asked immediately because then I can explore
it with you right away. The main reason why to use Numpy and not Vanilla Python list is the speed.
So I'm comparing here two lists and then Numpy array both of which are 10,000 elements long.
I'm just doing a simple power operation and usually Numpy is faster by two or three orders of magnitude
than normal Python. It depends what you are computing but the rule of thumb is two or three orders of magnitude faster.
The basis of Numpy is of course the nd array. This is the multi-dimensional array. It has a fixed size.
Of course you can resize it. It costs something but from the get-go it has a fixed size. It has
also a fixed data type. All the usual data types are supported like doubles, floats, integers,
complex numbers, even strings and even structs. We won't be covering those today but you can also have structs in our array.
Throughout the talk I have also included links to the Numpy documentation which is really great.
This is the first point where you also start looking for features I don't know or clarifying anything about Numpy.
First things first we need to create the arrays. So that's why we will be discussing array creation.
The simplest way how to get Numpy arrays is just providing a list, plain old Python list to the nd array function
which then creates the Numpy array. As you can see the data type in this case is integer with 64 bits. This is the
long and c-speak usually, maybe, depending on your compiler environment. It's an integer because all of these numbers were integer as well.
We have four of them so the size of the array is four and the item size is eight because 64 bits is the integer.
Computers like to count them. Of course we can also put in floats. A Python float is a c double and that's why
Numpy has it as a float 64 which is in c-speak it's double.
It's also a size of four and an item size of eight since the double has eight bytes.
In the case of mixed lists, I'm going to put in a number of items.
In the case of mixed lists, Numpy automatically provides the promotion.
Here we have a list of integers and floats and Numpy just makes the model float.
We can also fix the data type so we can specify what kind of data type we want to have.
Numpy will do the conversion. In this case, I specified I want a really small integer into eight which means just one byte.
This is also called char. Again, size is the same only for elements but this time the item size is only one.
Depending on what you're computing, you might want to have different data types and
Numpy provides all the features to simplify the process to make the promotion of the data types work.
Not always you have a list of numbers like that. You could also use Numpy range and Numpy linspace.
What that does is range is pretty similar to Python's range.
You have a start, you have an end and you have a step size but Numpy's
A range can also do floating point numbers so you have nice floating point steps.
Linspace is similar. Here also you have a start and the end and you're specifying how many steps you want to do.
Numpy provides you with all kinds of functions to create arrays. I don't want to get in all of them.
You can do diagonal matrices, you can do the identity matrix, you can do better matrices,
you can create random arrays, you can create arrays from functions but the ones we are using today in this talk will be zeros where we specify the shape as a tuple.
This is just a regular tuple in Python and it will create the zero array accordingly.
We can do it with ones as well. The second thing is how to get more dimensional arrays.
Presenters
Zugänglich über
Offener Zugang
Dauer
00:51:41 Min
Aufnahmedatum
2021-12-14
Hochgeladen am
2021-12-16 12:06:04
Sprache
en-US
Speaker: Jan Hönig, LSS
Link to example code: https://github.com/m3m0ry/numpy-basics
Jan gives an introduction to the popular numpy, scipy, and matplotlib packages.