Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Jupyter/Google Colab, NumPy, Pandas, and Matplotlib for data science workflows
Jupyter Notebooks
Running Python interactively, cell-by-cell
Google Colab
Cloud notebooks, no local setup, free GPU access
NumPy
Arrays, vectorized operations, indexing/slicing/reshaping
Pandas
Series and DataFrames: cleaning, transforming, aggregating
Matplotlib
Line/bar/scatter/histogram plots, labels, legends
np.array()import numpy as np
my_list = [0, 1, 2, 3, 4]
arr = np.array(my_list)
# array([0, 1, 2, 3, 4])
np.arange(0, 10, 3)
# array([0, 3, 6, 9])
np.zeros((5, 5))
np.ones((2, 4))np.random.randint(low, high, size) for random integersnp.linspace(start, stop, num) for evenly spaced floatsnp.random.randint(0, 100, (3, 3))
# array([[27, 73, 80],
# [53, 26, 91],
# [91, 54, 68]])
np.linspace(0, 10, 6)
# array([ 0., 2., 4., 6., 8., 10.]).reshape() changes an array's dimensions without copying datanp.random.seed(101)
arr2 = np.random.randint(0, 100, 10)
arr2.max() # 95
arr2.min() # 9
arr2.mean() # 60.8
arr2.argmin() # index of min -> 7
arr2.argmax() # index of max -> 0
arr2.reshape(2, 5)mat[row, col]: selects an entire row or column; slices work like Python listsmat = np.arange(0, 100).reshape(10, 10)
mat[0, 1] # single element -> 1
mat[:, 0] # first column
mat[0, :] # first row
mat[0:3, 0:3] # top-left 3x3 block
mat[0:3, -3:-1] # negative indices count from the endimport pandas as pd
step_data = [3620, 7891, 9761, 3907, 4338, 5373]
step_counts = pd.Series(step_data, name='steps')
step_counts.index = pd.date_range('20240329', periods=6)
step_counts['2024-04-01'] # label lookup -> 3907
step_counts[3] # position lookup -> 3907
step_counts.dtypes # int64cycling_data = [10.7, 0, None, 2.4, 15.3, 10.9]
joined_data = list(zip(step_data, cycling_data))
activity_df = pd.DataFrame(
joined_data,
index=pd.date_range('20240329', periods=6),
columns=['Walking', 'Cycling']
).loc selects by label (index value, column name).iloc selects by integer position (like array indexing)activity_df.loc['2024-04-01']
activity_df.loc['2024-04-01':'2024-04-03', ['Walking']]
activity_df.iloc[0, :] # first row, all columns
activity_df.iloc[:2, 0] # first two rows, first columnpd.read_csv() loads a dataset from a file into a DataFrame.head() previews the first rows; .shape gives (rows, columns)data = pd.read_csv('.\\Iris_Data.csv')
data.head()
data.shape # (150, 5)
data.shape[1] # 5 columns
data['sepal_width'] # select one column
data.loc[2, ['sepal_length']].groupby() aggregates rows sharing a valuedata['species'] = data.species.str.replace('Iris-', '')
# Filter rows
data.loc[(data.sepal_width > 3.5)]
# Derived column
data['sepal_area'] = data.sepal_length * data.sepal_width
# Group and count
data.groupby('species').size()data.sepal_width.mean() # 3.054
data.petal_length.median() # 4.35
data.petal_length.mode() # 1.5
data.petal_length.std() # 1.764
data.petal_length.var() # 3.113
data.petal_length.sem() # 0.144plt.hist() for distributions, plt.plot() for line/scatter trendsimport matplotlib.pyplot as plt
ax = plt.axes()
ax.hist(data.petal_length, bins=25)
ax.set(xlabel='Petal Length (cm)',
ylabel='Frequency',
title='Distribution of Petal Lengths')
plt.plot(data.sepal_length, data.sepal_width, marker='o')
plt.xlabel("Petal Length (cm)")
plt.ylabel("sepal_width")NumPy arrays enable fast, vectorized numeric computation and flexible indexing/reshapingPandas Series and DataFrames organize tabular data with label-based (.loc) and position-based (.iloc) accessread_csv, filtering, new columns, and .groupby() form the core data-cleaning workflowstd, var, and sem summarize a dataset's center and spreadMatplotlib turns DataFrame columns directly into histograms and line/scatter plotsJupyter/Colab notebooks are the standard interactive environment for this entire workflowMahmoud Abas|Lab 1: Introduction to Python for Data Science