Convert float 2-D array to integer 2-D array in Julia - arrays

I know that it is possible to convert a Float64 into an Int64
using the convert function.
Unfortunately, it doesn't work when applying convert to a 2-D array.
julia> convert(Int64, 2.0)
2
julia> A = [1.0 2.0; 3.0 4.0]
2x2 Array{Float64,2}:
1.0 2.0
3.0 4.0
julia> convert(Int64, A)
ERROR: `convert` has no method matching convert(::Type{Int64}, ::Array{Float64,2
})
in convert at base.jl:13
How do I convert a 2-D array of floats into a 2-D array of ints?
What I tried
I could do it using the following code,
which is a little verbose but it works.
I am hoping there is an easier way to do it though.
julia> A = [1.0 2.0; 3.0 4.0]
2x2 Array{Float64,2}:
1.0 2.0
3.0 4.0
julia> B = Array(Int64, 2, 2)
2x2 Array{Int64,2}:
4596199964293150115 4592706631984861405
4604419156384151675 0
julia> for i = 1:2
for j = 1:2
B[i,j] = convert(Int64,A[i,j])
end
end
julia> B
2x2 Array{Int64,2}:
1 2
3 4
An answer that doesn't work for me
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "help()" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.3.10 (2015-06-24 13:54 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org release
|__/ | x86_64-linux-gnu
julia> A = [1.2 3.4; 5.6 7.8]
2x2 Array{Float64,2}:
1.2 3.4
5.6 7.8
julia> round(Int64, A)
ERROR: `round` has no method matching round(::Type{Int64}, ::Array{Float64,2})

You can convert a 2x2 array of floats into a 2x2 array of ints very easily, after you decide how you want rounding to be handled:
julia> A = [1.0 -0.3; 3.9 4.5]
2x2 Array{Float64,2}:
1.0 -0.3
3.9 4.5
julia> round.(Int, A)
2x2 Array{Int64,2}:
1 0
4 4
julia> floor.(Int, A)
2x2 Array{Int64,2}:
1 -1
3 4
julia> trunc.(Int, A)
2x2 Array{Int64,2}:
1 0
3 4
julia> ceil.(Int, A)
2x2 Array{Int64,2}:
1 0
4 5

You can use map, which preserves the matrix dimensions, and does not depend on vectorized methods:
julia> x = rand(2,2)
2x2 Array{Float64,2}:
0.279777 0.610333
0.277234 0.947914
julia> map(y->round(Int,y), x)
2x2 Array{Int64,2}:
0 1
0 1

This answer is for Julia v0.3. For newer versions, see answer of DSM
Use the int function:
julia> a = rand(2,2)
2x2 Array{Float64,2}:
0.145651 0.362497
0.879268 0.753001
julia> int(a)
2x2 Array{Int64,2}:
0 0
1 1

Related

Equivalent np.nanquantile in Julia?

Is there a np.nanquantile equivalent in Julia? I have a 2D array and calculate a quantile along one axis but the array contains NaN-values. My current code block:
quantiles = Array{Float32}(undef, size(array, 1), 2)
p=0.1
quantiles[:, 1] = mapslices(x -> quantile(x, p), array, dims = 2)
quantiles[:, 2] = mapslices(x -> quantile(x, 1 - p), array, dims = 2)
The simplest thing to do is to use the following:
x -> quantile(filter(!isnan, x), p)
e.g.
julia> array = [1 NaN 3 4
NaN 2 3 4]
2×4 Matrix{Float64}:
1.0 NaN 3.0 4.0
NaN 2.0 3.0 4.0
julia> mapslices(x -> quantile(filter(!isnan, x), 0.5), array, dims = 2)
2×1 Matrix{Float64}:
3.0
3.0

Remove a value in an array and decrease the size of it

I have an array filled with some values. After running this code:
array = zeros(10)
for i in 1:10
array[i] = 2*i + 3
end
The array looks like:
10-element Array{Float64,1}:
5.0
7.0
9.0
11.0
13.0
15.0
17.0
19.0
21.0
23.0
I would like to obtain, for example, the following array by removing the third value:
9-element Array{Float64,1}:
5.0
7.0
11.0
13.0
15.0
17.0
19.0
21.0
23.0
How to do that?
EDIT
If I have an array (and not a vector), like here:
a = [1 2 3 4 5]
1×5 Array{Int64,2}:
1 2 3 4 5
The deleteat! proposed is not working:
a = deleteat!([1 2 3 4 5], 1)
ERROR: MethodError: no method matching deleteat!(::Array{Int64,2}, ::Int64)
You might have used a 2d row vector where a 1d column vector was required.
Note the difference between 1d column vector [1,2,3] and 2d row vector [1 2 3].
You can convert to a column vector with the vec() function.
Closest candidates are:
deleteat!(::Array{T,1} where T, ::Integer) at array.jl:875
deleteat!(::Array{T,1} where T, ::Any) at array.jl:913
deleteat!(::BitArray{1}, ::Integer) at bitarray.jl:961
I don't want a column vector. I would want:
1×4 Array{Int64,2}:
2 3 4 5
Is it possible ?
To make that clear: Vector{T} in Julia is just a synonym for Array{T, 1}, unless you're talking about something else... we call Arrays of all ranks arrays.
But this seems to be a Matlab-inherited misconception. In Julia, you construct a Matrix{T}, ie., an Array{T, 2}, by using spaces in the literal:
julia> a = [1 2 3 4 5]
1×5 Array{Int64,2}:
1 2 3 4 5
Deleting from a matrix does not make sense in general, since you can't trivially "fix the shape" in a rectangular layout.
A Vector or Array{T, 1} can be written using commas:
julia> a = [1, 2, 3, 4, 5]
5-element Array{Int64,1}:
1
2
3
4
5
And on this, deleteat! works:
julia> deleteat!(a, 1)
4-element Array{Int64,1}:
2
3
4
5
For completeness, there's also a third variant, the RowVector, which results of a transposition:
julia> a'
1×4 RowVector{Int64,Array{Int64,1}}:
2 3 4 5
From this you also can't delete.
Deleteat! is only defined for:
Fully implemented by:
Vector (a.k.a. 1-dimensional Array)
BitVector (a.k.a. 1-dimensional BitArray)
A Row Vector (2 Dimensions) won't work.
But ... there is a workaround by this trick:
julia> deleteat!(a[1,:], 1)' # mind the ' -> transposes it back to a row vector.
1×4 RowVector{Int64,Array{Int64,1}}:
2 3 4 5
Ofcourse this wouldn't work for an Array with 2 or more rows.

How can I find a value's location in a matrix in Julia?

I was using find with a 3D matrix A like this:
julia> find(A.==1)
2-element Array{Int64,1}:
1
234
4567
Julia gives me the location as an index instead of as an array of indices. For example, it returns 234 instead of (1,2,1).
I looked at this question, but my matrix is very large and has a shape of (360,360,360). I can't use the method suggested there.
I tried to study its index pattern and transform it using a function that I coded:
function cmf_p(matrix)
for a=1:length(matrix);
aa=matrix[a]
rd_u_m=ceil(aa/(360^2))
rd_d_m=floor(aa/(360^2)-1)
rd_d_t=(aa-rd_d_m*360)/360^2
rd_d_p=aa-rd_d_m*360^2-floor(rd_d_t)*360
println(rd_u_m);
println(ceil(rd_d_t)*360);
println(ceil(aa-rd_d_m*360^2-floor(rd_d_t)*360))
end
end
But it gives me the wrong result.
How can I use the index and transform it to the location I want?
You are looking for ind2sub:
julia> A = eye(3)
3x3 Array{Float64,2}:
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
julia> inds = find(A.==1.0)
3-element Array{Int64,1}:
1
5
9
julia> [ind2sub(size(A), ind) for ind in inds]
3-element Array{Any,1}:
(1,1)
(2,2)
(3,3)

How to multiply multi-dimensional arrays/matrices in Julia

Multiplying two multi-dimensional arrays, say, a 1-dimensional with a 3-dimensional array:
[1 2] * reshape(1:8,2,2,2)
gives me the error message:
LoadError: MethodError: `*` has no method matching *(::Array{Int64,2}, ::Array{Int64,3})
Closest candidates are:
*(::Any, ::Any, !Matched::Any, !Matched::Any...)
*{TA,TB}(::Union{DenseArray{TA,1},DenseArray{TA,2},SubArray{TA,1,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD},SubArray{TA,2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}, !Matched::Base.LinAlg.AbstractTriangular{TB,S<:AbstractArray{T,2}})
*{TA,TQ,N}(::Union{DenseArray{TA,N},SubArray{TA,N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}, !Matched::Union{Base.LinAlg.QRCompactWYQ{TQ,M<:AbstractArray{T,2}},Base.LinAlg.QRPackedQ{TQ,S<:AbstractArray{T,2}}})
...
while loading In[167], in expression starting on line 1
in Ac_mul_B at operators.jl:157
using the math definition of multi-dimensional matrix algebra for a (1 by 2) * (2 by 2 by 2) multiplication of matrices/arrays.
A somewhat more general example can be A*B = C meaning sum_k A_{i,j,k} B_{k,l,m} = C_{i,j,l,m}, where A is a 3-index matrix, or tensor if you like, B is a 3-index one, and the resulting C is a four index matrix/tensor, but, in general, there can be any number of dimensions and a dimension can have any size (within reason). See more info on the definition of a matrix product or tensor contraction.
What is the right syntax of this multiplication in Julia?
You can use reshape to convert the multi-dimensional arrays into matrices,
multiply them, and convert the result back to a multi-dimensional array.
A = [1 2]
B = reshape(1:8,2,2,2)
reshape( reshape(A,2,1)' * reshape(B,2,4), 2, 2 )
(In this example, since A is already a matrix, there is actually no need to reshape it.)
Matrices multiplication: use *
Matrices element-wise multiplication: use .*
If you want to multiply two matrices A*B, the size of both must much: e.g.
size of A (a1,a2),
size of B (b1, b2);
in order to make the multiplication feasible a2 must be the same as b1
In your case:
julia> A=[1 2]
1x2 Array{Int64,2}:
1 2
julia> size(A) #remember that size(A') is (2,1)
(1,2)
julia> B=reshape(1:8,2,2,2)
2x2x2 Array{Int64,3}:
[:, :, 1] =
1 3
2 4
[:, :, 2] =
5 7
6 8
julia> size(B)
(2,2,2)
julia> size(B[:,:,1])
(2,2)
julia> A*B[:,:,1]
1x2 Array{Int64,2}:
5 11
julia> A*B[:,:,2]
1x2 Array{Int64,2}:
17 23
Edit: Multiplication in "one shot" (separated for clarity):
julia> A*B[:,:,1]
1x2 Array{Int64,2}:
5 11
julia> A*B[:,:,2]
1x2 Array{Int64,2}:
17 23
julia> C=A*B[:,:]
1x4 Array{Int64,2}:
5 11 17 23
julia> size(C)
(1,4)
julia> D1 = reshape(C, 2,2)
2x2 Array{Int64,2}:
5 17
11 23
julia> D2 = reshape(C, 2,2)'
2x2 Array{Int64,2}:
5 11
17 23
Truly in one shot:
julia> D = reshape(A*B[:,:], 2,2)
2x2 Array{Int64,2}:
5 17
11 23

Convert Julia array to dataframe

I have an array X that I'd like to convert to a dataframe. Upon recommendation from the web, I tried converting to a dataframe and get the following error.
julia> y=convert(DataFrame,x)
ERROR:converthas no method matching convert(::Type{DataFrame}, ::Array{Float64,2})
in convert at base.jl:13
When I try DataFrame(x), the conversion works but i get a complaint that the conversion is deprecated.
julia> DataFrame(x)
WARNING: DataFrame(::Matrix, ::Vector)) is deprecated, use convert(DataFrame, Matrix) instead in DataFrame at /Users/Matthew/.julia/v0.3/DataFrames/src/deprecated.jl:54 (repeats 2 times)
Is there another method I should be aware of to keep my code consistent?
EDIT:
Julia 0.3.2,
DataFrames 0.5.10
OSX 10.9.5
julia> x=rand(4,4)
4x4 Array{Float64,2}:
0.467882 0.466358 0.28144 0.0151388
0.22354 0.358616 0.669564 0.828768
0.475064 0.187992 0.584741 0.0543435
0.0592643 0.345138 0.704496 0.844822
julia> convert(DataFrame,x)
ERROR: `convert` has no method matching convert(::Type{DataFrame}, ::Array{Float64,2}) in convert at base.jl:13
This works for me:
julia> using DataFrames
julia> x = rand(4, 4)
4x4 Array{Float64,2}:
0.790912 0.0367989 0.425089 0.670121
0.243605 0.62487 0.582498 0.302063
0.785159 0.0083891 0.881153 0.353925
0.618127 0.827093 0.577815 0.488565
julia> convert(DataFrame, x)
4x4 DataFrame
| Row | x1 | x2 | x3 | x4 |
|-----|----------|-----------|----------|----------|
| 1 | 0.790912 | 0.0367989 | 0.425089 | 0.670121 |
| 2 | 0.243605 | 0.62487 | 0.582498 | 0.302063 |
| 3 | 0.785159 | 0.0083891 | 0.881153 | 0.353925 |
| 4 | 0.618127 | 0.827093 | 0.577815 | 0.488565 |
Are you trying something different?
If that doesn't work try posting a bit more code we can help you better.
Since this is the first thing that comes up when you google, for more recent versions of DataFrames.jl, you can just use the DataFrame() function now:
julia> x = rand(4,4)
4×4 Matrix{Float64}:
0.920406 0.738911 0.994401 0.9954
0.18791 0.845132 0.277577 0.231483
0.361269 0.918367 0.793115 0.988914
0.725052 0.962762 0.413111 0.328261
julia> DataFrame(x, :auto)
4×4 DataFrame
Row │ x1 x2 x3 x4
│ Float64 Float64 Float64 Float64
─────┼────────────────────────────────────────
1 │ 0.920406 0.738911 0.994401 0.9954
2 │ 0.18791 0.845132 0.277577 0.231483
3 │ 0.361269 0.918367 0.793115 0.988914
4 │ 0.725052 0.962762 0.413111 0.328261
I've been confounded by the same issue a number of times, and eventually realized the issue is often related to the format of the array, and is easily resolved by simply transposing the array prior to conversion.
In short, I recommend:
julia> convert(DataFrame, x')
# convert a Matrix{Any} with a header row of col name strings to a DataFrame
# e.g. mat2df(["a" "b" "c"; 1 2 3; 4 5 6])
mat2df(mat) = convert(DataFrame,Dict(mat[1,:],
[mat[2:end,i] for i in 1:size(mat,2)]))
# convert a Matrix{Any} (mat) and a list of col name strings (headerstrings)
# to a DataFrame, e.g. matnms2df([1 2 3;4 5 6], ["a","b","c"])
matnms2df(mat, headerstrs) = convert(DataFrame,
Dict(zip(headerstrs,[mat[:,i] for i in 1:size(mat,2)])))
A little late, but with the update to the DataFrame() function, I created a custom function that would take a matrix (e.g. an XLSX imported dataset) and convert it into a DataFrame using the first row as column headers. Saves me a ton of time and, hopefully, it helps you too.
function MatrixToDataFrame(mat)
DF_mat = DataFrame(
mat[2:end, 1:end],
string.(mat[1, 1:end])
)
return DF_mat
end
So I found this online and honestly felt dumb.
using CSV
WhatIWant = DataFrame(WhatIHave)
this was adapted from an R guide, but it works so heck
DataFrame([1 2 3 4; 5 6 7 8; 9 10 11 12], :auto)
This works as per >? DataFrame

Resources