Skip to Main Content

Learning Python: Part III: Libraries

A beginner's guide to Python programming.

Introduction

Course Instructions Part III:

Welcome to the third part of the learning python self-paced course.  On this page, we will learn about various library packages that are useful for research and data science.  Below is a brief description of what you will learn and various tabs.  Go through the different tabs below.  Under the Sample Code subheading you will find a link to get to the code, so you can start learning hands-on. 

Coming soon: Seaborn.

More Information about Libraries for Data Science:

Libraries

What is it?

Numpy is a Python package that allows you to create multidimensional arrays, matrices, and other functions, including mathematical, logical, shape manipulation, sorting, basic linear algebra, basic statistics, etc.

Basic Syntax

First the numpy library needs to be imported.  You can rename it anything you want but the consensus has been np.  By renaming numpy to np you can save time typing.  To create an array you can use the function np.array().  Be careful how you enter in the array or matrix because there is a certain format.  See below:

import numpy as np  # import numpy with the name np for easier use.

arr = np.array([1,2,3,4,5]) # the list is converted to a numpy array.

matrix = np.array([[1,2],[3,4]]) # you can make 2D matrices as well.

Why it Matters?

Numpy has been optimized for quick, n-dimensional operations.  Additionally, it acts in the background of other important libraries such as Pandas (dataframes), Scipy (scientific computing), Matplotlib (plotting), scikit-learn (machine learning), and PyTorch (deep learning).

Sample Code

This sample code goes through more detail of the syntax and other functions.  Go through it carefully and try modifying the code to see what happens. You can get the code from:

References

Here are some Python cheat sheets:

What is it?

Pandas is a library that creates dataframes that can be quickly analyzed.  You can even import text, excel, csv and other file formats that your data may be in.  Then use pandas to filter, separate, combine, and analyze your data, and much more!

Basic Syntax

First the numpy library needs to be imported.  You can rename it anything you want but the consensus has been np.  By renaming numpy to np you can save time typing.  To create an array you can use the function np.array().  Be careful how you enter in the array or matrix because there is a certain format.  See below:

import pandas as pd  # import pandas with the name pd for easier use.

data = pd.read_csv('data.csv') # load your data directly.

data_details = data.describe() # you can get basic statistics (mean, std, min, max, ect.)

Why it Matters?

Pandas is the one of the greatest tool when it comes to data analysis.  It is fast, flexible, and a powerful tool when working with tables spreadsheets.

Sample Code

This sample code goes through more detail of the syntax and other functions.  Go through it carefully and try modifying the code to see what happens. You can get the code from:

References

Here are some Python cheat sheets:

What is it?

Matplotlib is a python library that is used to create and plot figures.  This and seaborn will be the main libraries you will use to make all your figures.

Basic Syntax

First the numpy library needs to be imported.  You can rename it anything you want but the consensus has been np.  By renaming numpy to np you can save time typing.  To create an array you can use the function np.array().  Be careful how you enter in the array or matrix because there is a certain format.  See below:

import numpy as np              # to create sample data to plot
import matplotlib.pyplot as plt # import matplotlib and simplify the name to plt

# Create sample data
t = np.arange(0.0, 2.0, 0.01) # load your data directly.
s = np.sin(2*np.pi*t)
c = np.cos(2*np.pi*t)

# Plot the data
fig, plt.figure()
ax = plt.axes()
plt.plot(t,s)
plt.plot(t,c)

Why it Matters?

Matplotlib is a powerful tool that will help you visualize data and allow you to customize different parts of your figure.

Sample Code

This sample code goes through more detail of the syntax and other functions.  Go through it carefully and try modifying the code to see what happens. You can get the code from: