0% found this document useful (0 votes)
3 views9 pages

Data Science and AI with Python

The document provides an overview of using Python for data science and AI, covering topics such as exploratory data analysis, data preparation, and visualizations. It includes code snippets for importing libraries, manipulating data, and creating various types of plots. Additionally, it discusses binary classification and linear regression modeling using libraries like sklearn and seaborn.

Uploaded by

rite2shinjini
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Data Science and AI with Python

The document provides an overview of using Python for data science and AI, covering topics such as exploratory data analysis, data preparation, and visualizations. It includes code snippets for importing libraries, manipulating data, and creating various types of plots. Additionally, it discusses binary classification and linear regression modeling using libraries like sklearn and seaborn.

Uploaded by

rite2shinjini
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Maths into Data Science and AI

Python commands used in the course

Markdown formatting

Headings Maths

# Heading 1 $e^{\pi i} + 1 = 0$

## Heading 2

### Heading 3 Super script

X^2^

Bold and italic

**bold text** Tables

*italicized text* | Length | Width | Species |

|------------|-----------|------------|

Lists | 5.1| 3.3| setosa |

- First item | 4.8| 3.0| setosa |


- Second item
- Third item

TB v1.1 © MEI
1 of 9 2025-11-11

(x
Maths into Data Science and AI
Lesson 1-3: Exploratory data analysis

Importing libraries and data

Import libraries

# import pandas for data analysis


import pandas as pd

# import seaborn for visualisations


import seaborn as sns

Import a csv file

# import the csv file to a data frame called weather_data


weather_data = pd.read_csv('/kaggle/input/uk-daily-weather-1961-2024/uk_climate.csv')

Display a data set

# display the data to verify it has imported


weather_data

# display the first 10 rows of a data frame


weather_data.head(10)

Preparing data

Get information about the data types

# display the data types of the features


weather_data.info()

TB v1.1 © MEI
2 of 9 2025-11-11

(x
Maths into Data Science and AI

Create a new numerical feature

# create a new numerical feature from a function of existing features


bike_data['total'] = bike_data['casual']+bike_data['registered']

# divide GNI by population, round it the nearest cent, and assign it to a new feature
wdi_data['GNI per capita'] = (wdi_data['GNI'] / wdi_data['Population']).round(2)

Create a new categorical feature from a numerical feature (e.g. when categories have been stored as numbers

# replace numbers with categories


bike_data['weather'] = bike_data['weathersit'].replace({1: 'Clear', 2: 'Cloud', 3:'Rain', 4:'Heavy rain'})

Bin ages into categories

bins=[0,17,30,60,80]
labels = ['Child', 'Young Adult', 'Adult', 'Senior']
titanic_data['age_cat'] = [Link](titanic_data['age'], bins = bins, labels = labels, right = True)

Exploratory data analysis

Get a summary of numerical features

# find summary statistics of all the numerical features


bike_data.describe()

# use describe() to output a summary of the temperature column


weather_data['temp'].describe()

Get a summary of a numerical feature rounded to 2 decimal places

TB v1.1 © MEI
3 of 9 2025-11-11

(x
Maths into Data Science and AI
# use describe to get the statistics for the feature rounded to 2 dp
weather_data['temp'].describe().round(2)

Get a summary of a numerical feature grouped by a categorical feature

# Group the data by season and show statistics for temperature


weather_data.groupby('season')['temp'].describe().round(2)

Create a two-way table of frequencies split across two categorical features (using crosstab)

# display a 2-way table


[Link](wdi_data['High Income Economy'], wdi_data['Region'])

Create a two-way table with the means of a third, numerical, feature in the cells (using crosstab)

# create a two-way table for two categories using crosstab with the mean of a numerical feature in each cell
[Link](daytime_data['season'],daytime_data['weather'],values=daytime_data['total'],aggfunc='mean').round(2)

Slicing data

Take a slice: create a new data set from a subset of an existing one – NB it is good practice to add .copy() at the end

# create a new data set called heathrow_data where the station value is 'heathrow'
heathrow_data = weather_data[weather_data['station'] == 'heathrow'].copy()

Create a slice based on a numerical condition

# create a slice of the data with just non working days


weekend_data = bike_data[bike_data['workingday'] == 0].copy()

TB v1.1 © MEI
4 of 9 2025-11-11

(x
Maths into Data Science and AI

Create a slice of a data set based on multiple conditions (using & for 'and')

# create a slice of the data based on the condition


daytime_data = bike_data[(bike_data['hr'] >6) & (bike_data['hr'] <21)].copy()

Visualisations

Plots for categorical features

Bar chart split by categories showing frequencies

# bar chart split by a category (frequencies)


[Link](data=wdi_data, x='Region', hue='High Income Economy', multiple='stack', aspect=2);

Bar chart split by categories showing proportions

# bar chart split by a category (proportions)


[Link](data=wdi_data, x='Region', hue='High Income Economy', stat='proportion', multiple='fill', aspect=2);

Normalised ‘histograms’ for each of the values in a categorical column

# plot histograms using a category for columns - stat='density', common_norm=False normalises each one
[Link](data=wdi_data, x='Life expectancy, female', col='Region', stat='density', common_norm=False);

1D plots for a single numerical feature

Draw a boxplot
# box plot for temp
[Link](data=weather_data, kind='box', x='temp', aspect=2);

TB v1.1 © MEI
5 of 9 2025-11-11

(x
Maths into Data Science and AI

Draw a boxplot grouped by a categorical feature

# Create box plots for daily average temperature, grouped by season


[Link](data=weather_data, kind='box', x='temp', y='season', aspect=2);

Violin plot - NB the hue must be a categorical feature with exactly two values

# create a violin plot with a continuous variable on x, a categorical on y and a binary variable for hue
[Link](data=bike_data, kind='violin', x='total', y='season', hue='workingday', split=True);

KDE (kernel density estimate) to display the distribution

# kind='kde' - estimate the shape of a continuous distribution


[Link](data=wdi_data, kind='kde', x='Life expectancy, female', hue='High Income Economy', aspect=2);

2D plots for a pair of numerical features

Scatter plot

# scatter plot
[Link](data=bike_data, kind='scatter', x='temp', y='total', alpha=0.4, aspect=2)

Scatter plot with a category for the colour (or hue)

# hue='Region' - colour by region


[Link](data=wdi_data, x='Internet use', y='Emissions per capita', hue='Region', aspect=2);

Setting titles and axes

# plot the visualisation

TB v1.1 © MEI
6 of 9 2025-11-11

(x
Maths into Data Science and AI
plot = [Link](data=wdi_data, x='Life expectancy, female', aspect=2)

# Label the axes and show the plot


[Link](xlabel='Female life expectancy (years)', ylabel='Frequency', title='Average female life expectancy')
plot

Lesson 4-6: Binary classification

Importing libraries and data

# Import functions from sklearn for building the model, training-testing split, visualising the model and metrics
from [Link] import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
import [Link] as plt
from [Link] import plot_tree
from [Link] import accuracy_score, precision_score, recall_score

# Function to draw the model


def plot_decision_tree(tree_model, fontsize=12):
fig, ax = [Link](figsize=(20,8))
plot_tree(tree_model,
filled=True,
impurity=False,
feature_names=input_features,
class_names=['No','Yes'],
proportion=True,
ax=ax)
[Link]()

Exploratory data analysis

Dropping features from the dataset

bank_data = bank_data.drop(['RowNumber', 'CustomerId', 'Surname'], axis=1)

TB v1.1 © MEI
7 of 9 2025-11-11

(x
Maths into Data Science and AI
Create a table comparing the means for a range of numerical features grouped by a categorical feature

# create a list of the features to be explored


features=['GP', 'MIN', 'PTS', 'FGS', 'FGA', '3PM', '3PA', 'FTM', 'FTA', 'OREB', 'DREB', 'AST', 'STL', 'BLK', 'TOV']

# use groupby to get the means of these for 'no' and 'yes'
nba_data.groupby('Successful')[features].mean().round(2)

Create a two-way table of frequencies split across two categorical features (using crosstab)

# create a two-way table for two categories


[Link](hotel_data['hotel'],hotel_data['cancelled'])

Create a two-way table to compare proportions of each row (using crosstab)

# create a two-way table for two categories showing the proportions of each row
[Link](hotel_data['hotel'],hotel_data['cancelled'],normalize='index').round(3)

Create a classification model with multiple depths, measure the precision and recall, display the confusion matrix

# define the input features create the input table (X) and define the target feature (y), perform the training-testing split
input_features = ['3PM','PTS']
X = nba_data[input_features]
y = nba_data['5Yrs']
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, random_state=1)

# create the model and display the decision tree


tree_model = DecisionTreeClassifier(max_depth=2).fit(X_train, y_train)
plot_decision_tree(tree_model)

# create a list of the predictions and


calculate the metrics and confusion matrix
y_pred = tree_model.predict(X_test)
print("Precision: ",round(100*precision_score(y_test, y_pred, zero_division=0),1),"%")
print("Recall: ",round(100*recall_score(y_test, y_pred),1),"%")
[Link](y_test, y_pred, rownames=['Actual'], colnames=['Predicted'], margins=True)

TB v1.1 © MEI
8 of 9 2025-11-11

(x
Maths into Data Science and AI
Lesson 7-8: Linear regression

Import functions for linear regression

# import functions from sklearn for modelling


from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from [Link] import r2_score, mean_squared_error

Visualisations

# plot all possible scatter diagrams


features = ['EngineSize', 'Mass']
[Link](cars_data_clean, x_vars=features, y_vars=['CO2']);

Linear regression model

Create a linear regression model: Define the input/output features, perform the training/testing split, create a linear model and measure the model

# Define the input and target data, perform the training-testing split
input_features = ['NOX']
X = house_data[input_features]
y = house_data['MEDV']
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, random_state=1)

# create the model and display the parameters - output the coefficients and y-intercept
linear_model = LinearRegression().fit(X_train,y_train)
print('Coefficients: \n', linear_model.coef_.round(3))
print('Intercept: \n', linear_model.intercept_.round(3))

# create a list of the predictions and calculate the metrics (RMSE and R² score) by to the target values in y_test
y_pred = linear_model.predict(X_test)
print('RMSE: ',mean_squared_error(y_test, y_pred, squared=False).round(3))
print('R²: ',(100*r2_score(y_test, y_pred)).round(3),'%')

TB v1.1 © MEI
9 of 9 2025-11-11

(x

You might also like