


How to convert Scikit-learn's IRIS dataset into a dataset with only two features in Python?
Iris, a multivariate flower dataset, is one of the most useful Python scikit-learn datasets. It is divided into 3 categories of 50 instances each and contains measurements of the sepal and petal parts of three species of iris (Iris mountaina, Iris virginia and Iris variegated). Apart from this, the Iris dataset contains 50 instances of each of the three species and consists of four features, namely sepal_length (cm), sepal_width (cm), petal_length (cm), petal_width (cm).
We can use Principal Component Analysis (PCA) to transform the IRIS dataset into a new feature space with 2 features.
step
We can convert the IRIS dataset into a 2-feature dataset using PCA in Python by following the steps given below -
Step 1 - First, import the necessary packages from scikit-learn. We need to import the dataset and decomposition package.
Steps 2 - Load the IRIS dataset.
Steps 3 - Print detailed information about the dataset.
Steps 4 - Initialize Principal Component Analysis (PCA) and apply the fit() function to fit the data. p>
Step 5 - Convert the dataset into a new dimension, a 2-feature dataset.
Example
In the example below, we will transform the scikit-learn IRIS plant dataset into 2 features via PCA using the above steps.
# Importing the necessary packages from sklearn import datasets from sklearn import decomposition # Load iris plant dataset iris = datasets.load_iris() # Print details about the dataset print('Features names : '+str(iris.feature_names)) print('\n') print('Features size : '+str(iris.data.shape)) print('\n') print('Target names : '+str(iris.target_names)) print('\n') X_iris, Y_iris = iris.data, iris.target # Initialize PCA and fit the data pca_2 = decomposition.PCA(n_components=2) pca_2.fit(X_iris) # Transforming iris data to new dimensions(with 2 features) X_iris_pca2 = pca_2.transform(X_iris) # Printing new dataset print('New Dataset size after transformations: ', X_iris_pca2.shape)
Output
It will produce the following output -
Features names : ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'] Features size : (150, 4) Target names : ['setosa' 'versicolor' 'virginica'] New Dataset size after transformations: (150, 2)
How to convert Iris dataset to 3-feature dataset?
We can transform the Iris dataset into a new feature space with 3 features using a statistical method called Principal Component Analysis (PCA). PCA essentially linearly projects the data into a new feature space by analyzing the features of the original data set.
The main concept behind PCA is to select the "main" features of the data and build features based on them. It will give us a new dataset that is smaller in size but has the same information as the original dataset.
Example
In the example below, we will use PCA to transform the scikit-learn Iris plant dataset (initialized with 3 components).
# Importing the necessary packages from sklearn import datasets from sklearn import decomposition # Load iris plant dataset iris = datasets.load_iris() # Print details about the dataset print('Features names : '+str(iris.feature_names)) print('\n') print('Features size : '+str(iris.data.shape)) print('\n') print('Target names : '+str(iris.target_names)) print('\n') print('Target size : '+str(iris.target.shape)) X_iris, Y_iris = iris.data, iris.target # Initialize PCA and fit the data pca_3 = decomposition.PCA(n_components=3) pca_3.fit(X_iris) # Transforming iris data to new dimensions(with 2 features) X_iris_pca3 = pca_3.transform(X_iris) # Printing new dataset print('New Dataset size after transformations : ', X_iris_pca3.shape) print('\n') # Getting the direction of maximum variance in data print("Components : ", pca_3.components_) print('\n') # Getting the amount of variance explained by each component print("Explained Variance:",pca_3.explained_variance_) print('\n') # Getting the percentage of variance explained by each component print("Explained Variance Ratio:",pca_3.explained_variance_ratio_) print('\n') # Getting the singular values for each component print("Singular Values :",pca_3.singular_values_) print('\n') # Getting estimated noise covariance print("Noise Variance :",pca_3.noise_variance_)
Output
It will produce the following output -
Features names : ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'] Features size : (150, 4) Target names : ['setosa' 'versicolor' 'virginica'] Target size : (150,) New Dataset size after transformations : (150, 3) Components : [[ 0.36138659 -0.08452251 0.85667061 0.3582892 ] [ 0.65658877 0.73016143 -0.17337266 -0.07548102] [-0.58202985 0.59791083 0.07623608 0.54583143]] Explained Variance: [4.22824171 0.24267075 0.0782095 ] Explained Variance Ratio: [0.92461872 0.05306648 0.01710261] Singular Values : [25.09996044 6.01314738 3.41368064] Noise Variance : 0.02383509297344944
The above is the detailed content of How to convert Scikit-learn's IRIS dataset into a dataset with only two features in Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



We teach you step by step how to convert USDTERC20 to TRC20 network. This is because many people like to move the USDT stablecoin from the Ethereum network to the Tron network to save on transaction fees. So, if you want to convert your ERC-20 tokens to TRC-20, I believe this tutorial will be helpful to you. The difference between ERC-20 and TRC-20 ERC-20 tokens and TRC-20 tokens represent tokens based on the Ethereum network and the Tron network respectively. There are some differences between the two networks, mainly in the following aspects: First, the Ethereum network often faces congestion and high gas fees, which may lead to transaction delays and high transaction costs. In comparison, the Tron network is relatively less congested

Practical tips for converting full-width English letters into half-width forms. In modern life, we often come into contact with English letters, and we often need to input English letters when using computers, mobile phones and other devices. However, sometimes we encounter full-width English letters, and we need to use the half-width form. So, how to convert full-width English letters to half-width form? Here are some practical tips for you. First of all, full-width English letters and numbers refer to characters that occupy a full-width position in the input method, while half-width English letters and numbers occupy a full-width position.

AI files refer to vector graphics files created by Adobe Illustrator (AI for short) software, while CDR files refer to vector graphics files created by CorelDRAW software. Since these two softwares are developed by different manufacturers, their file formats are different and cannot be directly converted to each other. However, we can convert AI files to CDR files through some methods. A commonly used conversion method will be introduced below. Step 1: Export AI files to EPS format AdobeIllust

In this article, we will show you how to convert OpenDocumentTextDocument (ODT) files to Microsoft Word (Docx, DOC, etc.). Format. How to Convert ODT to Word in Windows 11/10 Here is how you can convert ODT documents to DOC or DOCX format on Windows PC: Convert ODT to Word using WordPad or Word The first method we are going to show you Is to use WordPad or MicrosoftWord to convert ODT to Word. Here are the steps to achieve this: First, open the WordPad app using the Start menu. Now, go to

Converting a virtual machine (VM) to a physical machine is the process of migrating a virtual instance and associated application software to a physical hardware platform. This conversion helps optimize operating system performance and hardware resource utilization. This article aims to provide an in-depth look at how to make this conversion. How to implement migration from virtual machine to physical machine? Typically, the conversion process between a virtual machine and a physical machine is performed outside the virtual machine by third-party software. This process consists of multiple stages involving the configuration of virtual machines and the transfer of resources. Prepare the physical machine: The first step is to ensure that the physical machine meets the hardware requirements for Windows. We need to back up the data on a physical machine as the conversion process will overwrite the existing data. *Username and password for an administrator account with administrator rights to create system images. will be virtual

Golang time conversion: How to convert timestamp to string In Golang, time operation is one of the very common operations. Sometimes we need to convert the timestamp into a string for easy display or storage. This article will introduce how to use Golang to convert timestamps to strings and provide specific code examples. 1. Conversion of timestamps and strings In Golang, timestamps are usually expressed in the form of integer numbers, which represent the number of seconds from January 1, 1970 to the current time. The string is

This article will introduce in detail how to convert months in PHP to English months, and give specific code examples. In PHP development, sometimes we need to convert digital months to English months, which is very practical in some date processing or data display scenarios. The implementation principles, specific code examples and precautions will be explained in detail below. 1. Implementation principle In PHP, you can convert digital months into English months by using the DateTime class and format method. Date

QQ Music allows everyone to enjoy watching movies and relieve boredom. You can use this software every day to easily satisfy your needs. A large number of high-quality songs are available for everyone to listen to. You can also download and save them. The next time you listen to them, you don’t need an Internet connection. The songs downloaded here are not in MP3 format and cannot be used on other platforms. After the membership songs expire, there is no way to listen to them again. Therefore, many friends want to convert the songs into MP3 format. Here, the editor explains You provide methods so that everyone can use them! 1. Open QQ Music on your computer, click the [Main Menu] button in the upper right corner, click [Audio Transcoding], select the [Add Song] option, and add the songs that need to be converted; 2. After adding the songs, click to select Convert to [mp3]
