kewiii

Study

Weka導入

by kewi on 12 月.10, 2009, under Memo, Study

http://www.google.co.jp/search?rlz=1C1CHMV_jaJP328JP328&sourceid=chrome&ie=UTF-8&q=WEKA

-ニュージーランドのWaikato大学で開発されたデータマイニング・ツール

ここの説明がわかりやすい
http://web.sfc.keio.ac.jp/~soh/dm03/man_w_03.html

  1. インストール
    Java VMセット版
    Java入ってない人や,versionが1.5未満の人はこれ
    Wekaのみ版
    JavaVM 1.5 以上が入っている人はこっち
  2. クラスパスを通す
    インストールフォルダにできるweka.jarにクラスパスを通す
    環境変数CLASSPATHにパスを追加
  3. チェック
    C:\Program Files\Weka-3-6\data>java weka.classifiers.trees.J48 -t iris.arff
    
    J48 pruned tree
    ------------------
    
    petalwidth <= 0.6: Iris-setosa (50.0)
    petalwidth > 0.6
    |   petalwidth <= 1.7
    |   |   petallength <= 4.9: Iris-versicolor (48.0/1.0)
    |   |   petallength > 4.9
    |   |   |   petalwidth <= 1.5: Iris-virginica (3.0)
    |   |   |   petalwidth > 1.5: Iris-versicolor (3.0/1.0)
    |   petalwidth > 1.7: Iris-virginica (46.0/1.0)
    
    Number of Leaves  :     5
    
    Size of the tree :      9
    
    Time taken to build model: 0.04 seconds
    Time taken to test model on training data: 0.03 seconds
    
    === Error on training data ===
    
    Correctly Classified Instances         147               98      %
    Incorrectly Classified Instances         3                2      %
    Kappa statistic                          0.97
    Mean absolute error                      0.0233
    Root mean squared error                  0.108
    Relative absolute error                  5.2482 %
    Root relative squared error             22.9089 %
    Total Number of Instances              150
    
    === Confusion Matrix ===
    
      a  b  c   <-- classified as
     50  0  0 |  a = Iris-setosa
      0 49  1 |  b = Iris-versicolor
      0  2 48 |  c = Iris-virginica
    
    === Stratified cross-validation ===
    
    Correctly Classified Instances         144               96      %
    Incorrectly Classified Instances         6                4      %
    Kappa statistic                          0.94
    Mean absolute error                      0.035
    Root mean squared error                  0.1586
    Relative absolute error                  7.8705 %
    Root relative squared error             33.6353 %
    Total Number of Instances              150
    
    === Confusion Matrix ===
    
      a  b  c   <-- classified as
     49  1  0 |  a = Iris-setosa
      0 47  3 |  b = Iris-versicolor
      0  2 48 |  c = Iris-virginica

    となればOK
    バージョンによってjarファイルの構成がかわるので注意が必要

  4. 実行オプション
    • -h or -help
      ヘルプを表示
    • -t
      教師データを読み込む
    • -T
      テストデータを読み込む
    • -c
      クラスに用いるデータの列番号を入力(デフォルトはlast)
    • -x
      交差検定を行う場合のfold数を入力(デフォルトは10)
    • -no-cv
      交差検定の結果を表示しない
    • あきたのであとで とりあえず -no-cv -vつければ木だけ表示

    • -split-percentage Sets the percentage for the train/test set split, e.g., 66.
    • -preserve-order
      Preserves the order in the percentage split.
    • -s
      Sets random number seed for cross-validation or percentage split
      (default: 1).
    • -m
      Sets file with cost matrix.
    • -l
      Sets model input file. In case the filename ends with ‘.xml’,
      a PMML file is loaded or, if that fails, options are loaded
      from the XML file.
      -d
      Sets model output file. In case the filename ends with ‘.xml’,
      only the options are saved to the XML file, not the model.
      -v
      Outputs no statistics for training data.
      -o
      Outputs statistics only, not the classifier.
      -i
      Outputs detailed information-retrieval statistics for each class.
      -k
      Outputs information-theoretic statistics.
      -p
      Only outputs predictions for test instances (or the train
      instances if no test instances provided and -no-cv is used),
      along with attributes (0 for none).
      -distribution
      Outputs the distribution instead of only the prediction
      in conjunction with the ‘-p’ option (only nominal classes).
      -r
      Only outputs cumulative margin distribution.
      -z
      Only outputs the source representation of the classifier,
      giving it the supplied name.
      -g
      Only outputs the graph representation of the classifier.
      -xml filename | xml-string
      Retrieves the options from the XML-data instead of the command line
      -threshold-file
      The file to save the threshold data to.
      The format is determined by the extensions, e.g., ‘.arff’ for ARFF
      format or ‘.csv’ for CSV.
      -threshold-label

      Options specific to weka.classifiers.trees.J48:

      -U
      Use unpruned tree.
      -C Set confidence threshold for pruning.
      (default 0.25)
      -M
      Set minimum number of instances per leaf.
      (default 2)
      -R
      Use reduced error pruning.
      -N
      Set number of folds for reduced error
      pruning. One fold is used as pruning set.
      (default 3)
      -B
      Use binary splits only.
      -S
      Don’t perform subtree raising.
      -L
      Do not clean up after the tree has been built.
      -A
      Laplace smoothing for predicted probabilities.
      -Q
      Seed for random data shuffling (default 1).

    • -t
2 Comments more...

DSL Toolsで作った言語の配布方法

by kewi on 12 月.03, 2009, under Memo, Other, Study

インストーラの作成

パッケージの読み込み

パッケージの読み込み

これでいいんじゃないの?
ビルドしてできたmsiファイルを使ってインストールしても新しいモデルが追加されない…わからん。
2008はDSL Tools Redistributable用のmsiはデザイナインストール用のmsiに含まれてるって話だから、setup.exeとかiniは出力されなくていいんだよね?

→よかった。インストールしたディレクトリが変なとこになってるだけだった。

Leave a Comment more...

Pythonのインスタンス変数

by kewi on 11 月.22, 2009, under Python

うーん、よくわかりません。


from Tkinter import *

#Raw Data and Feature Data
class SensorData :

#Sensor Data
DataList = []
DataMaxSize = 0
DataDefault = 450

#Widget
Data = ""
GraphWidth = 100
GraphHeight = 50

#print Data
def printData(self) :

print self.DataList[len(self.DataList)-1]
self.Data.set(str(self.DataList[len(self.DataList)-1]))

#draw Graph (for GUI)
def drawDataGraph(self, _Color) :

graph = Canvas(self.Frame, width=self.GraphWidth, height=self.GraphHeight)
graph.create_line([(i*2, (self.DataList[i]-450)/4+25) for i in range(0, len(self.DataList))], fill=_Color, width=1.5)
graph.grid(row=self.DataLabelRow, column=2)

#append DataList
def appendData(self, _Data):

self.DataList.append(_Data)
if len(self.DataList) > self.DataMaxSize :
self.DataList.pop(0)
print self.DataList

#pack Widget (for GUI)
def packWidget(self, _Frame, _Row, _Font) :

self.Frame = _Frame
self.DataLabelRow = _Row
self.Data = StringVar()
Label(_Frame, text=self.DataName+" :").grid(row=_Row, column=0, padx=self.Padx, pady=self.Pady)
self.DataLabel = Label(_Frame, textvariable=self.Data, font=_Font, width = 10)
self.DataLabel.grid(row=_Row, column=1, padx=5, pady=5)
self.Data.set(str(0))

def __init__ (self, _Name, _Length, _Position, _MaxSize) :

self.DataName = _Name
self.DataLength = _Length
self.DataPosition = _Position
self.DataMaxSize = _MaxSize

class DataCalcurator :

#Data
RawData = []
RawDataName =[]
DataMaxSize = 0

#Widget
RawDataFrame = ""
GraphColor = ["red","green","blue","yellow","orange","gray"]

#print All Raw Data
def printRawData(self):

for i in range(0,len(self.RawData)) :
self.RawData[i].printData()

#Draw Raw DataGraph
def drawRawDataGraph(self) :

for i in range(0, len(self.RawData)) :
self.RawData[i].drawDataGraph(self.GraphColor[i%len(self.GraphColor)])

#Set All Raw Data
def setRawData(self, _Data) :

for i in range(0, len(self.RawData)) :
self.RawData[i].setData(_Data)

#pack Widget
def packWidget(self, _Frame, _Font) :

#Raw Data
self.RawDataFrame = LabelFrame(_Frame, text="Raw Data")
self.RawDataFrame.grid(row=0, column=0, columnspan=1, padx=5, pady=5, sticky=W + E + N + S)

for i in range(0, len(self.RawData)) :
self.RawData[i].packWidget(self.RawDataFrame, i, _Font)

def __init__(self, _RawDataName, _SerialDataList, _DataMaxSize) :

self.RawDataName = _RawDataName
self.DataMaxSize = _DataMaxSize

for i in range(0, len(_RawDataName)) :
AppendData = SensorData(_RawDataName[i], 2, (11+(i*2)), _DataMaxSize)
self.RawData.append(AppendData)

class Main :

#Frames
SensorData = ""
SerialConnector = ""

def packWidget(self, _Title) :

##### MAIN WINDOW #####
root = Tk()
root.title(_Title)

##### sensordata frame #####
sensordataFrame = LabelFrame(root, text="Data")
sensordataFrame.grid(row=0, column=0, padx=5, pady=5, sticky=N + W)
self.SensorData.packWidget(sensordataFrame, "courier 12")

return root

def setData(self) :

while True :

if len(self.SerialDatalist) :
self.SensorData.setRawData(self.SerialDatalist.pop(0))
if GUI_APPLICATION_FLAG :
self.SensorData.printRawData()
self.SensorData.drawRawDataGraph()
else :
time.sleep(0.1) #tmp

def __init__(self) :

#Sensor Data
self.SensorData = DataCalcurator(RAW_DATA_NAME, FEATURE_DATA_NAME, SERIAL_DATA_LIST, DATA_MAXSIZE, SERIAL_DATA_OFFSET)

####################
######  MAIN  ######
####################
def main() :

#GUI APP Flag
global GUI_APPLICATION_FLAG
GUI_APPLICATION_FLAG = True
X = Main()
root = X.packWidget("テスト")
root.mainloop();

if __name__ == '__main__':
main()

色々抜けてるけどこんな感じで、

class SensorData

のインスタンスをいっぱいつくって、それぞれの

DataList = []

にデータを追加していきたい。そして、

printData

drawDataGraph

でデータとグラフを表示させたい。
でも、これだと作ったインスタンスがすべて同じ

DataList

のアドレスを参照してしまうみたいで、全部同じ値になってしまう。
調べてたら、

http://d.hatena.ne.jp/smeghead/20080414/ooppy

をみつけたので、DataListをそれぞれ__init__で再定義すればよいのかと思って


from Tkinter import *

#Raw Data and Feature Data
class SensorData :

#Sensor Data
DataList = []
DataMaxSize = 0
DataDefault = 450

#Widget
Data = ""
GraphWidth = 100
GraphHeight = 50

#print Data
def printData(self) :

print self.DataList[len(self.DataList)-1]
self.Data.set(str(self.DataList[len(self.DataList)-1]))

#draw Graph (for GUI)
def drawDataGraph(self, _Color) :

graph = Canvas(self.Frame, width=self.GraphWidth, height=self.GraphHeight)
graph.create_line([(i*2, (self.DataList[i]-450)/4+25) for i in range(0, len(self.DataList))], fill=_Color, width=1.5)
graph.grid(row=self.DataLabelRow, column=2)

#append DataList
def appendData(self, _Data):

self.DataList.append(_Data)
if len(self.DataList) > self.DataMaxSize :
self.DataList.pop(0)
print self.DataList

#pack Widget (for GUI)
def packWidget(self, _Frame, _Row, _Font) :

self.Frame = _Frame
self.DataLabelRow = _Row
self.Data = StringVar()
Label(_Frame, text=self.DataName+" :").grid(row=_Row, column=0, padx=self.Padx, pady=self.Pady)
self.DataLabel = Label(_Frame, textvariable=self.Data, font=_Font, width = 10)
self.DataLabel.grid(row=_Row, column=1, padx=5, pady=5)
self.Data.set(str(0))

def __init__ (self, _Name, _Length, _Position, _MaxSize) :

self.DataName = _Name
self.DataLength = _Length
self.DataPosition = _Position
self.DataMaxSize = _MaxSize
self.DataList = [] ##### 追加 #####

class DataCalcurator :

#Data
RawData = []
RawDataName =[]
DataMaxSize = 0

#Widget
RawDataFrame = ""
GraphColor = ["red","green","blue","yellow","orange","gray"]

#print All Raw Data
def printRawData(self):

for i in range(0,len(self.RawData)) :
self.RawData[i].printData()

#Draw Raw DataGraph
def drawRawDataGraph(self) :

for i in range(0, len(self.RawData)) :
self.RawData[i].drawDataGraph(self.GraphColor[i%len(self.GraphColor)])

#Set All Raw Data
def setRawData(self, _Data) :

for i in range(0, len(self.RawData)) :
self.RawData[i].setData(_Data)

#pack Widget
def packWidget(self, _Frame, _Font) :

#Raw Data
self.RawDataFrame = LabelFrame(_Frame, text="Raw Data")
self.RawDataFrame.grid(row=0, column=0, columnspan=1, padx=5, pady=5, sticky=W + E + N + S)

for i in range(0, len(self.RawData)) :
self.RawData[i].packWidget(self.RawDataFrame, i, _Font)

def __init__(self, _RawDataName, _SerialDataList, _DataMaxSize) :

self.RawDataName = _RawDataName
self.DataMaxSize = _DataMaxSize

for i in range(0, len(_RawDataName)) :
AppendData = SensorData(_RawDataName[i], 2, (11+(i*2)), _DataMaxSize)
self.RawData.append(AppendData)

class Main :

#Frames
SensorData = ""
SerialConnector = ""

def packWidget(self, _Title) :

##### MAIN WINDOW #####
root = Tk()
root.title(_Title)

##### sensordata frame #####
sensordataFrame = LabelFrame(root, text="Data")
sensordataFrame.grid(row=0, column=0, padx=5, pady=5, sticky=N + W)
self.SensorData.packWidget(sensordataFrame, "courier 12")

return root

def setData(self) :

while True :

if len(self.SerialDatalist) :
self.SensorData.setRawData(self.SerialDatalist.pop(0))
if GUI_APPLICATION_FLAG :
self.SensorData.printRawData()
self.SensorData.drawRawDataGraph()
else :
time.sleep(0.1) #tmp

def __init__(self) :

#Sensor Data
self.SensorData = DataCalcurator(RAW_DATA_NAME, FEATURE_DATA_NAME, SERIAL_DATA_LIST, DATA_MAXSIZE, SERIAL_DATA_OFFSET)

####################
######  MAIN  ######
####################
def main() :

#GUI APP Flag
global GUI_APPLICATION_FLAG
GUI_APPLICATION_FLAG = True
X = Main()
root = X.packWidget("テスト")
root.mainloop();

if __name__ == '__main__':
main()

こんな風にしてみると、DataListはそれぞれのものを参照するみたいで

printData

は上手くいった。
でも

drawDataGraph

でエラーが出てくるようになってしまった…うーむ。

→グラフのデータがない状態でのエラーで、2つ以上の点を読むようにすればいけました。どうやってグラフ書けって話だよね、当然ですな…。

Leave a Comment more...

threadからDialog

by kewi on 10 月.28, 2009, under Python

Python のスレッドからダイアログを呼ぶとエラーを起こす気がする。
というかTkinterとかのGUI扱うやつもかな?
そんな気がするのでメモ。

Leave a Comment more...

C/C++からPythonの呼び出し

by kewi on 10 月.22, 2009, under Python, Study

環境は、ミングー+Windows用のPython2.5

C→Pythonが多いけども。
いろいろいじくってみるも、できない、できない。死にたい。
でも、


#include   <C:\Python25\include\Python.h>

int main(void)
{
    Py_Initialize();
    PyRun_SimpleString("mystring = \"Hello World from Python\"");
    PyRun_SimpleString("print mystring");
    Py_Finalize();
    return 0;
}

こいつをtest.cとして

gcc test.c -lm C:\Python25\libs\python25.lib

…あら、なんか通った。

a.exe
Hello World from Python

なんかうまく動いてる!?これでいいのか?

Leave a Comment more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...