Module 3: NumPy Flashcards
definition of libraries
Definition of libraries: Python libraries are collections of pre-written code and functions that extend the capabilities of the Python programming language. They provide a wide range of tools and modules for various tasks, making it easier for developers to work on specific tasks without reinventing the wheel.
Note that packages are third party libraries that are made available to python through pip install. One of the locations you get all of these packages is through the pypi.org website. After you pip install then you can go about importing it into your own code
Also note that API requests which are third party services that code talks to – the code acts as a browser to connect to the API server and downloads data to incorporate into the program
Requests library for example helps you make browser requests
The response from the API will return it in JSON format – which is really a dictionary in python that is in curly braces and key-value pairs
what is numpy and what is it good for
NumPy is a library that allows you to create large matricies and arrays from imported data. For example, if you import data in the form of a list, by using NumPy you can turn it into a matrix or array. Once it is organized, you have mathematical functions available to you within NumPy to draw some initial conclusions about your dataset. It allows you to create any number of dimensions with your data for a machine learning model to interpret it. So:
NumPy is good and efficient way to handle data
A good way of performing arithmetic and statistical operations
A good way of preparing data for machine learning models
what is an array and matrix
In computer programming, an array is a structure for storing and retrieving data. We often talk about an array as if it were a grid in space, with each cell storing one element of the data. It is like a list, tuple, dictionary, but in that it allows us to use multiple dimensions.
What is a Matrix in Python? A matrix in Python is a rectangular Numpy array. This array has to be two-dimensional. It contains data stored in the array’s rows and columns.
You can’t imagine it, but think of multiple dimensions as attributes and not physical representations – your car for example can have a 100 attributes so 100 dimensions
creating numpy array basics
NumPy arrays can be created:
From other python structures, for example lists or tuples using an array() function
Using random functions to create an array of random data, or using the range () function to create an array of a sequence of numbers
Using one of the zeros(), ones(), or empty() functions to create an array of 0s or 1s or an array of uninitialized empty elements
Creating NumPy arrays from lists, an overview of array properties:
The main NumPy library is numpy which we need to import to be able to use the NumPy functionality:
import numpy as np
numerical data arranged in an array like structure in python can be converted to arrays using the array() function. The most obvious examples of array-like structures are lists and tuples:
First, we will use a simple list of 4 integer numbers to create an array
list_int = [8, 3, 34, 111]
a_int = np.array(list_int)
a_int
To create a 2-dimensional array, we can use a list of lists. Below is an array with 2 rows and 3 columns:
list_2dim = [[1.5,2,3], [4,5,6]]
a_2dim = np.array(list_2dim)
a_2dim
Output:
array([[1.5, 2. , 3. ],
[4. , 5. , 6. ]])
The rank of the array a_int is 1 — it is a one-dimensional array of numbers. To check the rank of the array, use ndim, which outputs the number of axes (dimensions) of the array.
a_int.ndim
To see what those dimensions are use .shape. The output is a tuple of integers indicating the size of the array in each dimension. If you do the second list, which has two dimensions, it will print the rows by the columns.
a_int.shape
output: (2, 3)
To see the type of the data format:
a_2dim.dtype
We can create a NumPy array from a mix of tuples and lists:
a_mix = np.array([[1, 2.0], [0, 0],(5.78, 3.)])
a_mix
The type of the array can be explicitly specified at creation time:
a_intfloat = np.array([[1, 2], [3, 4]], dtype = float)
a_intfloat