楼主: NewOccidental
3252 14

[休闲其它] 【独家发布】Machine Learning: An Algorithmic Perspective(using Java) [推广有奖]

  • 0关注
  • 6粉丝

副教授

31%

还不是VIP/贵宾

-

TA的文库  其他...

Complex Data Analysis

东西方金融数据分析

eBook with Data and Code

威望
0
论坛币
11574 个
通用积分
1.5850
学术水平
119 点
热心指数
115 点
信用等级
114 点
经验
8940 点
帖子
173
精华
10
在线时间
30 小时
注册时间
2006-9-19
最后登录
2022-11-3

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币

http://seat.massey.ac.nz/personal/s.r.marsland/MLBook.html
二维码

扫码加我 拉你入群

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

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

关键词:perspective Algorithmic Perspectiv Algorithm Learning Java

本帖被以下文库推荐

沙发
NewOccidental 发表于 2014-11-28 05:57:37 |只看作者 |坛友微信交流群
  1. # Code from Chapter 2 of Machine Learning: An Algorithmic Perspective (2nd Edition)
  2. # by Stephen Marsland (http://stephenmonika.net)

  3. # You are free to use, change, or redistribute the code in any way you wish for
  4. # non-commercial purposes, but please maintain the name of the original author.
  5. # This code comes with no warranty of any kind.

  6. # Stephen Marsland, 2008, 2014

  7. # Plots a 1D Gaussian function
  8. import pylab as pl
  9. import numpy as np

  10. gaussian = lambda x: 1/(np.sqrt(2*np.pi)*1.5)*np.exp(-(x-0)**2/(2*(1.5**2)))
  11. x = np.arange(-5,5,0.01)
  12. y = gaussian(x)
  13. pl.ion()
  14. pl.plot(x,y,'k',linewidth=3)
  15. pl.xlabel('x')
  16. pl.ylabel('y(x)')
  17. pl.axis([-5,5,0,0.3])
  18. pl.title('Gaussian Function (mean 0, standard deviation 1.5)')
  19. pl.show()
复制代码


使用道具

藤椅
NewOccidental 发表于 2014-11-28 05:59:19 |只看作者 |坛友微信交流群
  1. # Code from Chapter 3 of Machine Learning: An Algorithmic Perspective (2nd Edition)
  2. # by Stephen Marsland (http://stephenmonika.net)

  3. # You are free to use, change, or redistribute the code in any way you wish for
  4. # non-commercial purposes, but please maintain the name of the original author.
  5. # This code comes with no warranty of any kind.

  6. # Stephen Marsland, 2008, 2014

  7. import numpy as np

  8. class pcn:
  9.         """ A basic Perceptron"""
  10.        
  11.         def __init__(self,inputs,targets):
  12.                 """ Constructor """
  13.                 # Set up network size
  14.                 if np.ndim(inputs)>1:
  15.                         self.nIn = np.shape(inputs)[1]
  16.                 else:
  17.                         self.nIn = 1
  18.        
  19.                 if np.ndim(targets)>1:
  20.                         self.nOut = np.shape(targets)[1]
  21.                 else:
  22.                         self.nOut = 1

  23.                 self.nData = np.shape(inputs)[0]
  24.        
  25.                 # Initialise network
  26.                 self.weights = np.random.rand(self.nIn+1,self.nOut)*0.1-0.05

  27.         def pcntrain(self,inputs,targets,eta,nIterations):
  28.                 """ Train the thing """       
  29.                 # Add the inputs that match the bias node
  30.                 inputs = np.concatenate((inputs,-np.ones((self.nData,1))),axis=1)
  31.                 # Training
  32.                 change = range(self.nData)

  33.                 for n in range(nIterations):
  34.                        
  35.                         self.activations = self.pcnfwd(inputs);
  36.                         self.weights -= eta*np.dot(np.transpose(inputs),self.activations-targets)
  37.                
  38.                         # Randomise order of inputs
  39.                         #np.random.shuffle(change)
  40.                         #inputs = inputs[change,:]
  41.                         #targets = targets[change,:]
  42.                        
  43.                 #return self.weights

  44.         def pcnfwd(self,inputs):
  45.                 """ Run the network forward """
  46.                 # Compute activations
  47.                 activations =  np.dot(inputs,self.weights)

  48.                 # Threshold the activations
  49.                 return np.where(activations>0,1,0)


  50.         def confmat(self,inputs,targets):
  51.                 """Confusion matrix"""

  52.                 # Add the inputs that match the bias node
  53.                 inputs = np.concatenate((inputs,-np.ones((self.nData,1))),axis=1)
  54.                
  55.                 outputs = np.dot(inputs,self.weights)
  56.        
  57.                 nClasses = np.shape(targets)[1]

  58.                 if nClasses==1:
  59.                         nClasses = 2
  60.                         outputs = np.where(outputs>0,1,0)
  61.                 else:
  62.                         # 1-of-N encoding
  63.                         outputs = np.argmax(outputs,1)
  64.                         targets = np.argmax(targets,1)

  65.                 cm = np.zeros((nClasses,nClasses))
  66.                 for i in range(nClasses):
  67.                         for j in range(nClasses):
  68.                                 cm[i,j] = np.sum(np.where(outputs==i,1,0)*np.where(targets==j,1,0))

  69.                 print cm
  70.                 print np.trace(cm)/np.sum(cm)
  71.                
  72. def logic():
  73.         import pcn
  74.         """ Run AND and XOR logic functions"""

  75.         a = np.array([[0,0,0],[0,1,0],[1,0,0],[1,1,1]])
  76.         b = np.array([[0,0,0],[0,1,1],[1,0,1],[1,1,0]])

  77.         p = pcn.pcn(a[:,0:2],a[:,2:])
  78.         p.pcntrain(a[:,0:2],a[:,2:],0.25,10)
  79.         p.confmat(a[:,0:2],a[:,2:])

  80.         q = pcn.pcn(b[:,0:2],b[:,2:])
  81.         q.pcntrain(b[:,0:2],b[:,2:],0.25,10)
  82.         q.confmat(b[:,0:2],b[:,2:])
复制代码


使用道具

板凳
Nicolle 学生认证  发表于 2014-11-28 06:02:21 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

报纸
Nicolle 学生认证  发表于 2014-11-28 06:04:25 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

地板
Nicolle 学生认证  发表于 2014-11-28 06:10:44 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

7
Nicolle 学生认证  发表于 2014-11-28 06:14:22 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

8
Nicolle 学生认证  发表于 2014-11-28 06:16:15 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

9
Nicolle 学生认证  发表于 2014-11-28 06:17:18 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

10
Nicolle 学生认证  发表于 2014-11-28 06:19:31 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

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

本版微信群
加JingGuanBbs
拉您进交流群

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

GMT+8, 2024-5-7 05:15