Official address, Https://github.com/scalanlp/breeze/wiki/Quickstar
Because of the format of the editor, you can copy the debug format to the integrated development environment and the code will run correctly
def Breezetest:unit ={
Vector supports access and update,
densevector is a column vector
Val
x = densevector.zeros[double] (5)//Build a 5-dimensional dense vector
Sparsevector.zeros[double] (5)//build a sparse vector that does not allocate space to zero
Like NumPy, negative indices are supported, when i<0, we insert data from the back, i.e. x (i) = x (x.length + i);
X ( -1) = x (5-1) = x (4), because the subscript range supported by x is [ -5,5).
X (-1) = 6;
println ("FIRSTX:" + x + "T size:" + x.length);
println ("-1 Index:" + x (-1));
/**
*
Unlike Scalala, all Vectors are column Vectors. Row vectors are
represented as Transpose[vector[t]]
*
*
when using slicing, using range is much faster than slicing using any one sequence (sequence)
3 to 4 is a range
*/
Val range:Range.Inclusive = 3 to 4,
X (3 to 4): =. 5
X (-4) = 2;
println ("Second x:" + x)//densevector (0.0, 2.0, 0.0, 0.5, 0.5)
In fact, as with the slice parameter in Scala, the key is that the second parameter is until not To,slice (Start:int, Until:int)
Val subvector:densevector[double] = X.slice (2, 5);
println ("subvector:" + subvector);
/**
*
Vectorized-set Operator: = (: = is a to the quantization set operation)
* The
slice operator constructs a read-through and Write-through view of the given elements in the underlying
Or.
* The
slice operation constructs a read channel and a Write channel window (view) for the data in the given vector : = The operation of the quantization set can be assigned a value for this slice operation.
*
*
You can also assign it to a vector of size compatibility.
*/
X (0 to 1): = Densevector (. 6,.5)//Densevector (0.6, 0.5, 0.0, 0.5, 0.5)
println ("Third
x:" + x);
/**
*
Densematrix
*
dense matrices can be constructed in the same way (calling constructors), and can also be accessed and updated
*/
Val m = densematrix.zeros[int] (5,5);
println ("\nfirst m: \ n" + M + "\ n");
println ("Converts dense matrices into dense vectors:" + m.todensevector);
The columns of M can be accessed as densevectors, and the rows as densematrices.
The M column can be used as a densevector in accessing the column vectors, doing densematrix access
M (::, 1) is the Access column, M (4,::) access the row vector labeled 4
println (S "Rows: ${m.rows}, cols: ${m.cols}");
M (::, 1): = Densevector (8, 9, 10, 22, 11);//Column vector
println ("M (::, 1):" + M (::, 1));
M (4,::): = Densevector (1,2,3,4,5). T //Transpose to match row shape
println ("\NSECONDM: \ n" + M + "\ n");
This hermit transformation converts the double vector to an int vector
Implicit Val d2i= new Opset.inplaceimpl2[densevector[int],densevector[double]]
def apply (V:densevector[int], v2:densevector[double]) {
V
: = Densevector (V2.toArray.map (_.toint))
}
}
Val
mCol0 = densevector (0.0, 2.0, 0.0, 4.5, 1.5)
M (::,
0): = mCol0;
println ("\nthird
m: \ n" + M + "\ n");
m: = Densematrix.zeros[int] (3,3)
java.lang.IllegalArgumentException:requirement failed:matrices
must have same number of row
M
: = Densematrix.zeros[int] (5,5)
println ("\nfouth
m: \ n" + M + "\ n");
/**
*
sub-matrices can be sliced and updated, and literal matrices can is
specified using a simple tuple-based syntax.
*
Unlike Scalala, only range slices are supported,
* And only the
columns (or rows for a transposed matrix) can have a
Range of step size different from 1.
*
The matrix can be sliced and updated, using a simple
basic tuple syntax (tuple-based
syntax) literal matrix can be instantiated.
* Unlike
Scalala,
only range partitioning is supported,
{x (0 to 1), X is the column vector}
and only columns (or rows of transpose matrices
) have a range
*
Breeze rows and columns can be divided by range:
m (0 to 1, 0 to 1)
*/
M (0
to 1, 0 to 1): = Densematrix ((3,1), ( -1,-2))
println ("\nfifth
m: \ n" + M + "\ n");
/**
3
1 0 0 0
-1
-2 0 0 0
0
0 0 0 0
0
0 0 0 0
1
2 3 4 5
*
*/
/**
*
Linear Algebra Cheat-sheet enumerated these operations:
Https://github.com/scalanlp/breeze/wiki/Linear-Algebra-Cheat-Sheet
*
similar to MATLAB or NumPy,
Breeze also supports a range of operations
Breeze
matlab numpy
elementwise addition A + b A + b A + b
Elementwise
multiplication a:* b a. * b a * b
Elementwise
comparison a:< b a < B (gives matrix of
1/0 instead of True/false) a < b
InPlace
Addition A: + = 1.0 A = 1 A + = 1
(internal Plus)
InPlace
elementwise multiplication A:* = 2.0 a *= 2 a *= 2
Vector
dot product a dot b,a.t * b† dot (a,b)
dot (a,b)
Elementwise
sum sum (a) sum (sum (a)) a.sum ()
Elementwise
Max A.max Max (a) A.max ()
Elementwise
Argmax Argmax (a) Argmax (a) A.argmax ()
Ceiling
Ceil (a) ceil (a) ceil (a)
//Upward rounding
Floor
Floor (a) Floor (a)
Floor (a)
*/
/**
*
*
Broadcasting:
*
Sometimes you need to apply an action to each row or column of a matrix
,
as a unit
*
For example:
you might calculate the mean value of each row (which can be used for the mean value operation in PCA), or add a vector to each column
*
adapt to a matrix so that the operation can be applied to the
column or line type,
called broadcast broadcasting
;
*
hermits do broadcasting, as
Intelligent as R and NumPy
.
*
means:
if randomly (accidentally) adds a matrix or a vector,
they won't stop you.
* In
Breeze, use *
to indicate your intention (signal
your intent).
*
* means that visually (visualize) wakes
foreach.
*/
Import
Breeze.stats.mean;
Val
dm = Densematrix ((1.0, 2.0, 3.0), (4.0, 5.0, 6.0))//3 a two-dimensional vector
Val
res = DM (::, *) + densevector (3.0, 4.0);//a two-dimensional column vector
println ("\nfirst
res: \ n" + res + "\ n");
println (S "Rows
: ${dm.rows}, cols: ${dm.cols}");
Res (::,
*): = Densevector (3.0, 4.0);
println ("\nsecond
res: \ n" + res + "\ n");
To find the average of each row of the DM matrix
Val
dmean:densevector[double] = mean (DM (*,::))
println ("\nfirst
DM: \ n" + DM + "\ n");
println ("The average value of each row of the DM matrix
:" +
Dmean);
println ("The average value of each column of the DM matrix
:" +
Mean (DM (::, *));
}
def figure:unit ={
/**
* Breeze-viz
* With the version changes, the API will change dramatically, after all, no matplotlib strong
*
* Breeze continued Scalala plotting many functions, although the APIs are somewhat different (but many are inherited from Scalala).
* In the Scaladoc document in the form of trait in the Breeze.plot package.
* First, draw some graphs and save, all the actual drawing work is done by a very sound Jfreechart package
* *
val a = new Densevector[int] (1 to 3 ToArray)
VA L B = new densematrix[