I'm trying to study about neural networks, following a great guide:
http://neuralnetworksanddeeplearning.com/chap1.html
Currently I've reached this code snippet which I'm trying to understand and write in Java:
class Network(object):
def __init__(self, sizes):
self.num_layers = len(sizes)
self.sizes = sizes
self.biases = [np.random.randn(y, 1) for y in sizes[1:]]
self.weights = [np.random.randn(y, x)
for x, y in zip(sizes[:-1], sizes[1:])]
I managed to figure out what everything means except for the last line:
[np.random.randn(y, x) for x, y in zip(sizes[:-1], sizes[1:])]
As far as I can understand: create a matrix with y rows and x columns, for each pair x,y which can be found in the matrix zip which is created by the merging of the two "sizes" arrays. I understand that sizes[1:] means taking all elements from sizes starting from index 1, but sizes[:-1] makes no sense to me.
I read online that s[::-1] means getting the reverse of the array, but in the above case we only have one colon, while in the formula for the reverse array there seems to be two colons.
Sadly, I have no idea how Python works and I got pretty far along with the online book to give it up now (I also truly like it), so can someone say if I'm right until now, correct me if needed, or straight out explaining that final line?
sizes[:-1] is a list slice which returns a copy of the sizes list but without the last item.
I'm working with ImageJ. I have two arrays of points (i.e it[ ], cmx[ ]) and what I want is to adjust this to a sine function. I've been working with CurveFitting but I don't understand it very well. I also am having issues with UserFunction.
Is there an easier approach to this? If you have examples I would appreciate it.
The following Groovy script is an example of running curve fitting on three data points:
import ij.measure.CurveFitter;
xData = [0,1,2];
yData = [3.1, 5.1, 6.9];
cv = new CurveFitter((double[]) xData.toArray(), (double[]) yData.toArray());
cv.doFit(CurveFitter.STRAIGHT_LINE);
println (cv.getResultString());
I'm not sure if CurveFitter allows fitting to trigonometric functions, there doesn't seem to be this option in the available fitting types. You might try a high-degree polynomial fitting instead.
You can also ask on the ImageJ forum or mailing list regarding the implementation details of the CurveFitter class.
I am trying to use OpenMaple with Java interface to simply turn a matrix into reduced row echelon form. Ultimately I want to find the inverse of a matrix. I am not sure how to take a 2-d array in my program and make it interact with engine objects.
Some basic examples are here.http://www.maplesoft.com/support/help/Maple/view.aspx?path=OpenMaple/Java/Engine/evaluate
I basically want this: t.evaluate( "with(LinearAlgebra): A:=<<2,4,8>|<8,2,4>|<4,8,2>>; ReducedRowEchelonForm(A);" );
Problem 1: getting a 2-d matrix I made into that statement.
Problem 2: somehow making it output into another 2-d array as a result.
I don't know much about OpenMaple, therefore I come here. Thanks!
I am trying to use Weka for feature selection using PCA algorithm.
My original feature space contains ~9000 attributes, in 2700 samples.
I tried to reduce dimensionality of the data using the following code:
AttributeSelection selector = new AttributeSelection();
PrincipalComponents pca = new PrincipalComponents();
Ranker ranker = new Ranker();
selector.setEvaluator(pca);
selector.setSearch(ranker);
Instances instances = SamplesManager.asWekaInstances(trainSet);
try {
selector.SelectAttributes(instances);
return SamplesManager.asSamplesList(selector.reduceDimensionality(instances));
} catch (Exception e ) {
...
}
However, It did not finish to run within 12 hours. It is stuck in the method selector.SelectAttributes(instances);.
My questions are:
Is so long computation time expected for weka's PCA? Or am I using PCA wrongly?
If the long run time is expected:
How can I tune the PCA algorithm to run much faster? Can you suggest an alternative? (+ example code how to use it)?
If it is not:
What am I doing wrong? How should I invoke PCA using weka and get my reduced dimensionality?
Update: The comments confirms my suspicion that it is taking much more time than expected.
I'd like to know: How can I get PCA in java - using weka or an alternative library.
Added a bounty for this one.
After deepening in the WEKA code, the bottle neck is creating the covariance matrix, and then calculating the eigenvectors for this matrix. Even trying to switch to sparsed matrix implementation (I used COLT's SparseDoubleMatrix2D) did not help.
The solution I came up with was first reduce the dimensionality using a first fast method (I used information gain ranker, and filtering based on document frequencey), and then use PCA on the reduced dimensionality to reduce it farther.
The code is more complex, but it essentially comes down to this:
Ranker ranker = new Ranker();
InfoGainAttributeEval ig = new InfoGainAttributeEval();
Instances instances = SamplesManager.asWekaInstances(trainSet);
ig.buildEvaluator(instances);
firstAttributes = ranker.search(ig,instances);
candidates = Arrays.copyOfRange(firstAttributes, 0, FIRST_SIZE_REDUCTION);
instances = reduceDimenstions(instances, candidates)
PrincipalComponents pca = new PrincipalComponents();
pca.setVarianceCovered(var);
ranker = new Ranker();
ranker.setNumToSelect(numFeatures);
selection = new AttributeSelection();
selection.setEvaluator(pca);
selection.setSearch(ranker);
selection.SelectAttributes(instances );
instances = selection.reduceDimensionality(wekaInstances);
However, this method scored worse then using a greedy information gain and a ranker, when I cross-validated for estimated accuracy.
It looks like you're using the default configuration for the PCA, which judging by the long runtime, it is likely that it is doing way too much work for your purposes.
Take a look at the options for PrincipalComponents.
I'm not sure if -D means they will normalize it for you or if you have to do it yourself. You want your data to be normalized (centered about the mean) though, so I would do this yourself manually first.
-R sets the amount of variance you want accounted for. Default is 0.95. The correlation in your data might not be good so try setting it lower to something like 0.8.
-A sets the maximum number of attributes to include. I presume the default is all of them. Again, you should try setting it to something lower.
I suggest first starting out with very lax settings (e.g. -R=0.1 and -A=2) then working your way up to acceptable results.
Best
for the construction of your covariance matrix, you can use the following formula which is also used by matlab. It is faster then the apache library.
Whereby Matrix is an m x n matrix. (m --> #databaseFaces)
I am trying to display a single magnetic field vector (point in space, with arrow from origin) using data from Android phone sensors. I wrote a simple server in Python to poll the phone for sensor data, and I want to plot the data received in real time. I'm looking for a simple solution, but I can't find any.
I looked at matplotlib, blender, and visual python but I couldn't find a simple enough solution that simply takes the 3 coordinates and plots. The data received is simply a vector with 3 points. The arrow from the origin is not so important, I just want to be able to visualize the moving point in 3d space.
Also, if necessary, I can rewrite the server in Java and use a Java plotting library. I just need some suggestions, and short code examples that achieve this.
You can draw scene by VPython very easily:
from visual import *
import math
def make_grid(unit, n):
nunit = unit * n
f = frame()
for i in xrange(n+1):
if i%5==0:
color = (1,1,1)
else:
color = (0.5, 0.5, 0.5)
curve(pos=[(0,i*unit,0), (nunit, i*unit, 0)],color=color,frame=f)
curve(pos=[(i*unit,0,0), (i*unit, nunit, 0)],color=color,frame=f)
return f
arrow(pos=(0,0,0), axis=(5,0,0), color=(1,0,0), shaftwidth=0.1)
arrow(pos=(0,0,0), axis=(0,5,0), color=(0,1,0), shaftwidth=0.1)
arrow(pos=(0,0,0), axis=(0,0,5), color=(0,0,1), shaftwidth=0.1)
grid_xy = make_grid(0.5, 10)
grid_xz = make_grid(0.5, 10)
grid_xz.rotate(angle=pi/2, axis=(1,0,0), origin=(0,0,0))
grid_yz = make_grid(0.5, 10)
grid_yz.rotate(angle=-pi/2, axis=(0,1,0), origin=(0,0,0))
sphere(radius=0.3)
obj = arrow(pos=(0,0,0), axis=(1,2,3), shaftwidth=0.3)
th = 0
while True:
rate(20)
obj.axis = (3*math.cos(th), 3*math.sin(th), 2)
th += 0.04
VPython is very simple:
pointer = arrow(pos=(0,0,0), axis=(1,2,3), shaftwidth=1)
Just change the axis=(1,2,3) to the 3 points in your vector. More information can be found here.
If c++ is an option you should really take a look at VTK
http://www.vtk.org/
It is very powerful to display 3D vector fields, and is pretty easy to use