v-crn Code Log

主に備忘録

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

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

Octave 5.1.0でpauseの挙動がおかしい

CouseraのStanford University Machine Learningクラス2週目の課題ex1「2.1 Plotting the Data」において、説明に従ってplotData.mを編集し、ex1を実行すると、Enterキーを押してもpauseのまま動かなくなる事案に遭遇した。

Running warmUpExercise ... 
5x5 Identity Matrix: 
ans =

Diagonal Matrix

   1   0   0   0   0
   0   1   0   0   0
   0   0   1   0   0
   0   0   0   1   0
   0   0   0   0   1

Program paused. Press enter to continue.

調べてみたところ、どうやらOctave 5.1.0ではpause関数にバグあることに原因があるようだ。

下記の記事を参考に、新たにpause関数を定義することで解決した。

octave pause; not working properly. Forces to ctrl+c :( : octave

ex1.mの適当な箇所に次の内容を追記。

function pause
  input('','s');
end

これでEnterキーを押すごとに処理が進行する。

Rubyで変数が定義されているか確認する

defined?

Rubyで変数が定義されているか確認するにはdefined?メソッドを使用する。defined?は引数として入れた変数やメソッドが定義済みであれば式の種別を表す文字列を返す。未定義であればnilを返す。

>> a = 1
>> defined? a
=> "local-variable"

>> hoge = nil
>> defined? hoge
=> "local-variable"

>> defined? poke
=> nil

>> defined? puts
=> "method"

>> defined? defined?
Traceback (most recent call last):
SyntaxError ((irb):10: syntax error, unexpected end-of-input)
defined? defined?
                 ^

参考

クラス/メソッドの定義 (Ruby 2.6.0)

ruby on rails - How to check if a variable exists with a value without "undefined local variable or method"? - Stack Overflow