如何使用 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 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告