MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_NextPart_01D0291F.2C3AF250" このドキュメントは単一ファイル Web ページ (Web アーカイブ ファイル) です。お使いのブラウザー、またはエディターは Web アーカイブ ファイルをサポートしていません。Windows? Internet Explorer? など、Web アーカイブをサポートするブラウザーをダウンロードしてください。 ------=_NextPart_01D0291F.2C3AF250 Content-Location: file:///C:/B1341C79/db9.htm Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="shift_jis"
データベース特論資料 9 (Advanced Database Exercise =
9)
2014年12月15日
概要 Abstract
今日の授業では、データの分類に関する演習を行う.今日も
ZODB を使う.
Tod=
ay's
class is exercise on data classification using an object-oriented database
management system 'ZODB'.
■ pandas のデータフ= 戟[ムの列名 Column name of a pandas dataframe
pandas のデータフ= 戟[ムの列には、数字や文字列などの列名がある.下記の例では,name, price の2つの列名が= る.
&n= bsp; Each column of pandas dataframe has a numeric or string column name.= In the following example, there are two column names: 'name' and 'price'.
= p>
■ <=
span
lang=3DEN-US>Python pylab
Pyth= on での散布図= フプロットの例を下に示す.pylab.scatterを使う
An example of scatter plot using P=
ython
is following (It uses pylab.scatter)
■ <=
span
lang=3DEN-US>Python
&n= bsp; Python では、区間 [s, t) の数値列(<= /span>s で始まり、tの値を超え= 驍アとが無い数値列)は、arange を用いて、次のように生成する.
A numeirc interval [s, t=
) including start<=
/span> but excluding<=
/span> stop=
span> can be generated as follows.
■ メッシュグ= 潟bド meshgrid
1 |
2 |
3 |
1 |
2 |
3 |
10 |
10 |
10 |
11 |
11 |
11 |
図. メッシュグリッドの例 A meshgrid example
&n=
bsp;
2次元のメッシュグリッドは、2つの=
2次元配列からなる.
1) x座標を格納した配列.各列には同じ値が入る<= /span>
&n= bsp; 2) y座標を格納した配列.各行には同じ値が入る.
A two-dimensional mesh grid consist= s of two two-dimensional arrays.
&n= bsp; 1) One stores x-coodinate values. Each column is the same value.
&n= bsp; 2) Another stores y-coordinate values. Each line is the same value. =
図 =
Python でのメッシュグリッド生成例 A example Python program=
to
generate a meshgrid
■ メッシュグリッドを用いたグラフ表示=
@Graph
display using mesh grid
メ=
ッシュグリッドはグラフ表示に役立つ
[
図.メッシュグリッドを用いた f(x, y)
=3Dx2 + y2 の表示 図. f(x, y)
=3Dx2 + y2 の等高線表=
ヲ
Displaying f(x, y) =3Dx2 + y2 using meshgrid Displaying contour of= f(x, y) =3Dx2 + y2
■ メッシュグリッドを数値列に変換する操作
import numpy as np
xx =3D np.arange(-1, 3, 1)= p>
yy =3D np.arange(-2, 2, 1)= p>
px, py =3D np.meshgrid(xx, yy)
print( ravel(px) )
print( ravel(py) )
演習 9 (Exercises 9)
■ <=
/span>ステップ1
(Step 1)
課題:以下の手順で、=
CSVデータファイルを準備しなさい (Prepare a CSV data file)
( If you can not use USB memoyr, o=
pen htt=
p://133.5.18.161/rinkou/addb/digits.csv usi=
ng a
出典:htt=
p://archive.ics.uci.edu/ml/datasets/Pen-Based+Recognition+of+Handwritten+Di=
gits
=3D=3D=3D
E. Alpaydin, Fevzi. Alimoglu
Department of Computer Engineering =
;
Bogazici University, 80815 Istanbul Turkey
alpaydin '@' boun.edu.tr
■ <=
/span>ステップ2
(Step 2)
課題:以下の手順で Spyderの設定を行いなさい =
(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>ステップ3
(Step 3)
課題:以下の手順で ZODB データベースに接続しなさい=
u> (Connect to a ZODB
database)
◆
今日の授業では、ZOD=
B データベースファイル名は =
span>hoge9.fs
(The
ZODB database file name in the previous class was 'hoge9.fs'.
Let us use it again here)
2. IPython コンソールで、ZODBデータベースに接続し、変数= b>root にZOD= Bのルートオ= uジェクトを格納する
(connect to a ZODB database using I=
Python
console, and store the root object into the variable named root)
■
(1) 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:\digits.csv =
span>であるとして説明を続ける
(In this exercise, the file name is c:\digits.csv)
2.
データフレーム d
に CS=
Vファイルを読み込む.
列名は 0,=
x, y,
c の4=
span>つである
(read
the CSV file into a dataframe d. There are four column names: 0, x, y and c=
).
(2)
Pandas のデータフレームをデータベースに格納 <=
/span>
(Store the Pandas data frame into the ZODB database.)
◆ 今日の演習ではキーとして「'digits'」=
設定 =
The key is=
a
character string 'digits'
1. 格納する Store
=
2. 格納できたことを確認 <=
/span>=
Get the
object from the database.
=
■
&n= bsp; データベース内のデータからの散布図の作成の練習 Exercises on displaying a scatter plot using database
1. データフレームの x 列と y =
列=
u>を、新し=
いデータフレーム X に格納する.データフレームの c 列を新しい=
変数 Y に格納する(データフレームから
&n= bsp; Store the column x and d into a new dataframe X, and store the colum= n c into a new dataframe Y.
2. 散布図の作成 sc= atterplot
= p>
3.散布図の作成(色と番号付= ォでプロット) scatterplot wi= th color and number
= p>
■ <=
/span>ステップ6
(Step 6)
分類する.この授業では Linear SVM を用いる.Linear
SVM の=
レ細はこの授業では触れない。
Do classification using SVM
1.
分=
゙する。分類機は変数 classifier に格納される. An classifier is stored into variable n=
amed
'classifier'
2.
分=
゙機を使ってみる.Use the classifier
3. 分類機のプロット (plot the
classifier)
m =3D 0.2
xmin, xmax =
=3D
X.loc[:, 'x'].min() - m, X.loc[:, 'x'].max() + m
ymin, ymax =
=3D
X.loc[:, 'y'].min() - m, X.loc[:, 'y'].max() + m
h =3D 0.01
xx, yy =3D
np.meshgrid(np.arange(xmin, xmax, h), np.arange(ymin, ymax, h))
Z =3D
classifier.predict(np.c_[xx.ravel(), yy.ravel()])
Z =3D
Z.reshape(xx.shape)
import
matplotlib.pyplot as plt
plt.pcolor(xx, yy, Z)
演習シート 9 (Exercises Sh=
eet
9) 2014/12/15
氏名 専攻 = 学生番号
Name = &nb= sp; Department &= nbsp; &nbs= p; Student number =
記入して提出しなさい Fill out and submit
Q1<= span style=3D'mso-spacerun:yes'> ステップ1は終わった= ゥ 終わっていないか? Did you finish = the step 1 ?
□ はい Yes = □ いいえ No
Q2<= span style=3D'mso-spacerun:yes'> ステップ2は終わった= ゥ 終わっていないか? Did you finish = the step 2 ?
□ はい Yes = □ いいえ No
Q3<= span style=3D'mso-spacerun:yes'> ステップ3は終わった= ゥ 終わっていないか? Did you finish = the step 3 ?
□ はい Yes = □ いいえ No
Q4<= span style=3D'mso-spacerun:yes'> ステップ4は終わった= ゥ 終わっていないか? Did you finish = the step 4 ?
□ はい Yes = □ いいえ No
Q5<= span style=3D'mso-spacerun:yes'> ステップ5は終わった= ゥ 終わっていないか? Did you finish = the step 5 ?
□ はい Yes = □ いいえ No
Q6<= span style=3D'mso-spacerun:yes'> ステップ6は終わった= ゥ 終わっていないか? Did you finish = the step 5 ?
□ はい Yes = □ いいえ No