LoginSignup
2
3

More than 5 years have passed since last update.

サンプルデータとしてIrisデータを読み込む

Last updated at Posted at 2018-01-06

可視化などのためにサンプルデータが欲しくなることはありますか?
私はよくあります。

で、そんなときはこんな感じでやったらいいかな?と思った次第です。

import numpy as np
import pandas as pd
def load_iris_data():
    from sklearn.datasets import load_iris
    iris = load_iris()
    df = pd.DataFrame(np.array(iris.data))
    df.columns = map(lambda x: x.split(" (")[0].replace(" ", "_"), iris.feature_names)
    target = pd.DataFrame(np.array(iris.target),columns=["target"])
    return pd.concat([df, target], axis=1)

こんなかんじでつかう

df = load_iris_data()
pd.plotting.scatter_matrix(df.drop("target", axis=1), s=80, c=df.target, edgecolor="Black",
                           figsize=(15, 15), diagonal="kde")

こんな感じのグラフになりました。

graph.png

以上、超小ネタでした。
なお、pythonやpandasの中級者以上ならもっと綺麗にできるのかもしれません。

2
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3