楼主: Scalachen
1214 4

[Julia]Naive Bayes using Julia [推广有奖]

  • 0关注
  • 0粉丝

本科生

56%

还不是VIP/贵宾

-

TA的文库  其他...

Haskell NewOccidental

Splunk NewOccidental

Apache Storm NewOccidental

威望
0
论坛币
5149 个
通用积分
0
学术水平
9 点
热心指数
11 点
信用等级
9 点
经验
1156 点
帖子
24
精华
1
在线时间
0 小时
注册时间
2015-3-29
最后登录
2017-8-22

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. # Method of computation
  2. # Assumes it works over 2D arrays

  3. function count_inputs_per_class(Y, C)
  4.   # Don't know how to specify type of C
  5.   # Anyhow since it's not known at compile time,
  6.   # doesn't make a performance difference I suppose
  7.   #
  8.   # Maybe find a more fancy comprehension way
  9.   # to do it one line
  10.   N_c = Dict{Any, Int64}()
  11.   for y in Y
  12.     N_c[y] = haskey(N_c, y) ? N_c[y] + 1 : 1
  13.   end
  14.   N_c
  15. end

  16. # X - training set inputs
  17. # Y - training set results
  18. # c - class for which tokens qualify
  19. # t - token for which count needs to be taken
  20. function count_tokens_T_in_class(X, Y, c, t)
  21.   count = 0
  22.   for i = 1:size(X, 1)
  23.     if !(Y[i] == c)
  24.       continue
  25.     end
  26.     for x in X[i,:]
  27.       if x == t
  28.         count += 1
  29.       end
  30.     end
  31.   end #Loop over training set
  32.   count
  33. end

  34. function count_tokens_in_class(X, Y, c)
  35.   count = 0
  36.   for i = 1:size(X, 1)
  37.     if (Y[i] == c)
  38.       count += length(X[i,:])
  39.     end
  40.   end #Loop over training set
  41.   count
  42. end

  43. # Currently supports only Multinomial Naive Bayes
  44. # TODO: Add optional options hash as 3rd parameter
  45. #
  46. # X - Array containing training set inputs.
  47. # Y - Vector containing training set results. (TODO: Change to vector parameter)
  48. # returns - learned classification function gamma
  49. #
  50. function naive_bayes_fit(X::Array, Y::Array)
  51.   V = unique(X)
  52.   C = unique(Y)

  53.   N = size(X, 1)
  54.   N_c = count_inputs_per_class(Y, C)

  55.   prior = Dict{Any, Float64}()
  56.   #P_T_c = Array(Float64, (length(V), length(C)))
  57.   # Might slow down due to the absence of type
  58.   P_T_c = Dict()
  59.   for c in C
  60.     prior[c] = N_c[c] / N
  61.     sum_T_ct = count_tokens_in_class(X, Y, c) + length(V)
  62.     for t in V
  63.       T_ct = count_tokens_T_in_class(X, Y, c, t) + 1
  64.       P_T_c[(t, c)] = T_ct / sum_T_ct
  65.     end
  66.   end

  67.   gamma(d) = naive_bayes_predict(prior, P_T_c, C, d)
  68. end

  69. function naive_bayes_predict(prior, P_T_c, C, d)
  70.   n_classes = length(C)
  71.   scores = Array(Float64, n_classes)
  72.   for i = 1:n_classes; c = C[i]
  73.     scores[i] = prior[c]
  74.     for t in d
  75.       if (haskey(P_T_c, (t, c)))
  76.         scores[i] += log(P_T_c[(t, c)])
  77.       end
  78.     end
  79.   end
  80.   C[indmax(scores)]
  81. end
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:naive Using Bayes Julia baye difference training suppose

本帖被以下文库推荐

沙发
Scalachen 发表于 2015-3-31 21:07:35 |只看作者 |坛友微信交流群
  1. X = reshape(rand(0:5, 600), 6, 100)
  2. Y = [1:6]

  3. gamma = naive_bayes_fit(X, Y)
  4. print(gamma(X[1]))
复制代码

使用道具

藤椅
Nicolle 学生认证  发表于 2018-7-17 01:27:27 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

板凳
Nicolle 学生认证  发表于 2018-7-17 01:28:24 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

报纸
Nicolle 学生认证  发表于 2018-7-17 01:29:26 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-1 13:19