Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent for the Numpy multi-dimensional object

After using it for a while, I really like the Numpy multi-dimensional array. It's helpful to write algorithms with a concise yet readable and fairly general code. I wish to have the same thing in Java. Before coding a multi-dimensional array with a Numpy-like API myself, is there such a thing already ?

[PS] I searched a bit, did not see

like image 517
Monkey Avatar asked Dec 04 '11 13:12

Monkey


People also ask

What is NumPy equivalent in Java?

Therefore the equivalent for NumPy in Java would simply be the standard Java math module.

Does Java have multidimensional arrays?

No, Java does not support multi-dimensional arrays. Java supports arrays of arrays. In Java, a two-dimensional array is nothing but, an array of one-dimensional arrays.

What is an multidimensional array in Java?

In Java, a multi-dimensional array is nothing but an array of arrays. 2D array − A two-dimensional array in Java is represented as an array of one-dimensional arrays of the same type. Mostly, it is used to represent a table of values with rows and columns − Int[][] myArray = {{10, 20, 30}, {11, 21, 31}, {12, 22, 32} }

Can Java have 4 dimensional arrays?

In this article, we will show how to declare and initialize Multi Dimensional Array in Java. For Java Multi Dimensional Array better understanding, we are using Three Dimensional Array. Moreover, you can try four dimensional array using the same technique.


8 Answers

The OP is from 2011. So as of end of 2015 I would like mention that there is a new kid in town which claims to be numpy for java -> nd4j. The nice thing is that nd4j is an abstraction layer on top of different libraries like blas. Depending on the size of your matrices there are underlying implementations twice as fast as numpy or jblas. And your code is real platform independent.

like image 80
KIC Avatar answered Nov 15 '22 09:11

KIC


The library Vectorz (https://github.com/mikera/vectorz) offers a fully featured NDArray that is broadly equivalent in functionality to Numpy's NDArray, i.e. it offers the fullowing features:

  • Arbitrary N-dimensional arrays of numeric values (in this case, Java doubles)
  • Lightweight views using strided access for efficient slicing
  • A broad range of mathematical operations with effiecient implementations

It's also very fast: it is much faster then NumPy for most operations, although NumPy may still be faster for certain large matrix operations because it uses the native BLAS libraries to accelerate these.

Here's the NDArray class itself:

https://github.com/mikera/vectorz/blob/develop/src/main/java/mikera/arrayz/NDArray.java

Disclaimer: I'm the author of Vectorz

like image 35
mikera Avatar answered Nov 15 '22 08:11

mikera


You can use numerical libraries for linear algebra; those will have matricies in them. Have a look at Apache Commons Math.

like image 26
duffymo Avatar answered Nov 15 '22 08:11

duffymo


So the closest match seems to be Colt ! http://acs.lbl.gov/software/colt/

It features a multi-dimensional array object, views over an array and your usual linear algebra ! And it's seems to be rather efficient.

like image 27
Monkey Avatar answered Nov 15 '22 08:11

Monkey


Scala has a wider number of numpy-like libraries, if that counts. (You should even be able to use them from Java.)

BIDMat promises to be both powerful and fast (and GPU-powered).

As already mentioned, there is also Breeze

like image 21
Aleksandr Dubinsky Avatar answered Nov 15 '22 10:11

Aleksandr Dubinsky


Another great option is to use Spark’s DataFrame API.

http://spark.apache.org/docs/latest/sql-programming-guide.html

This gives you a Pandas/Numpy like interface to arrays in Java. Plus the code is inherently parallelizable and can be run on a cluster of machines if your data size increases.

like image 38
Asim Jalis Avatar answered Nov 15 '22 08:11

Asim Jalis


Java is rather clumsy for nd-arrays (no operator overloading, etc). If Kotlin is OK, you can try Kotlin-NumPy (https://github.com/Kotlin/kotlin-numpy)

nd4j (https://github.com/deeplearning4j/nd4j) was quite popular some time ago, but now it seems not maintained.

like image 25
iirekm Avatar answered Nov 15 '22 08:11

iirekm


This is an old question, but I just thought I'd add these two Java ndarray libraries:

  • TensorFlow NdArray Java Library
  • AWS Deep Java NdArray Library
like image 37
qwerty Avatar answered Nov 15 '22 08:11

qwerty