How can scikit-learn library be used to load data in Python?


Scikit-learn, commonly known as sklearn is an open-source library in Python that is used for the purpose of implementing machine learning algorithms.

This includes classification, regression, clustering, dimensionality reduction, and much more with the help of a powerful, and stable interface in Python. This library is built on Numpy, SciPy and Matplotlib libraries.

Let us see an example to load data −

Example

from sklearn.datasets import load_iris
my_data = load_iris()
X = my_data.data
y = my_data.target
feature_name = my_data.feature_names
target_name = my_data.target_names
print("Feature names are : ", feature_name)
print("Target names are : ", target_name)
print("\nFirst 8 rows of the dataset are : \n", X[:8])

Output

Feature names are : ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
Target names are : ['setosa' 'versicolor' 'virginica']
First 8 rows of the dataset are :
[[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]
[5. 3.6 1.4 0.2]
[5.4 3.9 1.7 0.4]
[4.6 3.4 1.4 0.3]
[5. 3.4 1.5 0.2]]

Explanation

  • The required packages are imported.
  • The dataset required for this is also loaded into the environment.
  • The features and the target values are separated from the dataset.
  • These features and target are printed on the console.
  • Also, to see a sample of the data, the first 8 rows of the data is printed on the console.

Updated on: 11-Dec-2020

300 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements