Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Machine Learning & AI - Lesson 7
NumPy, Pandas, and Matplotlib fundamentals for machine learning
Jupyter / Colab
interactive Python environments
NumPy
arrays, vectorized ops
Pandas
Series and DataFrames
Matplotlib
line, bar, scatter plots
Jupyter Notebook and Google Colab are interactive Python environments, Colab runs entirely in the cloudSeries and DataFrame structuresnp.array() converts a Python list into an ndarraynp.arange(start, stop, step) generates an evenly stepped range of integersimport numpy as np
arr = np.array([0, 1, 2, 3, 4])
np.arange(0, 10) # [0 1 2 ... 9]
np.arange(0, 10, 3) # [0 3 6 9]zeros/ones build filled arrays of a given shaperandom.randint and linspace generate random or evenly spaced valuesnp.zeros((5, 5))
np.ones((2, 4))
np.random.randint(0, 100, (3, 3))
np.linspace(0, 10, 6) # 6 evenly spaced points from 0 to 10.max(), .min(), .mean(), .argmin(), .argmax().reshape() changes an array's shape without changing its dataarr2 = np.random.randint(0, 100, 10)
arr2.max(), arr2.min(), arr2.mean()
arr2.argmin(), arr2.argmax()
arr3.reshape(3, 3)mat[row, col]; : selects a full row or columnmat = np.arange(0, 100).reshape(10, 10)
mat[0, 1] # single element
mat[:, 0] # first column
mat[0:3, 0:3] # top-left 3x3 block
mat[0:3, -3:-1] # slicing with negative indicesstep_counts = pd.Series([3620, 7891, 9761], name='steps')
step_counts.index = pd.date_range('20240329', periods=3)
step_counts['2024-03-30'] # access like a dictionary
step_counts.dtypesSeries into a labeled table.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.ilocactivity_df.loc['2024-04-01':'2024-04-03', ['Walking']]
activity_df.iloc[0, :] # first row, all columns
activity_df.iloc[:2, 0] # first 2 rows, first columnpd.read_csv()DataFrame.data = pd.read_csv('Iris_Data.csv')
data.head()
data.shape # (150, 5)
data['sepal_width']data.loc[data.sepal_width > 3.5]data['sepal_area'] = data.sepal_length * data.sepal_widthgroupby('species').size() gives 50 samples per species, perfectly balancedgroup_sizes = data.groupby('species').size().mean(), .median(), .mode().std() (standard deviation), .var() (variance), .sem() (standard error)data.petal_length.mean(), data.petal_length.median()
data.petal_length.std(), data.petal_length.var()plt.hist(data.petal_length, bins=25)
plt.plot(data.sepal_length, data.sepal_width, marker='o')
plt.xlabel('Petal Length (cm)')
plt.title('Distribution of Petal Lengths')Series and DataFrame are the standard tools for tabular data in Python.loc selects by label, .iloc selects by integer position, know the differenceDataFrame columns into histograms, scatter plots, and line charts for quick EDAMahmoud Abas|Lab 1: Introduction to Python for Data Science