Follow the project repository: https://github.com/Kotlin/kotlin-numpy
%use numpy(0.1.4)
val a = array(arrayOf(1L, 2L, 3L))
val b = array<Double>(listOf(listOf(1.5f, 2f, 3f), listOf(4f, 5f, 6f)))
val c = array<Double>(
listOf(
listOf(listOf(1.5f, 2f, 3f), listOf(4f, 5f, 6f)),
listOf(listOf(3f, 2f, 1f), listOf(4f, 5f, 6f))
)
)
zeros<Int>(3, 4) // Create an array of zeros
ones<Short>(2, 3, 4) // Create an array of ones
val d = arange<Long>(10, 25, 5) // Create an array of evenly spaced values (step value)
linspace<Float>(0, 2, 9) // Create an array of evenly spaced values (number of samples)
val e = full(intArrayOf(2, 2), 7L) // Create a constant array
val f = eye<Double>(2) // Create a 2x2 identity matrix
Random.random(2, 2) // Create an array with random values
empty<Float>(3, 2) // Create an empty array
[[1.4012985e-45 0.0000000e+00] [2.0743408e+01 4.5909340e-41] [2.0743408e+01 4.5909340e-41]]
// a.toFile("my_array")
// fromfile<Long>("my_array")
a.shape // Array domensions
b.ndim // Number of array dimensions
e.size // Number of array elements
b.dtype // Dara type of array elements
b.dtype.name // Name of data type
b.asType<Double, Int>() // Convert an array to a different type
[[1 2 3] [4 5 6]]
val g = a - b
println("Subtraction:")
println(a - b) // Subraction
println("Addition:")
println(b + a) // Addition
println("Division:")
println(a / b) // Division
println("Multiplication:")
println(a * b) // Multiplication
exp(b) // Exponentiation
sqrt(b) // Square root
sin(a) // Sin
cos(b) // Cos
log(a) // natural logarithm
e.dot(f) // Dot product
Subtraction: [[-0.5 0. 0. ] [-3. -3. -3. ]] Addition: [[2.5 4. 6. ] [5. 7. 9. ]] Division: [[0.66666667 1. 1. ] [0.25 0.4 0.5 ]] Multiplication: [[ 1.5 4. 9. ] [ 4. 10. 18. ]]
[[7. 7.] [7. 7.]]
println("Element-wise comparison")
println(c eq b)
println("Element-wise comparison")
println(a lt 2)
println("Array comparison")
println(a == b)
Element-wise comparison [[[ True True True] [ True True True]] [[False True False] [ True True True]]] Element-wise comparison [ True False False] Array comparison false
a.sum() // Array sum
a.min() // Array minimum value
b.max(0) // Maximum value of an array row
b.cumSum(1) // Cumulative sum of the lements
a.mean() // Mean
median(b) // Median
corrcoef<Long, Long>(a) // Correlation coefficient
std(d) // Standard deviation
4.08248290463863
var h = a.view<Long, Long>()
copy(a)
h = copy(a)
a.sort()
c.sort(axis = 0)
println(a[2]) // Select the element at the 2nd index
println(b[1, 2]) // Select the element at row 1 column 2
3 6.0
println(a[0..2]) // Select items at index 0 and 1
println(b[0..2, 1]) // Select items at rows 0 and 1 in column 1
println(b[None..1]) // Select all items at row 0
println(c[1, None..None, None..None]) // Same as [1, ...] or [1, :, :] in Python
println(a[None..None..-1]) // Reversed array a
[1 2] [2. 5.] [[1.5 2. 3. ]] [[3. 2. 3.] [4. 5. 6.]] [3 2 1]
a[a lt 2] // Select elements from a less than 2
[1]
println(b[arrayOf(1, 0, 1, 0), arrayOf(0, 1, 2, 0)]) // Select elements (1,0), (0,1), (1,2) and (0, 0)
println(b[arrayOf(1, 0, 1, 0)][None..None, arrayOf(0, 1, 2, 0)]) // Select a subset of the matrix's rows and columns
[4. 2. 6. 1.5] [[4. 5. 6. 4. ] [1.5 2. 3. 1.5] [4. 5. 6. 4. ] [1.5 2. 3. 1.5]]
// Flat array iteration
for (element in a.flatIter()) {
print("$element ")
}
println()
// Iteration along the axes of the array
for (ax in b) {
println(ax)
}
1 2 3 [1.5 2. 3. ] [4. 5. 6.]
val i = transpose(b) // Permute array dimensions
i.t
[[1.5 2. 3. ] [4. 5. 6. ]]
b.ravel() // Flatten the array
g.reshape(3, -2) // Reshape, but don't chenge data
[[-0.5 0. ] [ 0. -3. ] [-3. -3. ]]
h.resize(2, 6) // Change to new array with shape (2,6)
append(h.view(), g) // Append items to an array
insert(a, 1, array(arrayOf(5L))) // Insert items in an array
delete(a, 1) // Delete items from an array
[1 3]
println(concatenate(a, d, axis = 0)) // Concatenate arrays
println(vstack(a.view(), b)) // Stack arrays vertically
println(hstack(e.view(), f)) // Stack arrays horizontally
println(columnStack(a, d)) // Create stacked column-wise arrays
[ 1 2 3 10 15 20] [[1. 2. 3. ] [1.5 2. 3. ] [4. 5. 6. ]] [[7. 7. 1. 0.] [7. 7. 0. 1.]] [[ 1 10] [ 2 15] [ 3 20]]
hsplit(a, 3) // Split the array horizontally at the 3rd index
[[1], [2], [3]]