How can scikit-learn package be used to transform an array of specific size to a different size?


Scikit−learn, commonly known as sklearn is a library in Python that is used for the purpose of implementing machine learning algorithms. It is an open-source library hence it can be used free of cost. It is powerful and robust, since it provides a wide variety of tools to perform statistical modelling. This includes classification, regression, clustering, dimensionality reduction, and much more with the help of a powerful, and stable interface in Python. The library is built on Numpy, SciPy and Matplotlib libraries.

It can be installed using the ‘pip’ command as shown below −

pip install scikit−learn

This library focuses on data modelling. An array of different size can be transformed to an array of an entirely different size, using scikit−learn package.

Following is an example −

Example

from sklearn.preprocessing import PolynomialFeatures
import numpy as np
Y = np.arange(12)
print("The original dimensions of the ndarray")
print(Y.shape)
print("The changed dimensions of the ndarray")
x = Y.reshape(3, 4)
print(x.shape)
poly = PolynomialFeatures(degree=2)
print(poly.fit_transform(x))

Output

The original dimensions of the ndarray
(12,)
The changed dimensions of the ndarray
(3, 4)
[[ 1. 0. 1. 2. 3. 0. 0. 0. 0. 1. 2. 3. 4. 6.
9.]
[ 1. 4. 5. 6. 7. 16. 20. 24. 28. 25. 30. 35. 36. 42.
49.]
[ 1. 8. 9. 10. 11. 64. 72. 80. 88. 81. 90. 99. 100. 110.
121.]]

Explanation

  • The required packages are imported, and they are given alias names for ease of use.

  • The values for data points ‘x’ and ‘y’ are generated using NumPy library.

  • The details of the data generated is displayed on the console.

  • The ‘PolynomialFeatures’ function is called.

  • This function call is assigned to a variable.

  • This variable is fit to the model.

  • The data fit to the model is displayed on the console.

Updated on: 18-Jan-2021

125 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements