反正就是一整個好玩!!!(  ̄ c ̄)y▂ξ

目前日期文章:201005 (9)

瀏覽方式: 標題列表 簡短摘要
  • May 30 Sun 2010 11:02
  • 收涎

四個月大,收涎

igogo 發表在 痞客邦 留言(0) 人氣()

ruby with sqlite3

#!/usr/bin/ruby -Ku
# encoding: utf-8
require 'amalgalite'
require 'fileutils'

class String
    @@tmp_db = "tmp_db.txt"
    @@gsp_db = "gsp_corpus.db"
    @@gsp_corpus = "gsp_corpus" #table name                                            @@field_name = "word source"
  def to_db #字串與資料庫查詢
    FileUtils.rm(@@tmp_db) if File.exist?(@@tmp_db)  # start with a clean slate    
    my_db=Amalgalite::Database.new(@@gsp_db)
    stmt = my_db.execute("select * from gsp_corpus where word = ?", "#{self}")
    my_db.close

    if stmt.any?
      File.open("./#{@@tmp_db}","a") do |txt|
               txt.puts stmt.join(',')
      end
      res = File.open("./#{@@tmp_db}").read
      return res
    end
  end #end to_db

  def save_to_db
    str = self.gsub(/,/,"\",\"").gsub(/^/,"\"").gsub(/$/,"\"")
    my_db=Amalgalite::Database.new(@@gsp_db)
    stmt = my_db.execute("select * from gsp_corpus where word = ?", "#{self.split(',')[0]}")
  if !(stmt.any?)
   insert_stmt = "insert into gsp_corpus (%s) values (%s)" % [@@field_name.split.join(','), str]
   my_db.execute(insert_stmt)
   puts "insert into sqlite3_db sucessfully"
  end
    my_db.close


  end
end #end class





igogo 發表在 痞客邦 留言(0) 人氣()

OAASVM,CCR, MICRO F1,MACROF1


#!/usr/bin/ruby -Ku
#encoding: utf-8
#適用多類別, 產生ccr, micro f1, macro f

#讀取格式需為 libsvm format


require 'fileutils'


fold_csv = Dir.glob("./fold/*.csv")
fold_computing_data = Array.new

fold_computing_data <<  "fold_name,data size,class A,class B,class C,class D,ccr,Micro F1,Macro F1"
fold_csv.sort!
#從fold0計算到fold4
#index = 0
for index in 0 ..(fold_csv.size-1) do
  printf("%s calating...\n",File.basename("#{fold_csv[index]}"))
  cat_a = 0 ; cat_b = 0 ; cat_c = 0 ; cat_d = 0 ;
  raw_data = Array.new
  class_label = Array.new
  f=File.new("#{fold_csv[index]}")
  while (line = f.gets)
    raw_data << line.split(' ')[0] #測試資料的label
    cat_a += 1 if (line[0] == "1")
    cat_b += 1 if (line[0] == "2")
    cat_c += 1 if (line[0] == "3")
    cat_d += 1 if (line[0] == "4")
  end
  f.close
  data_size = raw_data.size
  class_label = raw_data.sort.uniq  #共有幾類

  micro_a=0 ; micro_c=0; micro_b=0;
  ccr_sum = 0 #該類別並且被正確預測為該類別
#ccr = Array.new
  fmeasure = Array.new
  ccr = Array.new
#四類別,逐一算出class_label[a,b,c,d]
  (class_label.size).times do |cat|
  trains_data = Array.new
  tests_data = Array.new
      #建立測試資料
        f=File.new("#{fold_csv[index]}")
        while (line = f.gets)
          if (class_label[cat] == line.split(' ')[0])
            tests_data << ["1",line.split(' ')[1..-1]].join(' ')
          else
            tests_data << ["-1",line.split(' ')[1..-1]].join(' ')
          end
        end
        f.close
       FileUtils.rm Dir.glob("tests.libsvm") #寫入
       File.open("./tests.libsvm","a") do |txt|
          tests_data.each do |word|
             txt.puts word
          end
       end
       tests_data.clear

        #建立訓練資料
      for i in 0..fold_csv.size-1
        if i != index
          f=File.new("#{fold_csv[i]}")
          while (line = f.gets)
            if (class_label[cat] == line.split(' ')[0])
              trains_data << ["1",line.split(' ')[1..-1]].join(' ')
            else
              trains_data << ["-1",line.split(' ')[1..-1]].join(' ')
            end
          end
          f.close
        end #end if i != index
      end #end for i in 0..fold_csv.size-1
       FileUtils.rm Dir.glob("trains.libsvm") #寫入
       File.open("./trains.libsvm","a") do |txt|
          trains_data.each do |word|
             txt.puts word
          end
       end
       trains_data.clear

    FileUtils.rm Dir.glob("libsvm_result")
    system("svm-train -t 0 trains.libsvm libsvm_model")
    system("svm-predict tests.libsvm libsvm_model libsvm_result")

    #讀取結果
    svm_ans = Array.new
    f=File.new("./libsvm_result")
    while (line = f.gets)
      svm_ans << line[0]  #預測的結果
    end
    f.close
   
    #測試資料的類別
    ori_data = Array.new
    f=File.new("./tests.libsvm")
    while (line = f.gets)
      ori_data << line[0]  #原始資料(正、負類別)
    end
    f.close
    a=0 ;b=0 ; c=0; d=0 ;
    (svm_ans.size).times do |x|
      a += 1 if (ori_data[x][0] == "1" and svm_ans[x] == "1")
      b += 1 if (ori_data[x][0] == "1" and svm_ans[x] != "1")
      c += 1 if (ori_data[x][0] != "1" and svm_ans[x] == "1")
      d += 1 if (ori_data[x][0] != "1" and svm_ans[x] != "1")
    end #end (svm_ans.size).times
    svm_ans.clear
    ori_data.clear
    #a:tp b:fp c:fn d:tn
    printf("[%s],a:%d b:%d c:%d d:%d\n",class_label[cat],a,b,c,d)
    printf("accuracy:%2.3f\n",100*(a+d)/(a+b+c+d).to_f)
    micro_a += a
    micro_b += b
    micro_c += c
    ccr[cat] = 100*(a+d)/(a+b+c+d).to_f
    precision = a.to_f/(a+c)
    recall = a.to_f/(a+b)

    if ((precision+recall) > 0)
      fmeasure[cat] = (2*precision*recall)/((precision+recall).to_f)  #fmeasure = 2pr/(p+r)
    else
      fmeasure[cat] = 0
    end
  #printf("micro_a:%d,micro b:%d, micro c:%d\n", micro_a,micro_b, micro_c)
  end  #end (class_label.size).times do |cat|

#計算此折的正確率
  ccr_rate = sprintf("%2.3f",ccr.inject(0) do |sum, i| sum +i end.to_f/class_label.size) #所有類別中能正確分類出/總資料
  microP = micro_a.to_f/(micro_a+micro_c)
  microR = micro_a.to_f/(micro_a+micro_b)
    if ((microP+microR) > 0)
      micro_f1 = sprintf("%2.3f",100*2*microP*microR.to_f/(microP+microR))
    else
      micro_f1 = 0
    end
  macro_f1 = sprintf("%2.3f",100*fmeasure.inject(0) do |sum, i| sum +i end.to_f/class_label.size) #macro f1的得分
  #printf("p:%2.3f,r:%2.3f, micro f1:%2.3f\n",microP,microR,micro_f1)
#printf("ccr:%2.3f",ans/4.0)
  tmp = Array.new
  tmp << "#{index}" << raw_data.size << cat_a << cat_b << cat_c << cat_d << ccr_rate << micro_f1 << macro_f1
  raw_data.clear
  class_label.clear
  fmeasure.clear
  fold_computing_data << tmp.join(',')
  tmp.clear
  ccr.clear
FileUtils.rm Dir.glob("libsvm_result")
end #end (fold_csv.size).times

printf("oaasvm\n")
fold_computing_data.each do |x|
  puts x
end
#FileUtils.rm Dir.glob("trains.libsvm")
#FileUtils.rm Dir.glob("libsvm_model")





igogo 發表在 痞客邦 留言(0) 人氣()

raw tf csv file

a,1,2,3,4

b,1,2,3,4

c,1,2,3,4

 

fname=ARGV[0]

ary=[]
File.open("#{fname}") { |content|
  while line=content.gets
    ary << line[0].to_s
    ary << line.split(',')[2..-1].map do |x|
                                        if (x.to_f>0)
                                          x = 1
                                        else
                                          x = 0
                                        end
                                      end
    puts ary.join(',')
    ary.clear
  end
}


igogo 發表在 痞客邦 留言(0) 人氣()

  • May 15 Sat 2010 22:26
  • 密碼文章

  • 這是一篇加密文章,請輸入密碼
  • 密碼提示:100
  • 請輸入密碼:

求一超球体的圓心與半徑


#!/usr/bin/ruby -Ku
require 'ai4r'
require 'csv'

def EuclideanDistance(x,y)
  sum = 0
  for i in (0..x.size-1)
    #printf("x[%d]=%f,y[%d]=%f\n",i,x[i],i,y[i])
    ->(px,py){ sum += (px-py)**2.0}.(x[i],y[i])
  end
return Math.sqrt(sum)
end

csv_file = "tfidf-ckip-a.csv"
f=File.new("#{csv_file}")
data = Array.new
while (line = f.gets)
  data << line.split(',').collect  do |s| s.to_f end
end

#data = [[1,1,1], [3,3,3],[9,9,9]]
ai4r_data = Ai4r::Data::DataSet.new(:data_items=> data)
kmeans = Ai4r::Clusterers::KMeans.new
#abuild(dataset,clusters)
res=kmeans.build(ai4r_data,1)
#puts res.inspect
center = res.centroids[0] #此球体圓心
r = 0 #此球体半徑


 r = 0 #此球体半徑
tmp = 0
printf("計算中...\n")
for i  in (0..data.size-1)
  tmp = EuclideanDistance(center,data[i])
  r = tmp if(tmp > r)
end

printf("球体,%s\n",csv_file)
printf("球体半徑為,%f\n",r)
printf("球体圓心為,%s\n",center.join(','))



igogo 發表在 痞客邦 留言(0) 人氣()

 

兩年前買的海芋,隨手种在花園裡,去年只長葉不開花,想不到今年竟意外開花了,真是漂亮 ^^

 

連結

igogo 發表在 痞客邦 留言(1) 人氣()

連結

   

igogo 發表在 痞客邦 留言(0) 人氣()

  • May 03 Mon 2010 17:12
  • 共同作者 人像

  • 這是一篇限定共同作者觀看的文章,若您為共同作者請先登入才可閱讀