Python の pandas データフレームを用いた基本情報の表示,散布図、要約統計量、ヒストグラムについて, プログラム例などで説明する.
この資料の URL: https://www.kkaneko.jp/data/od/iris.html
目次
【サイト内の関連ページ】
このページの内容は,Google Colaboratory でも実行できる.
そのために,次の URL で,Google Colaboratory のノートブックを準備している.
次のリンクをクリックすると,Google Colaboratory のノートブックが開く. そして,Google アカウントでログインすると,Google Colaboratory のノートブック内のコードを実行することができる.Google Colaboratory のノートブックは書き換えて使うこともできる.このとき,書き換え後のものを,各自の Google ドライブ内に保存することもできる.
https://colab.research.google.com/drive/1LfMuE3IVYKhXb57YGdsX_dmfnTvj5oKb?usp=sharing
Python プログラムを動かすために, Windows では「python」, Ubuntu では「python3」などのコマンドを使う.
あるいは, 開発環境や Python コンソール(Jupyter Qt Console,Spyder,PyCharm,PyScripter など)の利用も便利である.
あるいは,オンラインで動くGoogle Colaboratory のノートブックの利用も,場合によっては便利である.
Google Colaboratory のノートブックを使うか, 自分のパソコンで Python を動かすなどがありえる.
Google Colaboratory のノートブックを新規作成を行う.
https://colab.research.google.com
Google Colab はオンラインの Python 開発環境. 使用するには Google アカウントが必要
システム Python を使うことができる(システム Python を使う場合,Python のインストールは行わない)
システム Python を用いるときは,pip, setuptools の更新は次のコマンドで行う.
sudo apt -y update sudo apt -y install python3-pip python3-setuptools
Ubuntu で,システム Python 以外の Python をインストールしたい場合は pyenv が便利である: 別ページで説明している.
Python の URL: http://www.python.org/
【Python, pip の使い方】
Python, pip は,次のコマンドで起動できる.
【Python 開発環境のインストール】
JupyterLab, spyder, nteract (Python 開発環境) のインストールは, Windows でコマンドプロンプトを管理者として実行し, 次のコマンドを実行.
python -m pip install -U pip setuptools jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder
詳しくは,: 別ページで説明している.
JupyterLab, spyder, nteract (Python 開発環境) のインストール: : 別ページで説明している.
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
python -m pip install -U pip setuptools numpy pandas matplotlib seaborn scikit-learn scikit-learn-intelex
端末で,次のコマンドを実行
sudo apt -y update sudo apt -y install python3-numpy python3-pandas python3-seaborn python3-matplotlib python3-sklearn
import pandas as pd import seaborn as sns sns.set() iris = sns.load_dataset('iris') titanic = sns.load_dataset('titanic')
print(iris.head()) print(titanic.head())
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)
print(iris)
※ オブジェクト iris には 0, 1, 2, 3, 4列目がある.
print(iris.iloc[:,1]) print(iris.iloc[:,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()
import seaborn as sns sns.set() iris = sns.load_dataset('iris') titanic = sns.load_dataset('titanic') print(iris.describe()) print(titanic.describe())
%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()
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()