MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_NextPart_01D02919.582056E0" このドキュメントは単一ファイル Web ページ (Web アーカイブ ファイル) です。お使いのブラウザー、またはエディターは Web アーカイブ ファイルをサポートしていません。Windows? Internet Explorer? など、Web アーカイブをサポートするブラウザーをダウンロードしてください。 ------=_NextPart_01D02919.582056E0 Content-Location: file:///C:/B1341C76/db6.htm Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="shift_jis"
データベース特論資料 6 (Advanced Database Exercise =
6)
2014年11月20日
概要 Abstract
今日の授業では、オブジェクト指向データベース管理システムZODB
の機能を紹介する
Tod=
ay's
class is an object-oriented database management system 'ZODB'.
■
オブジェクト指向データベース object oriented database
オブジェクト指向データベースは、プログラムで使用しているオブジェクトを=
そのまま保存するのに便利である.
Obj=
ect
datbase is a set of objects. Objects in programs can be stored into object
database.
■
Pythonの関数定義の「@」
Pyt=
hon の関数定義で「@」を使うことがある
@foo def bar()=
: print('hello') def bar()=
: print('hello') bar =3D foo(bar) 同じ意味
■
pandas
pan=
das は
Python用のデータ解析ツールである. p=
anda
is a data analysis tools for the Python.
■
Python のデータ型 Python data type
・Python
でのデータ型の確認 get data type of=
an
object
変数の値のデータ型は、=
type
を使って確認できる. We=
can
obtain the data type of an object using type<=
span
lang=3DEN-US style=3D'mso-bidi-font-size:10.5pt;font-family:"Times New Roma=
n",serif'>
・リスト
list
リストはデータの列である.次のPythonプログラムでは、100、101、102、103
というデータが並んだリストを=
vに格納している.A
list is a sequence of data. In the followin program, a sequence 100, 101, 1=
02
and 103 is stored into v as a list.
数値sで始まり、数値t未満で終わる増分aのリストは、Python
では range<=
span
lang=3DEN-US style=3D'mso-bidi-font-size:10.5pt;font-family:"Times New Roma=
n",serif'>(s, t,
a) である.
We can obtain a numeric data list from s
to t using range(s, t, a), and the step of the list a.
・辞書
dictionary
この授業では、キーと値のペアの集合のことを辞書という.キーが=
xで値が100,
キーがyで値が200である辞書を=
dに格納するプログラムは次の通り.
A
dictionary is a set of key-value pairs. The following program stores a
dictionary into d.
新しいペアの追加ができる.
We can add a new pair into a dictionary.
ある辞書内のすべてのキーの取得もできる.We
can obtain all the keys in a dictionary.
・Pandas
の Series Pandas Series
Pan=
das の
Series はリストに似ている.データ解析のた=
゚のいろいろな機能がある.=
describe
を使って要約統計量を得ることができ=
.
Pandas Series a data structure similar to list. There are many functions for
data analysis. We can obtain summary statistics using describe.
・Pandas
のデータフレーム
Pan=
das のデータフレームを用いて、次のような表データを扱いたいとする
Han=
dle a
table data (as follows) using Pandas data frame.
i=
d |
n=
ame |
p=
rice |
1=
|
a=
pple |
8=
0 |
2=
|
o=
range |
1=
50 |
この表データを =
span>=
x
に格納する Py=
thon プログラムは次の通り.
A P=
ython
program to store the table data into x using Pandas.
この =
span>=
x
に新しい列を追加することは簡単にで=
ォる.
A new column can be added as follows.
Pan=
das のデータフレームは、辞書に似ている.データフレームの各列は、列名をキー=
としたペアになっている。ペアの値は、Pandas
の Se=
ries になっている.
Pan=
das
data frame is similar to the dictionary. Each column of a Pandas data frame=
is
a pair, and the key of a pair is column name. Each value of a pair is Pandas
Series.
ある条件を満足する行だけを取り出して、新しいデータフレームを作成するに=
は
query を使う.
A n=
ew
data frame can be obtained using query. T=
he
data frame cotains the rows that satisfy a specified condition.
■<=
/span> Pandas =
での
CSV フ=
@イルの読み込み Read
a CVS file into a Pandas data frame
l=
Windows
でのファイル名「C:\=
iris.csv」は、Python
のプログラム中では「'C:=
\\iris.csv'」のように書く
l=
読み込みたいCSVファイルの先頭行に、「id,
sepal_length, sepal_width, petal_length, petal_width, species」のように属性名リストがある場合には「header=3D0」を付ける
■<=
/span> Pandas での
CSV ファイルの書き出し Write
a CVS file using a Pandas data frame
■
ZODBのデータベースファイル ZODB database file
■<=
/span> ZODB のデータベース生成 Create a new ZODB database
次=
フ=
Pythonプ=
鴻Oラムでは,データベースファイル名が
hoge6.fs であるような=
ZODB デ=
[タベースに接続している.データベースファイルが存在しないときには、自動的に=
データベースファイルが生成される.=
The
following Python program connects the ZODB database whose file name is
'hoge6.fs'. If the database file does not exist, a new database file is
generated automatically.
■<=
/span> ZODB のルートオブジェクト ZODB root object
ZOD=
B のデータベースファイル生成時に、ルートオブジェクトと呼ばれる特別なオブ=
ジェクトが1つ生成される.ルートオブジェクトは辞書(上で説明)になっていて、新しい=
ペアの追加などが簡単にできる.次のプログラムでは「=
root =3D
conn.root()」でルートオブジェクトを取得してい=
驕D
An =
object
named "root object" is created automatically when ZODB database f=
ile
generation. Root object is a dictionary (explained above). In the following
program, we get the root object at "root =3D conn=
.root()".
演習
6 (Exercises 6)
前準備
(preparation)
・pythonxy
のインストール &=
nbsp; (install
pythonxy)
・spyder
のインストール
・pandas
のインストール
■ <=
/span>ステップ1
(Step 1)
課題:以下の手順で
Spyderの設定を行いなさい <=
span
lang=3DEN-US style=3D'mso-bidi-font-size:10.5pt;font-family:"Times New Roma=
n",serif'>(Setup
the Spyder software)
1.&n=
bsp;
Spyder の起動
(execute the Spyder)
&=
nbsp; &nbs=
p; &=
nbsp; &nbs=
p;
2.&n=
bsp;
Interpreters で =
span>「Open an IPython Console<=
/b>」を選ぶ. (Select Interpreters, and Select =
gOpen
an IPython console”)
3.&n=
bsp;
「IPy=
thon
console」をクリック. (Click “IPython console”)
■ =
span>ステップ2
(Step 2)
■
CSVファイルを Pandasのデータフレームに格納する=
u> (read a CSV file and store it =
into
a Pandas data frame)
1.
まず CSV<=
/span>ファイルのファイル名を確認する
(Examine the correct CSV file name)
以下、ファイル名が =
span>C:\iris.csv =
span>であるとして説明を続ける
(In this exercise, the file name is c:\iris.csv)
2.
データフレーム x
に CS=
Vファイルを読み込む
■
Python のPandas
のデータフレームに関する練習を行い=
ネさい Exercise on Python Pandas data fra=
me.
■
ステップ5 (Step 5)
python
のパッケージをインストールする。次回以降の授業でも使う
(Install
python packages for future use)
ステップ5を行うには、インターネット接続が必=
vである. いまインターネット接続ができない人=
ヘ、ステップ5を宿題とする (Internet connection is necess=
ary.
If you can not do internetc connection, the step 5 is your homework)=
=
1. Python のパッケージをインストールしたいので、IPython(sh)
を起動 (execute the
"IPython (sh)" to install python packages)
2. =
IPython(sh)
で次のコマンドを実行 (e=
xecute
the following commands in IPython(sh)'s console)
easy_install zodb
<実行結果の例の一部分>=
※ Mac OSX ユーザ (For Mac OS X users only)
conda update conda
conda update anaconda<= o:p>
conda install pandas
conda install zodb
ステップ6は、ステップ5が必要
■ ステップ6(Step 6) ZODBに関する演習 Exercise on ZODB
1.
IPython コンソールで、ZODBデータベースに接続する (connect to a ZODB database us=
ing
IPython console)
◆
今日の演習ではZOD=
B データベースファイル名は =
span>hoge6.fs
2. =
データベースが空であることを確認 The database is empty
3. =
先ほどの =
span>=
x
をデータベースに格納 Sto=
re the
object x into the database. ◆
今日の演習ではキーとして「'ir=
is_data'
4. =
格納できたことを確認 Get
the object from the database.
5. =
さらに格納する
Store more data
6. =
格納できたことを確認 Get
the object from the database.
演習シート 6 (Exercises Sheet 6) 2014/11/20
氏名 =
鼾U 学生番号
Name =
&nb=
sp;
Department &=
nbsp; &nbs=
p;
Student number =
記入して提出しなさい Fill out and submit
Q1 ステップ1は終わったか 終わっていないか? Did you finish the step 1 ?
□ はい
Yes =
□ いいえ No
Q1b もし終わっていないなら、理由、エラ=
[メッセージなどを書きなさい.
If 'No', please explai=
n your
situation
Q2 ステップ2は終わったか 終わっていないか? Did you finish the step 2 ?
□ はい
Yes =
□ いいえ No
Q2b もし終わっていないなら、理由、エラ=
[メッセージなどを書きなさい.
If 'No', please explai=
n your
situation
Q3 ステップ3は終わったか 終わっていないか? Did you finish the step 3 ?
□ はい
Yes =
□ いいえ No
Q3b もし終わっていないなら、理由、エラ=
[メッセージなどを書きなさい.
If 'No', please explai=
n your
situation
Q4 ステップ4は終わったか 終わっていないか? Did you finish the step 4 ?
□ はい
Yes =
□ いいえ No
Q4b もし終わっていないなら、理由、エラ=
[メッセージなどを書きなさい.
If 'No', please explai=
n your
situation
Q5 ステップ5は終わったか 終わっていないか? Did you finish the step 5 ?
□ はい
Yes =
□ いいえ No
Q5b もし終わっていないなら、理由、エラ=
[メッセージなどを書きなさい.
If 'No', please explai=
n your
situation
Q6 ステップ6は終わったか 終わっていないか? Did you finish the step 6 ?
□ はい
Yes =
□ いいえ No
Q6b もし終わっていないなら、理由、エラ=
[メッセージなどを書きなさい.
If 'No', please explai=
n your
situation