The Wayback Machine - https://web.archive.org/web/20240610095221/https://www.geeksforgeeks.org/python-generate-test-datasets-for-machine-learning/
Open In App

Generate Test Datasets for Machine learning

Last Updated : 11 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Whenever we think of Machine Learning, the first thing that comes to our mind is a dataset. While there are many datasets that you can find on websites such as Kaggle, sometimes it is useful to extract data on your own and generate your own dataset. Generating your own dataset gives you more control over the data and allows you to train your machine-learning model.  In this article, we will generate random datasets using sklearn.datasets library in Python.

Generate test datasets for Classification:

Binary Classification

Example 1: The 2d binary classification data generated by make_circles() have a spherical decision boundary.

Python3




# Import necessary libraries
from sklearn.datasets import make_circles
import matplotlib.pyplot as plt
 
# Generate 2d classification dataset
X, y = make_circles(n_samples=200, shuffle=True,
                    noise=0.1, random_state=42)
# Plot the generated datasets
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.show()


Output:

make_circles() - Geeksforgeeks

make_circles()

Example 2: Two interlocking half circles represent the 2d binary classification data produced by the make_moons() function.

Python3




#import the necessary libraries
from sklearn.datasets import make_moons
import matplotlib.pyplot as plt
# generate 2d classification dataset
X, y = make_moons(n_samples=500, shuffle=True,
                  noise=0.15, random_state=42)
# Plot the generated datasets
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.show()


Output:

make_moons() -Geeksforgeeks

make_moons()

Multi-Class Classification

Example 1: Data generated by the function make_blobs() are blobs that can be utilized for clustering.

Python3




#import the necessary libraries
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
 
# Generate 2d classification dataset
X, y = make_blobs(n_samples=500, centers=3, n_features=2, random_state=23)
 
# Plot the generated datasets
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.show()


Output:

make_blobs() - Geeksforgeeks

make_blobs()

Example 2: To generate data by the function make_classification() need to balance between n_informative, n_redundant and n_classes attributes X[:, :n_informative + n_redundant + n_repeated]

Python3




#import the necessary libraries
from sklearn.datasets import make_classification
import matplotlib.pyplot as plt
 
# generate 2d classification dataset
X, y = make_classification(n_samples = 100,
                           n_features=2,
                           n_redundant=0,
                           n_informative=2,
                           n_repeated=0,
                           n_classes =3,
                           n_clusters_per_class=1)
 
# Plot the generated datasets
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.show()


Output:

make_classification() - Geeksforgeeks

make_classification()

Example 3:A random multi-label classification data is created by the function make make_multilabel_classification() 

Python3




# Import necessary libraries
from sklearn.datasets import make_multilabel_classification
import pandas as pd
import matplotlib.pyplot as plt
 
# Generate 2d classification dataset
X, y = make_multilabel_classification(n_samples=500, n_features=2,
                                      n_classes=2, n_labels=2,
                                      allow_unlabeled=True,
                                      random_state=23)
# create pandas dataframe from generated dataset
df = pd.concat([pd.DataFrame(X, columns=['X1', 'X2']),
                pd.DataFrame(y, columns=['Label1', 'Label2'])],
               axis=1)
display(df.head())
 
# Plot the generated datasets
plt.scatter(df['X1'], df['X2'], c=df['Label1'])
plt.show()


Output:

    X1    X2    Label1    Label2
0    14.0    34.0    0    1
1    30.0    22.0    1    1
2    29.0    19.0    1    1
3    21.0    19.0    1    1
4    16.0    32.0    0    1
make_multilabel_classification() - Geeksforgeeks

make_multilabel_classification()

Generate test datasets for Regression:

Example 1:  Generate a 1-dimensional feature and target for linear regression using make_regression

Python3




# Import necessary libraries
from sklearn.datasets import make_regression
import matplotlib.pyplot as plt
# Generate 1d Regression dataset
X, y = make_regression(n_samples = 50, n_features=1,noise=20, random_state=23)
# Plot the generated datasets
plt.scatter(X, y)
plt.show()


Output:

make_regression() -Geeksforgeeks

make_regression()

Example 2:  Multilabel feature using make_sparse_uncorrelated()

Python3




# Import necessary libraries
from sklearn.datasets import make_sparse_uncorrelated
import matplotlib.pyplot as plt
# Generate 1d Regression dataset
X, y = make_sparse_uncorrelated(n_samples = 100, n_features=4, random_state=23)
# Plot the generated datasets
plt.figure(figsize=(12,10))
for i in range(4):
    plt.subplot(2,2, i+1)
    plt.scatter(X[:,i], y)
    plt.xlabel('X'+str(i+1))
    plt.ylabel('Y')
plt.show()


Output:

make_sparse_uncorrelated()-Geeksforgeeks

make_sparse_uncorrelated()

Example: 3  Multilabel feature using make_friedman2()

Python3




# Import necessary libraries
from sklearn.datasets import make_friedman2
import matplotlib.pyplot as plt
# Generate 1d Regression dataset
X, y = make_friedman2(n_samples = 100, random_state=23)
# Plot the generated datasets
plt.figure(figsize=(12,10))
for i in range(4):
    plt.subplot(2,2, i+1)
    plt.scatter(X[:,i], y)
    plt.xlabel('X'+str(i+1))
    plt.ylabel('Y')
plt.show()


Output:

make_friedman2() - Geeksforgeeks

make_friedman2()

 



Previous Article
Next Article

Similar Reads

Boston Housing Prices Datasets - Tensorflow.keras Datasets
Are you considering investing in the Boston real estate market? If so, you'll want to make informed decisions based on accurate data. That's where the Boston Housing Prices Datasets come into play. This comprehensive collection of datasets provides you with essential information on housing prices in the Boston area, allowing you to analyze trends,
7 min read
ChatGPT Prompt to get Datasets for Machine Learning
With the development of machine learning, access to high-quality datasets is becoming increasingly important. Datasets are crucial for assessing the accuracy and effectiveness of the final model, which is a prerequisite for any machine learning project. In this article, we'll learn how to use a ChatGPT[OpenAI] template prompt to collect a variety o
8 min read
Top Machine Learning Dataset: Find Open Datasets
In the realm of machine learning, data is the fuel that powers innovation. The quality and quantity of data directly influence the performance and capabilities of machine learning models. Open datasets, in particular, play an important role in democratizing access to data and fostering collaboration and innovation within machine learning. In this a
8 min read
Python | Create Test DataSets using Sklearn
Python's Sklearn library provides a great sample dataset generator which will help you to create your own custom dataset. It's fast and very easy to use. Following are the types of samples it provides.For all the above methods you need to import sklearn.datasets.samples_generator. C/C++ Code # importing libraries from sklearn.datasets import make_b
3 min read
Using Google Cloud Function to generate data for Machine Learning model
Prerequisite: Deploy cloud function on Google Cloud Platform Do you search for data to train your model online? What if we tell that you can generate your own data in just a few lines on code? All you need is a Google Cloud Platform account and know how to deploy simple code to Cloud Function. We will be using Google Sheets to store the data. You c
4 min read
Support vector machine in Machine Learning
In this article, we are going to discuss the support vector machine in machine learning. We will also cover the advantages and disadvantages and application for the same. Let's discuss them one by one. Support Vector Machines : Support vector machine is a supervised learning system and is used for classification and regression problems. Support vec
9 min read
Azure Virtual Machine for Machine Learning
Prerequisites: About Microsoft Azure, Cloud Based Services Some of the Machine Learning and Deep Learning algorithms may require high computation power which may not be supported by your local machine or laptop. In that case, creating a Virtual Machine on a cloud platform can provide you the expected computation power. We can have a system with hig
4 min read
Machine Learning Model with Teachable Machine
Teachable Machine is a web-based tool developed by Google that allows users to train their own machine learning models without any coding experience. It uses a web camera to gather images or videos, and then uses those images to train a machine learning model. The user can then use the model to classify new images or videos. The process of creating
7 min read
Artificial intelligence vs Machine Learning vs Deep Learning
Nowadays many misconceptions are there related to the words machine learning, deep learning, and artificial intelligence (AI), most people think all these things are the same whenever they hear the word AI, they directly relate that word to machine learning or vice versa, well yes, these things are related to each other but not the same. Let's see
4 min read
Difference Between Artificial Intelligence vs Machine Learning vs Deep Learning
Artificial Intelligence is basically the mechanism to incorporate human intelligence into machines through a set of rules(algorithm). AI is a combination of two words: "Artificial" meaning something made by humans or non-natural things and "Intelligence" meaning the ability to understand or think accordingly. Another definition could be that "AI is
14 min read