如何使用 scikit-learn 包将特定大小的数组转换为不同的大小?


Scikit−learn,通常称为 sklearn,是 Python 中一个用于实现机器学习算法的库。它是一个开源库,因此可以免费使用。它功能强大且健壮,因为它提供了各种工具来执行统计建模。这包括分类、回归、聚类、降维等等,借助于 Python 中强大且稳定的接口。该库构建在 Numpy、SciPy 和 Matplotlib 库之上。

可以使用以下所示的“pip”命令安装它:

pip install scikit−learn

此库侧重于数据建模。可以使用 scikit−learn 包将不同大小的数组转换为完全不同大小的数组。

以下是一个示例:

示例

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))

输出

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

解释

  • 导入所需的包,并为方便使用赋予它们别名。

  • 使用 NumPy 库生成数据点“x”和“y”的值。

  • 生成数据的详细信息显示在控制台上。

  • 调用“PolynomialFeatures”函数。

  • 此函数调用被分配给一个变量。

  • 此变量适合模型。

  • 在控制台上显示适合模型的数据。

更新于: 2021年1月18日

146 次查看

启动您的 职业生涯

通过完成课程获得认证

开始
广告

© . All rights reserved.