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')
print(iris.head())
配列(アレイ)の形: サイズは 150 ×5.次元数は 2. 最後の列は,iris.target は花の種類のデータである
print(iris.shape) print(iris.ndim)
print( iris.iloc[:,0:4] )
import numpy as np
import sklearn.decomposition
%matplotlib inline
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore') # Suppress Matplotlib warnings
# M の最初の2列を,b で色を付けてプロット.b はラベル
def scatter_label_plot(M, b, alpha):
a12 = pd.DataFrame( M[:,0:2], columns=['a1', 'a2'] )
f = pd.factorize(b)
a12['target'] = f[0]
g = sns.scatterplot(x='a1', y='a2', hue='target', data=a12, palette=sns.color_palette("hls", np.max(f[0]) + 1), legend="full", alpha=alpha)
# lenend を書き換え
labels=f[1]
for i, label in enumerate(labels):
g.legend_.get_texts()[i].set_text(label)
plt.show()
from sklearn.manifold import TSNE d = TSNE(n_components = 2).fit_transform(iris.iloc[:,0:4]) print(d)
scatter_label_plot(d, iris.iloc[:,4], 1)
from sklearn.manifold import Isomap d = Isomap(n_components=2, n_neighbors=10).fit_transform(iris.iloc[:,0:4]) print(d) scatter_label_plot(d, iris.iloc[:,4], 1)
from sklearn.manifold import SpectralEmbedding d = SpectralEmbedding(n_components=2, n_neighbors=10).fit_transform(iris.iloc[:,0:4]) print(d) scatter_label_plot(d, iris.iloc[:,4], 1)
scikit-learn の cheet sheet によれば、isomap, Spectral Embedding が働かないときは Locally Linear Embedding (LLE) が候補になっている
from sklearn.manifold import LocallyLinearEmbedding d = LocallyLinearEmbedding(n_components=2, n_neighbors=10).fit_transform(iris.iloc[:,0:4]) print(d) scatter_label_plot(d, iris.iloc[:,4], 1)
scikit-learn の cheet sheet によれば、 データ数が10000以上のときは kernel approximation が候補になっている.
from sklearn.kernel_approximation import RBFSampler d = RBFSampler(gamma=1).fit_transform(iris.iloc[:,0:4]) print(d) scatter_label_plot(d, iris.iloc[:,4], 1)