Module 1 - numpy 2024
numpy / Module 1
MODULE 1: NUMPY
Introduction to Numpy
Welcome to this session on introduction to Numpy. Numpy is a library in
python used for handling multidemensional arrays. It is a short form for
numerical python. Numpy array in a way are like the lists you were
introduced to in the introduction to python programming. As the arrays
grow bigger numpy array provide efficient ways and operations for
managing arrays. Arrays organize data into rows and columns
Numpy provides support for large multidimensional arrays and matrices,
with mathematical functions used for operating on this arrays.
To be able to use Numpy in your machine you will need to install it. If you
installed anacoda in your environment, it comes with Numpy installed. So
you will not need to install it. If you did not you can use
pip install numpy
Once you have Numpy installed in your machine, we can use it in our
notebooks by importing it using the followng code
import numpy as np
As np provides just an allias so that we do not have to type numpy all the
times. Lists as discussed earlier can contain elements of different
datatypes in contrast numpy arrays cointains only elements of one data
type. If the there are other datatypes in the array, numpy will try casting
them.
Module1 1
,Module 1 - numpy 2024
There are various ways we can create numpy array;
1. Creating numpy array from a list
We can convert a numpy array to a list as follows; lets say we have a list
of student marks we can convert it to numpy using the np.array()
1 import numpy as np
2 lis_marks=[40,67,89,45,67]
3 numpy_list=np.array(lis_marks)
4 print(numpy_list)
1 import numpy as np
2 lis_marks=[40,67,89,45,67]
3 numpy_list.dtype
You can specify the dtype of the created array by specifying the dtype
attribute in the the array function
Module1 2
, Module 1 - numpy 2024
1 import numpy as np
2 lis_marks=[40,67,89,45,67]
3 numpy_list_f=np.array(lis_marks, dtype=float)
4 print(numpy_list_f.dtype)
You can see from the result the numpy array has a float data type now.
The default one provided from the previous code is an integer.
Creating arrays from scratch We can also create numpy arrays from
scratch using the built in functions such as zeros, ones
The zeros() function
The numpy's zeros function return an array filled with zeros. The
parameters expected by the function include;
Shape: This specificy the dimension of the array. It is a tuple of two
values specifying the the number of rows and columns of the array.
dtype: The datatype expected of the array such as int8. The default
datatype if not specefied is float64
Module1 3
numpy / Module 1
MODULE 1: NUMPY
Introduction to Numpy
Welcome to this session on introduction to Numpy. Numpy is a library in
python used for handling multidemensional arrays. It is a short form for
numerical python. Numpy array in a way are like the lists you were
introduced to in the introduction to python programming. As the arrays
grow bigger numpy array provide efficient ways and operations for
managing arrays. Arrays organize data into rows and columns
Numpy provides support for large multidimensional arrays and matrices,
with mathematical functions used for operating on this arrays.
To be able to use Numpy in your machine you will need to install it. If you
installed anacoda in your environment, it comes with Numpy installed. So
you will not need to install it. If you did not you can use
pip install numpy
Once you have Numpy installed in your machine, we can use it in our
notebooks by importing it using the followng code
import numpy as np
As np provides just an allias so that we do not have to type numpy all the
times. Lists as discussed earlier can contain elements of different
datatypes in contrast numpy arrays cointains only elements of one data
type. If the there are other datatypes in the array, numpy will try casting
them.
Module1 1
,Module 1 - numpy 2024
There are various ways we can create numpy array;
1. Creating numpy array from a list
We can convert a numpy array to a list as follows; lets say we have a list
of student marks we can convert it to numpy using the np.array()
1 import numpy as np
2 lis_marks=[40,67,89,45,67]
3 numpy_list=np.array(lis_marks)
4 print(numpy_list)
1 import numpy as np
2 lis_marks=[40,67,89,45,67]
3 numpy_list.dtype
You can specify the dtype of the created array by specifying the dtype
attribute in the the array function
Module1 2
, Module 1 - numpy 2024
1 import numpy as np
2 lis_marks=[40,67,89,45,67]
3 numpy_list_f=np.array(lis_marks, dtype=float)
4 print(numpy_list_f.dtype)
You can see from the result the numpy array has a float data type now.
The default one provided from the previous code is an integer.
Creating arrays from scratch We can also create numpy arrays from
scratch using the built in functions such as zeros, ones
The zeros() function
The numpy's zeros function return an array filled with zeros. The
parameters expected by the function include;
Shape: This specificy the dimension of the array. It is a tuple of two
values specifying the the number of rows and columns of the array.
dtype: The datatype expected of the array such as int8. The default
datatype if not specefied is float64
Module1 3