v-crn Code Log

主に備忘録

Google Colaboratory でカスタムモジュールをインポートする

手順

1. Google Drive を Mount

from google.colab import drive
drive.mount('/content/drive')

2. sys.path.append() でパスを通してインポート

例として次のようなファイル構成になっているとする。

  • My Drive
    • Colab Notebooks
      • ML
        • workspace
          • my_module
            • __init__.py
            • my_utils.py
            • my_toolbox
              • __init__.py
              • visualizer.py

※「My Drive」が Google Drive のルートディレクト

このファイル構成で カスタムモジュール「my_module」を import するには次のように sys.path.append()モジュールの親ディレクトまでのパスをシステムに伝える必要がある。

import sys
ROOT_PATH = 'drive/My Drive/Colab Notebooks/ML/workspace/'
sys.path.append(ROOT_PATH)

import my_module

これで ModuleNotFoundError: No module named 'my_module' のようなエラーが出なければおけまる。

モジュール my_module の中のライブラリ my_utils やさらにその中のメンバーを使いたければたとえば次のように書く。

from my_module import my_utils
from my_toolbox.visualizer import hoge

visualizer.py

def hoge():
    print('hoge!')

モジュールの変更を反映する

Google Drive を再度マウントする必要がある。 Google Colaboratory のメニュータブから「Factory reset runtime」を選択し、ランタイムをリセットする。 そしていつものようにマウントするためのコードを実行すれば良い。

from google.colab import drive
drive.mount('/content/drive')

認証コードを入力してマウントが完了すれば、変更が反映されているはずだ。

ここで注意したいのは、単なる再起動 (Restart) では drive.mount('/content/drive') を実行しても Google Drive が再読み込みされないことである。その場合、認証コードを求められることなく以前の状態のドライブが適用されてしまう。

その他、強制マウントや importlib.reload を試してみたが駄目だった。

駄目な例1:強制マウント

drive.mount('/content/drive', force_remount=True)

駄目な例2:importlib.reload

import importlib
importlib.reload(module_name)

参考

TensorFlow 2.0.0へのアップデートで出くわしたエラー:AttributeError: module 'tensorflow' has no attribute 'placeholder'

前提

  • TensorFlow 2.0.0

エラー内容

TensorFlow 1.4.0で書かれたコード

g = tf.Graph()
with g.as_default():
    x = tf.placeholder(dtype=tf.float32,
                       shape=(None), name='x')
    w = tf.Variable(2.0, name='weight')
    b = tf.Variable(0.7, name='bias')

    z = w*x + b
    init = tf.global_variables_initializer()

を実行すると次のエラーが出た.

AttributeError: module 'tensorflow' has no attribute 'placeholder'

原因はバージョンアップに伴う変更の影響だった.

解決策

エラーの原因となっているメソッドの前に tensorflow.compat.v1 をつけることで解決した.(もちろん as tf でimportしているならtf.compat.v1

g = tf.Graph()
with g.as_default():
    x = tf.compat.v1.placeholder(dtype=tf.float32,
                       shape=(None), name='x')
    w = tf.Variable(2.0, name='weight')
    b = tf.Variable(0.7, name='bias')

    z = w*x + b
    init = tf.compat.v1.global_variables_initializer()

VS CodeのPythonエクステンションを使ったJupterNotebookは通常のJupyterNotebookより遅い

要点

VS Code拡張機能Python』を導入すると、VS Code上で通常のJupyterNotebookと同様にIPythonファイルを編集し、実行することができるようになります。

しかし、いざそれを使ってみると普段より明らかに処理に時間がかかっているように思えたので、今回それを検証してみました。

結論としては、VS Code上で実行した場合の方が圧倒的に遅いということがわかりました。

実行環境

  • macOS Mojave 10.14.6
  • Python 3.7.5
  • pipenv 2018.11.26
  • Jupyter
    • jupyter 1.0.0
    • jupyter-client 5.3.4
    • jupyter-console 6.0.0
    • jupyter-contrib-core 0.3.3
    • jupyter-contrib-nbextensions 0.5.1
    • jupyter-core 4.6.1
    • jupyter-highlight-selected-word 0.2.0
    • jupyter-latex-envs 1.4.6
    • jupyter-nbextensions-configurator 0.4.1
    • jupytext 1.3.0

検証コード

%%
import pandas as pd
import numpy as np
from pandas import DataFrame
from sklearn.datasets import load_boston
import matplotlib.pyplot as plt
import seaborn as sns

boston = load_boston()
df = DataFrame(X, columns = boston.feature_names)
df['PRICE'] = boston.target

%%

%%time
plt.figure(facecolor='w')
pd.plotting.scatter_matrix(df, figsize=(20,20))
filename = 'scatter-matrix.png'
plt.savefig(filename, dpi=100, bbox_inches="tight", pad_inches=0.1)
plt.show()

結果

VS Code上で実行した場合の方が3倍ほど時間が掛かる結果が得られました。

Case1: JupyterNotebook

CPU times: user 20.8 s, sys: 337 ms, total: 21.1 s Wall time: 19.9 s

Case2: JupyterNotebook with Python extention of VS Code

CPU times: user 56.2 s, sys: 1.12 s, total: 57.3 s Wall time: 1min 5s

IPython上でGIFを表示する

前提

ルートディレクトリに表示したいGIFファイルがある場合を想定します。

!ls
sample_data  sin_curve.gif

ちなみに上の「sin_curve.gif」を生成するコードはこんな感じ。

%%
!pip install animatplot

%%
"""
Introduction to animatplot
https://animatplot.readthedocs.io/en/latest/tutorial/getting_started..html#Basic-Animation
"""

import numpy as np
from matplotlib import pyplot as plt
import animatplot as amp


def main():
    xs = np.linspace(0, 1, 50)
    ts = np.linspace(0, 1, 20)
    Xs = np.asarray([xs] * len(ts))
    Ys = [np.sin(2 * np.pi * (xs + t)) for t in ts]
    """
    # you can also write
    Xs, Ts = np.meshgrid(xs, ts)
    Ys = np.sin(2 * np.pi * (Xs + Ts))
    """
    block = amp.blocks.Line(Xs, Ys)
    anim = amp.Animation([block])
    anim.save_gif("sin_curve")
    plt.show()


if __name__ == '__main__':
    main()

GIFを表示するコード

以下のコードでGIFを表示させることができる。

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
from IPython import display
from pathlib import Path

gifPath = Path("sin_curve.gif")

with open(gifPath,'rb') as f:
  display.Image(data=f.read(), format='png')

Path("")の引数に対象のGIFファイルのPathを入れてれればおけまる。

参考

elegant way to display gif file in notebook? · Issue #10045 · ipython/ipython

Google Colaboratoryのスクラッチコードセル

Google Colaboratoryにおいて!ls -adf -hなどのちょっとした確認のためのコマンドを実行したいとき、クラッチコードセルが便利。

以下のショートカットキーでスクラッチコードセルが用意される。

Mac

Cmd + alt + N

Windows

Ctrl + alt + N

ValueError: Integers to negative integer powers are not allowed.[Python][Numpy]

エラー内容

以下のように、負の数を指数とした累乗の計算でエラーが出た。

for num in np.arange(-5, 5):
  print(10**num)

""" ValueError: Integers to negative integer powers are not allowed.
"""

「負の整数による整数のべき乗は許されない」とのこと。

解決策

Python標準のrangeメソッドを使う。

for num in range(-5, 5):
  print(10**num)

numpyでは負の整数による整数のべき乗はできない。

逆に言えば、整数でなければnumpyでも期待通りの動作をする:

for num in np.arange(-5, 5):
  print(10.**num)

numpyは整数型の癖の強さに定評がある。

Python 3.x - python エラー|teratail

SQL何もわからんな人へ捧げる『SQL Bolt』

SQLBolt - Learn SQL - Introduction to SQL

SQLをWeb上で学べる教材。記述したクエリの結果がリアルタイムに出力される。

以下、コードサンプルといくつかの演習問題に対する解答例。簡単すぎる問題は省略。

SQL Lesson 3: Queries with constraints (Pt. 2)

-- 1. Find all the Toy Story movies
SELECT * FROM movies
where title
  like '%Toy Story%'
;

-- 3. Find all the movies (and director) not directed by John Lasseter
SELECT * FROM movies
where director
    not like '%John Lasseter%'
;

SQL Review: Simple SELECT Queries

SELECT * FROM north_american_cities
where Country like 'United States'
order by latitude ASC;

SQL Lesson 7: OUTER JOINs

-- Find the list of all buildings that have employees
SELECT distinct e.building
FROM employees e
join Buildings b
on b.building_name = e.building
;

-- List all buildings and the distinct employee roles in each building (including empty buildings)
SELECT distinct
    b.building_name,
    e.role
FROM Buildings b
left outer join employees e
on b.building_name = e.building
;

SQL Lesson 8: A short note on NULLs

SELECT column, another_column, …
FROM mytable
WHERE column IS/IS NOT NULL
AND/OR another_condition
AND/OR …;

Exercise

-- Find the name and role of all employees who have not been assigned to a building
SELECT * FROM employees
where building is null
;

-- Find the names of the buildings that hold no employees
SELECT * 
FROM buildings b
    left outer join employees e
    on e.building = b.building_name
where
    e.building is null
;

SQL Lesson 9: Queries with expressions

-- Example query with expressions
SELECT particle_speed / 2.0 AS half_particle_speed
FROM physics_data
WHERE ABS(particle_position) * 10.0 > 500;

-- Select query with expression aliases
SELECT col_expression AS expr_description, …
FROM mytable;

-- Example query with both column and table name aliases
SELECT column AS better_column_name, …
FROM a_long_widgets_table_name AS mywidgets
INNER JOIN widget_sales
  ON mywidgets.id = widget_sales.widget_id;

Exercise

-- List all movies and their combined sales in millions of dollars
SELECT
    m.title,
    (b.domestic_sales + b.international_sales) / 1000000
FROM movies m
left outer join Boxoffice b
on b.movie_id = m.id
;

-- List all movies that were released on even number years
SELECT
    m.title
FROM movies m
left outer join Boxoffice b
on b.movie_id = m.id
where m.year % 2 = 0
;

SQL Lesson 10: Queries with aggregates (Pt. 1)

-- Select query with aggregate functions over all rows
SELECT AGG_FUNC(column_or_expression) AS aggregate_description, …
FROM mytable
WHERE constraint_expression;

Exercise

-- Find the longest time that an employee has been at the studio
SELECT max(years_employed)
FROM employees
;

-- For each role, find the average number of years employed by employees in that role
SELECT role, avg(years_employed)
FROM employees
group by role
;

-- Find the total number of employee years worked in each building
SELECT building, sum(years_employed)
FROM employees
group by building
;

SQL Lesson 11: Queries with aggregates (Pt. 2)

-- Select query with HAVING constraint
SELECT group_by_column, AGG_FUNC(column_expression) AS aggregate_result_alias, …
FROM mytable
WHERE condition
GROUP BY column
HAVING group_condition;

Did you know? If you aren't using the GROUP BY clause, a simple WHERE clause will suffice.

Exercise

-- Find the number of Artists in the studio (without a HAVING clause)
SELECT count(*)
FROM employees
where role = 'Artist'
;

-- Find the number of Employees of each role in the studio
SELECT role,count(*)
FROM employees
group by role
;

-- Find the total number of years employed by all Engineers
SELECT role,sum(Years_employed)
FROM employees
group by role
having role = 'Engineer'
;

SQL Lesson 12: Order of execution of a Query

-- Complete SELECT query
SELECT DISTINCT column, AGG_FUNC(column_or_expression), …
FROM mytable
    JOIN another_table
      ON mytable.column = another_table.column
    WHERE constraint_expression
    GROUP BY column
    HAVING constraint_expression
    ORDER BY column ASC/DESC
    LIMIT count OFFSET COUNT;

Exercise

-- Find the number of movies each director has directed
SELECT m.director, count(*)
FROM movies m
    left outer join boxoffice b
    on b.movie_id = m.id
group by director
order by director
;

-- Find the total domestic and international sales that can be attributed to each director
SELECT m.director, sum(domestic_sales+international_sales)
FROM movies m
    left outer join boxoffice b
    on b.movie_id = m.id
group by director
order by director
;

SQL Lesson 13: Inserting rows

-- Insert statement with values for all columns
INSERT INTO mytable
VALUES (value_or_expr, another_value_or_expr, …),
       (value_or_expr_2, another_value_or_expr_2, …),
       …;
-- Insert statement with specific columns
INSERT INTO mytable
(column, another_column, …)
VALUES (value_or_expr, another_value_or_expr, …),
      (value_or_expr_2, another_value_or_expr_2, …),
      …;

SQL Lesson 14: Updating rows

-- Update statement with values
UPDATE mytable
SET column = value_or_expr, 
    other_column = another_value_or_expr, 
    …
WHERE condition;

Exercise

-- The director for A Bug's Life is incorrect, it was actually directed by John Lasseter
update movies
set director = 'John Lasseter'
where title = "A Bug's Life"
  • 文字列の中でシングルクォーテーション「'」を使う場合は全体の括弧はダブルクォーテーション「"」を使う

SQL Lesson 15: Deleting rows

-- Delete statement with condition
DELETE FROM mytable
WHERE condition;
delete from movies
where director = 'Andrew Stanton'

SQL Lesson 16: Creating tables

-- Create table statement w/ optional table constraint and default value
CREATE TABLE IF NOT EXISTS mytable (
    column DataType TableConstraint DEFAULT default_value,
    another_column DataType TableConstraint DEFAULT default_value,
    …
);

SQL Lesson 17: Altering tables

-- Altering table to add new column(s)
ALTER TABLE mytable
ADD column DataType OptionalTableConstraint
    DEFAULT default_value;

-- Altering table to remove column(s)
ALTER TABLE mytable
DROP column_to_be_deleted;

-- Altering table name
ALTER TABLE mytable
RENAME TO new_table_name;
-- Add a column named Aspect_ratio with a FLOAT data type to store the aspect-ratio each movie was released in.
alter talbe movies
add Aspect_ratio float;

-- Add another column named Language with a TEXT data type to store the language that the movie was released in. Ensure that the default for this language is English.
alter table movies
add Language text
default 'English';

SQL Lesson 18: Dropping tables

-- Drop table statement
DROP TABLE IF EXISTS mytable;

SQL Lesson X: To infinity and beyond!

graduation

これでクリアです。お疲れさまでした!