金子邦彦研究室プログラミングPythonPandas データフレームの基本情報の表示,散布図、要約統計量、ヒストグラム(Python, pandas, matplotlib, seaborn, Iris データセット, titanicデータセットを使用)(Google Colaboratroy へのリンク有り)

Pandas データフレームの基本情報の表示,散布図、要約統計量、ヒストグラム(Python, pandas, matplotlib, seaborn, Iris データセット, titanicデータセットを使用)(Google Colaboratroy へのリンク有り)

Python の pandas データフレームを用いた基本情報の表示,散布図、要約統計量、ヒストグラムについて, プログラム例などで説明する.

目次

  1. 前準備
  2. Iris データセット, titanic データセットの準備
  3. 基本的な情報の表示
  4. 散布図
  5. 各属性の要約統計量(総数、平均、標準偏差、最小、四分位点、中央値、最大)
  6. ヒストグラム

Google Colaboratory のページ:

次のリンクをクリックすると,Google Colaboratoryノートブックが開く. そして,Google アカウントでログインすると,Google Colaboratory のノートブック内のコード等を編集したり再実行したりができる.編集した場合でも,他の人に影響が出たりということはない.そして,編集後のものを,各自の Google ドライブ内に保存することもできる.

https://colab.research.google.com/drive/1LfMuE3IVYKhXb57YGdsX_dmfnTvj5oKb?usp=sharing

1. 前準備

Python の準備(Windows,Ubuntu 上)

サイト内の関連ページ

関連する外部ページ

Python の公式ページ: https://www.python.org/

Python の numpy, pandas, seaborn, matplotlib, scikit-learn のインストール

2. Iris データセット, titanic データセットの準備

  1. iris, titanic データセットの読み込み
    import pandas as pd
    import seaborn as sns
    sns.set()
    iris = sns.load_dataset('iris')
    titanic = sns.load_dataset('titanic')
    

    [image]
  2. データの確認
    print(iris.head())
    print(titanic.head())
    

    [image]

3. 基本的な情報の表示

print(iris.head())
print(iris.info())
print(iris.shape)
print(iris.ndim)
print(iris.columns)

print(titanic.head())
print(titanic.info())
print(titanic.shape)
print(titanic.ndim)
print(titanic.columns)

[image]

4. 散布図

  1. 読み込んだ Iris データセットの表示
    print(iris) 
    

    [image]
  2. Iris データセットのうち、1列目と 2列目の表示

    オブジェクト iris には 0, 1, 2, 3, 4列目がある.

    print(iris.iloc[:,1]) 
    print(iris.iloc[:,2]) 
    

    [image]
  3. Iris データセットについて、1列目と 2列目の散布図

    plt.style.use('ggplot')」はグラフの書式の設定.「ro」は「赤い丸」という意味.

    %matplotlib inline
    import matplotlib.pyplot as plt
    import warnings
    warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
    plt.style.use('ggplot')
    plt.plot(iris.iloc[:,1], iris.iloc[:,2], 'ro')
    plt.show()
    

    [image]

5. 各属性の要約統計量(総数、平均、標準偏差、最小、四分位点、中央値、最大)

import seaborn as sns
sns.set()
iris = sns.load_dataset('iris')
titanic = sns.load_dataset('titanic')

print(iris.describe())
print(titanic.describe())

[image]

6. ヒストグラム

%matplotlib inline
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
plt.style.use('ggplot')

plt.hist(iris.iloc[:,1])
plt.show()

plt.hist(iris.iloc[:,2])
plt.show()

[image]

2次元ヒストグラム

%matplotlib inline
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
plt.style.use('ggplot')

plt.hist2d(iris.iloc[:,1], iris.iloc[:,2])
plt.show()

[image]