Class: PCA::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/appswarm/routing/pca.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Base

Returns a new instance of Base.



45
46
47
48
49
50
51
# File 'lib/appswarm/routing/pca.rb', line 45

def initialize(input)
  assert { input.is_a?(Input) }
  @input=input
  
  computePCA
  @input=nil
end

Instance Attribute Details

#eigenvaluesObject (readonly)

Returns the value of attribute eigenvalues.



44
45
46
# File 'lib/appswarm/routing/pca.rb', line 44

def eigenvalues
  @eigenvalues
end

Instance Method Details

#compress(v) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/appswarm/routing/pca.rb', line 66

def compress(v)
  v=Linalg::DMatrix[v]
  v2=v-@middle
  #pp "---",v2,@baseInv,"---"
  @baseInv*v2.transpose
  
  #pp v2
end

#decompress(v) ⇒ Object



75
76
77
78
# File 'lib/appswarm/routing/pca.rb', line 75

def decompress(v)
  #pp "---",v,@base,"---"
  (@base*v).transpose+@middle
end

#dimensionObject



53
54
55
# File 'lib/appswarm/routing/pca.rb', line 53

def dimension
  @base.hsize
end

#measureErrors(input) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/appswarm/routing/pca.rb', line 80

def measureErrors(input)
  errs=input.map{|i|measureSingleError(i)}
  
  mean=errs.inject(0){|old,new|
   # pp "#{old} #{new}"
    old+new
  }/input.length
  [errs.max,errs.min,mean]
end

#measureSingleError(input) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/appswarm/routing/pca.rb', line 90

def measureSingleError(input)
  inter=compress(input)
  output=decompress(inter)
    pp inter
  input=Linalg::DMatrix[input]
  diff=input-output
  (diff*diff.transpose)[0,0]
end

#reduce(size) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/appswarm/routing/pca.rb', line 57

def reduce(size)
  assert{size<=@eigenvalues.length}
  return if size==@eigenvalues.length
  @eigenvalues=@eigenvalues[0...size]
  
  @base=@base.cut(size,@base.vsize)
  @baseInv=@base.pseudo_inverse
end