In this lab, we shall practice some of the data generation techniques that we saw in the previous lesson in order to generate datasets for regression and classification purposes. We will run a couple of simple simulations to help us generate different datasets by controlling noise and variance parameters in the data generation process. We will also look at the statistical indicators and visual output to see how these parameters affect the accuracy of an algorithm.
In this lab you will:
- Generate datasets for classification problems
- Generate datasets for regression problems
Use make_blobs()
to create a binary classification dataset with 100 samples, 2 features, and 2 centers (where each center corresponds to a different class label). Set random_state = 42
for reproducibility.
Hint: Here's a link to the documentation for make_blobs()
.
# Your code here
Place the data in a pandas
DataFrame called df
, and inspect the first five rows of the data.
Hint: Your dataframe should have three columns in total, two for the features and one for the class label.
# Your code here
Create a scatter plot of the data, while color-coding the different classes.
# Your code here
Repeat this exercise two times by setting cluster_std = 0.5
and cluster_std = 2
.
Keep all other parameters passed to make_blobs()
equal.
That is:
- Create a classification dataset with 100 samples, 2 features, and 2 centers using
make_blobs()
- Set
random_state = 42
for reproducibility, and pass the appropriate value forcluster_std
- Set
- Place the data in a
pandas
DataFrame calleddf
- Plot the values on a scatter plot, while color-coding the different classes
What is the effect of changing cluster_std
based on your plots?
# Your code here:
# cluster_std = 0.5
# Your code here:
# cluster_std = 2
# Your comments here
Create a function reg_simulation()
to run a regression simulation creating a number of datasets with the make_regression()
data generation function. Perform the following tasks:
-
Create
reg_simulation()
withn
(noise) andrandom_state
as input parameters- Make a regression dataset (X, y) with 100 samples using a given noise value and random state
- Plot the data as a scatter plot
- Calculate and plot a regression line on the plot and calculate
$R^2$ (you can do this instatsmodels
orsklearn
) - Label the plot with the noise value and the calculated
$R^2$
-
Pass a fixed random state and values from
[10, 25, 40, 50, 100, 200]
as noise values iteratively to the function above -
Inspect and comment on the output
# Import necessary libraries
def reg_simulation(n, random_state):
# Generate X and y
# Fit a linear regression model to X, y
# Use X,y to draw a scatter plot
# Label and plot the regression line
pass
random_state = 42
for n in [10, 25, 40, 50, 100, 200]:
reg_simulation(n, random_state)
# Your comments here
In this lesson, we learned how to generate random datasets for classification and regression problems. We ran simulations for this and fitted simple models to view the effect of random data parameters including noise level and standard deviation on the performance of parameters, visually as well as objectively. These skills will come in handy while testing model performance and robustness in the future.