How to read csv file into an Array of arrays in scala - arrays

I'm trying to read a csv file and return it as an array of arrays of doubles (Array[Array[Double]]). It's pretty clear how to read in a file line by line and immediately print it out, but not how to store it in a two-dimensional array.
def readCSV() : Array[Array[Double]] = {
val bufferedSource = io.Source.fromFile("/testData.csv")
var matrix :Array[Array[Double]] = null
for (line <- bufferedSource.getLines) {
val cols = line.split(",").map(_.trim)
matrix = matrix :+ cols
}
bufferedSource.close
return matrix
}
Had some type issues and then realized I'm not doing what I thought I was doing. Any help pointing me on the right track would be very appreciated.

It seems your question was already answered. So just as a side note, you could write your code in a more scalaish way like this:
def readCSV() : Array[Array[Double]] = {
io.Source.fromFile("/testData.csv")
.getLines()
.map(_.split(",").map(_.trim.toDouble))
.toArray
}

1) You should start with an empty Array unstead of null.
2) You append items to an Array with the operator :+
3) As your result type is Array[Array[Double]] you need to convert the strings of the csv to Double.
def readCSV() : Array[Array[Double]] = {
val bufferedSource = io.Source.fromFile("/testData.csv")
var matrix :Array[Array[Double]] = Array.empty
for (line <- bufferedSource.getLines) {
val cols = line.split(",").map(_.trim.toDouble)
matrix = matrix :+ cols
}
bufferedSource.close
return matrix
}

Related

Convert [MLMultiArray] to Float?

I have an MLMultiArray which is a result of an ML Model.
I need to convert it to Float so that I can further store it in Realm.
Below is an example of one of the MLMultiArray. The result from the ML Model contains 120 of the same vectors so its an array of MLMultiArrays i.e Array of Float32 1 x 128 matrices.
Float32 1 x 128 matrix
[4.476562,1.179688,0.07141113,6.976562,-0.2858887,-7.378906,0.6445312,3.695312,1.399414,2.486328,-3.988281,-0.2636719,1.000977,-4.480469,-7.832031,1.59082,0.8515625,-1.296875,-1.435547,7.839844,5.851562,0.3701172,-2.492188,7.273438,2.404297,-3.3125,-5.699219,-0.6816406,0.2807617,-3.882812,-3.982422,5.339844,4.125,-3.871094,0.6225586,1.712891,-10.02344,0.7119141,4.472656,3.566406,-0.559082,-1.049805,-4.679688,10.07812,-1.459961,4.707031,-6.078125,1.675781,-0.6259766,2.519531,3.472656,-3.400391,-6.714844,-4.933594,-1.733398,1.095703,-6.15625,9.234375,3.693359,-9.492188,0.8637695,0.8203125,-2.814453,-4.4375,-1.092773,3.332031,0.1623535,3.583984,-11.25781,-0.9941406,-0.3491211,1.464844,-1.579102,4.558594,2.703125,4.601562,5.914062,-2.402344,-5.46875,-0.355957,11.39062,2.070312,-7.289062,-0.4470215,-0.1595459,9.148438,1.833008,-2.097656,-3.9375,6.699219,-4.347656,-6.835938,-1.179688,3.910156,-13.07812,-1.947266,-0.9238281,-0.949707,-4.398438,2.363281,4.421875,4.632812,2.607422,8.773438,0.9106445,9.21875,-14.0625,-1.301758,-4.875,0.6054688,6.496094,-2.021484,3.898438,-4.644531,0.9853516,7.253906,3.066406,-1.051758,-8.09375,-6.527344,3.890625,5.175781,0.3701172,-0.5683594,-1.341797,0.1497803,4.074219,0.5932617]
Is there any way I can convert an array of MLMultiArray to Float32?
Any help would be appreciated <3
You can first convert the MLMultiArray to an UnsafeBufferPointer and then to a regular Array.
import CoreML
var a: [Float] = [ 1, 2, 3 ]
var m = try! MLMultiArray(a)
if let b = try? UnsafeBufferPointer<Float>(m) {
let c = Array(b)
print(c)
}
This is old question but this can help someone:
To convert from an MLMultiArray To An Array of primitive, You can use this function you will need just to change the output type
func convertToArray(from mlMultiArray: MLMultiArray) -> [Double] {
// Init our output array
var array: [Double] = []
// Get length
let length = mlMultiArray.count
// Set content of multi array to our out put array
for i in 0...length - 1 {
array.append(Double(truncating: mlMultiArray[[0,NSNumber(value: i)]]))
}
return array
}
To convert from an Array to MLMultiArray Use this, you may need to change the shape accordingly
func convertToMLMultiArray(from array: [Double]) -> MLMultiArray {
let length = NSNumber(value: array.count)
// Define shape of array
guard let mlMultiArray = try? MLMultiArray(shape:[1, length], dataType:MLMultiArrayDataType.double) else {
fatalError("Unexpected runtime error. MLMultiArray")
}
// Insert elements
for (index, element) in array.enumerated() {
mlMultiArray[index] = NSNumber(floatLiteral: element)
}
return mlMultiArray
}
I'm using this way, and subscript to access the element and it works fine for me.
const float *array = (float*)matrix.dataPointer

Sort first dimension of two dimensional array only Google Script

I am trying to sort only the first dimension of a two-dimensional array
I have
arr = [a,b,c,a,b,c,a,b,c]
arr1 = arr.sort() --> arr1 = [a,a,a,b,b,b,c,c,c]
result = transpose([arr1,arr])
Which gives
result = [[[a],[a]],[[a],[a]],[[a],[a]],[[b],[b]],[[b],[b]],[[b],[b]],[[c],[c]],[[c],[c]],[[c],[c]]]
But I need (and expected)
result = [[[a],[a]],[[a],[b]],[[a],[c]],[[b],[a]],[[b],[b]],[[b],[c]],[[c],[a]],[[c],[b]],[[c],[c]]]
Thanks
You need to make an actual clone of the array, arr is being sorted in what you are doing.
try this:
arr = [a,b,c,a,b,c,a,b,c]
arr1 = arr.slice(0);
arr1.sort();
result = transpose([arr1,arr])
My testing is limited because transpose isn't a GAS function and you didn't include it.
Actually with, this, it seems to work:
function transpose(a)
{
return Object.keys(a[0]).map(function (c) { return a.map(function (r) { return r[c]; }); });
}

How to convert String array to Int array in Kotlin?

Kotlin has many shorthands and interesting features. So, I wonder if there is some fast and short way of converting array of string to array of integers. Similar to this code in Python:
results = [int(i) for i in results]
You can use .map { ... } with .toInt() or .toIntOrNull():
val result = strings.map { it.toInt() }
Only the result is not an array but a list. It is preferable to use lists over arrays in non-performance-critical code, see the differences.
If you need an array, add .toTypedArray() or .toIntArray().
I'd use something simple like
val strings = arrayOf("1", "2", "3")
val ints = ints.map { it.toInt() }.toTypedArray()
Alternatively, if you're into extensions:
fun Array<String>.asInts() = this.map { it.toInt() }.toTypedArray()
strings.asInts()
If you are trying to convert a List structure that implements RandomAccess (like ArrayList, or Array), you can use this version for better performance:
IntArray(strings.size) { strings[it].toInt() }
This version is compiled to a basic for loop and int[]:
int size = strings.size();
int[] result = new int[size];
int index = 0;
for(int newLength = result.length; index < newLength; ++index) {
String numberRaw = strings.get(index);
int parsedNumber = Integer.parseInt(numberRaw);
result[index] = parsedNumber;
}
If you use Array.map as other answers suggest, you get back a List, not an Array. If you want to map an array strings to another array results, you can do it directly like this:
val results = Array(strings.size) { strings[it].toInt() }
This is more efficient than first mapping to a List and then copying the elements over to an Array by calling .toTypedArray().
Consider the input like this "" (empty string)
It would be better to do the filtering first. And it is true the return value is list but not array.
If you need an array, add .toTypedArray() or .toIntArray().
fun stringToIntList(data: String): List<Int> =
data.split(",").filter { it.toIntOrNull() != null }
.map { it.toInt() }
val result = "[1, 2, 3, 4, 5]".removeSurrounding("[","]").replace(" ","").split(",").map { it.toInt() }
Found following simplest
strings.chars().toArray()

Doing math with elements of a 2d-array?

So I am making a 2d-array like this:
var kcalVerdier:Array = new Array(92,80,103,36,53);
var alleNumSteppers:Array = new Array(alleNumSteps.numStepMelk.value,alleNumSteps.numStepEgg.value,alleNumSteps.numStepBrød.value,alleNumSteps.numStepSmør.value,alleNumSteps.numStepOst.value);
var c:Array = new Array(kcalVerdier,alleNumSteppers);
function endreAntall(evt:Event)
{
txtTotalKcal.text = String(c[0] * [0]);
}
Is it not possible to do multiply 2 values of a 2-d Array? I get this error:
Scene 1, Layer 'script', Frame 1, Line 17, Column 38 1067: Implicit coercion of a value of type Array to an unrelated type Number.
I don't understand why, c[0][0] should both be integer values or am I misunderstanding?
Which values you actually want to multiply?
By doing:
c[0] * [0]
you're trying to multiply Array by Array.
c[0] would be 1st element of c Array which is in fact kcalVerdier Array.
[0] is making new Array with one element (that is 0);
so it's like
[92,80,103,36,53] * [0]
[EDIT]
Ok, try this piece of code:
// Check if both Arrays are what we want:
trace("kcalVerdier => " + c[0]);
trace("alleNumSteppers => " + c[0]);
trace();
// Gett Arrays lengths
var arr1Length:int = c[0].length;
var arr2Length:int = c[1].length;
// Check if both are the same length
if(arr1Length == arr2Length)
{
// Let's iterate
for(var i:int = 0; i<arr1Length; i++)
{
trace( c[0][i] * c[1][i] );
}
}

Scala updating Array elements

I never thought I would be asking such a simple question but how do I update array element in scala
I have declared inner function inside my Main object and I have something like this
object Main
{
def main(args: Array[String])
{
def miniFunc(num: Int)
{
val myArray = Array[Double](num)
for(i <- /* something*/)
myArray(i) = //something
}
}
}
but I always get an exception, Could someone explain me why and how can I solve this problem?
Can you fill in the missing details? For example, what goes where the comments are? What is the exception? (It's always best to ask a question with a complete code sample and to make it clear what the problem is.)
Here's an example of Array construction and updating:
scala> val num: Int = 2
num: Int = 2
scala> val myArray = Array[Double](num)
myArray: Array[Double] = Array(2.0)
scala> myArray(0) = 4
scala> myArray
res6: Array[Double] = Array(4.0)
Perhaps you are making the assumption that num represents the size of your array? In fact, it is simply the (only) element in your array. Maybe you wanted something like this:
def miniFunc(num: Int) {
val myArray = Array.fill(num)(0.0)
for(i <- 0 until num)
myArray(i) = i * 2
}

Resources