Pythonの仮想環境とパッケージ on Ubuntu

# はじめに

- 詳しく知りたい人は、[公式のチュートリアル](https://docs.python.org/ja/3/tutorial/venv.html)へどうぞ。
 - [Pythonの仮想環境とパッケージ on Windows](https://qiita.com/sireline/items/597e70099b9273587e64)

# venvで仮想環境の構築

```sh:Python環境インストール
$ sudo apt update && upgrade
$ sudo apt install python3-pip
$ sudo python3 -m pip install pip -U
$ sudo apt install python3-venv
$ sudo apt install python3-tk
```

```sh:仮想環境構築
$ python3 -m venv myapp
```

```sh:仮想環境の有効化
$ cd myapp
$ source bin/activate
(myapp) $ pip install pip --upgrade
(myapp) $ pip install setuptools --upgrade
```

```sh:機械学習関連パッケージのインストール
(myapp) $ pip install numpy
(myapp) $ pip install pandas
(myapp) $ pip install matplotlib
(myapp) $ pip install pillow
(myapp) $ pip install IPython
(myapp) $ pip install tensorflow
(myapp) $ pip install scikit-learn
(myapp) $ pip install scipy
(myapp) $ pip install jupyter
```

```sh:インストール済パッケージの確認
(myapp) $ pip freeze
```

```sh:仮想環境の無効化
(myapp) $ deactivate
```

```sh:仮想環境削除
$ cd ..
$ rm -fR myapp
```

# 動作確認

## [numpy](https://numpy.org/), [matplotlib](https://matplotlib.org/)

```py:plt.py
import numpy as np
import matplotlib.pyplot as plt

def relu(x):
    return np.maximum(0, x)

x = np.arange(-5.0, 5.0, 0.1)
y = relu(x)
plt.plot(x, y)
plt.ylim(-0.5, 5.5)
plt.show()
```

## [pandas](https://pandas.pydata.org/)

```py:pd.py
import pandas as pd

df = pd.DataFrame({
    "Name": ["Braund, Mr. Owen Harris",
        "Allen, Mr. William Henry",
        "Bonnell, Miss. Elizabeth"],
    "Age": [22, 35, 58],
    "Sex": ["male", "male", "female"]}
)

print(df)
```

## [tkinter](https://qiita.com/sireline/items/07dc06683e43ff120915)

```py:tk.py
import tkinter as tk

root = tk.Tk()
root.title('Hello World!')
root.geometry('400x200')
root.mainloop()
```

## [scikit-learn](https://scikit-learn.org/stable/tutorial/basic/tutorial.html)

```py:sl.py
from sklearn import svm
from sklearn import datasets

digits = datasets.load_digits()
clf = svm.SVC(gamma=0.001, C=100)
clf.fit(digits.data[:-1], digits.target[:-1])
ans = clf.predict(digits.data[-1:])

print(f'Target Number : {digits.target[-1]}')
print(f'Predict Number: {ans[0]}')
```

## [SciPy](https://www.scipy.org/)

```py:sp.py
from scipy import misc
import matplotlib.pyplot as plt

face = misc.face()
plt.imshow(face)
plt.show()
```

## [TensorFlow](https://www.tensorflow.org/)

```py:tf.py
from __future__ import absolute_import, division, print_function, unicode_literals

# TensorFlow をインストール

import tensorflow as tf

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)

model.evaluate(x_test,  y_test, verbose=2)
```

## jupyter

```txt
$ jupyter notebook
```

# SSH接続

```sh:sshサーバのインストール
$ sudo apt install openssh-server
```

# おわりに

- 仮想環境上だと色々試せるのでお勧めです。