忍者ブログ

いけいけ機械学習

統計、機械学習、AIを学んでいきたいと思います。 お役に立てば幸いです。

Python sklearn iria datasetをロードし表示する

1.概要

iris datasetをロードし、pandasを利用して表示する例です。


2.サンプル


import pandas as pd
from sklearn.datasets import load_iris

# 1. Irisデータセットをロード
iris = load_iris()

# 2. データをPandas DataFrameに変換
# 特徴量のデータ (data) と特徴量の名前 (feature_names) を使用
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
#
print("--- データセット読み込み後 ---")
print(df.head())

# 3. 目的変数 (target) の追加
#
df['species'] = iris.target
#
print("--- taeget追加後 ---")
print(df.head())

# 4. 目的変数 (target) の追加
# targetは通常数値(0, 1, 2)なので、わかりやすいようにtarget_namesを対応付けます
df['species_name'] = df['species'].apply(lambda x: iris.target_names[x])
#
print("--- taeget_name追加後 ---")
print(df.head())


3.実行結果

以下のように表示されました。


--- データセット読み込み後 ---


   sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)


0                5.1               3.5                1.4               0.2


1                4.9               3.0                1.4               0.2


2                4.7               3.2                1.3               0.2


3                4.6               3.1                1.5               0.2


4                5.0               3.6                1.4               0.2


--- taeget追加後 ---


   sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)  species


0                5.1               3.5                1.4               0.2        0


1                4.9               3.0                1.4               0.2        0


2                4.7               3.2                1.3               0.2        0


3                4.6               3.1                1.5               0.2        0


4                5.0               3.6                1.4               0.2        0


--- taeget_name追加後 ---


   sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)  species species_name


0                5.1               3.5                1.4               0.2        0       setosa


1                4.9               3.0                1.4               0.2        0       setosa


2                4.7               3.2                1.3               0.2        0       setosa


3                4.6               3.1                1.5               0.2        0       setosa


4                5.0               3.6                1.4               0.2        0       setosa




PR