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

Machine LearningServer Side ProgrammingProgramming

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 −


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


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


  • 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.

Published on 18-Jan-2021 12:15:46