如何在 Python 中使用 scikit-learn 庫載入資料?
Scikit-learn,通常稱為 sklearn,是 Python 中一個開源庫,用於實現機器學習演算法。
這包括分類、迴歸、聚類、降維等等,藉助於 Python 中強大且穩定的介面。該庫建立在 Numpy、SciPy 和 Matplotlib 庫之上。
讓我們看一個載入資料的示例:
示例
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])
輸出
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]]
解釋
- 匯入所需的包。
- 將此所需的資料集載入到環境中。
- 從資料集中分離特徵和目標值。
- 在控制檯上列印這些特徵和目標。
- 此外,要檢視資料的樣本,會在控制檯上列印資料的前 8 行。
廣告