5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-04-10

はじめに

venvで仮想環境の構築

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
仮想環境構築
$ python3 -m venv myapp
仮想環境の有効化
$ cd myapp
$ source bin/activate
(myapp) $ pip install pip --upgrade
(myapp) $ pip install setuptools --upgrade
機械学習関連パッケージのインストール
(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
インストール済パッケージの確認
(myapp) $ pip freeze
仮想環境の無効化
(myapp) $ deactivate
仮想環境削除
$ cd ..
$ rm -fR myapp

動作確認

numpy, matplotlib

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

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

tk.py
import tkinter as tk

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

scikit-learn

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

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

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

TensorFlow

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

$ jupyter notebook

SSH接続

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

おわりに

  • 仮想環境上だと色々試せるのでお勧めです。
5
8
0

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
5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?