How can I add 1 in the beginning of a numpy array? - arrays

I have a numpy array X = [[3 4 5 6] [6 5 3 3] [9 8 5 2]]
I would like to add 1 in each array like so:
X = [[1 3 4 5 6] [1 6 5 3 3] [1 9 8 5 2]]
I wanted to do it using np.ones() and np.hstack()
This is what I tried to do
X = [[3 4 5 6] [6 5 3 3] [9 8 5 2]]
ones = np.ones(len(X))
X = np.hstack((ones, X))

Related

Slice matrix every 2 columns

I am trying to figure out how to slice a matrix every 2 columns. For example, if we have
A=[[1 2 3 4 5 6];[7 8 9 10 11 12]]
2×6 Matrix{Int64}:
1 2 3 4 5 6
7 8 9 10 11 12
then I would like to return 3 matrices sliced every 2 columns, i.e. a 3 dimensional array with 3 matrices,
2×2×3 Array{Int64, 3}:
[:, :, 1] =
1 2
7 8
[:, :, 2] =
3 4
9 10
[:, :, 3] =
5 6
11 12
One thing I have tried is the eachslice function,
collect(eachslice(A,dims=2))
6-element Vector{SubArray{Int64, 1, Matrix{Int64}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, true}}:
[1, 7]
[2, 8]
[3, 9]
[4, 10]
[5, 11]
[6, 12]
but this slices along every column, but I want to slice along every two columns!
Thanks in advance.
That's just a reshape. Reshapes share memory (like views), so you should copy it if you want it to be independent of the original A:
julia> reshape(A, 2, 2, 3)
2×2×3 Array{Int64, 3}:
[:, :, 1] =
1 2
7 8
[:, :, 2] =
3 4
9 10
[:, :, 3] =
5 6
11 12
But if you really want, you can express it as indexing, too:
julia> A[:, [1:2 3:4 5:6]]
2×2×3 Array{Int64, 3}:
[:, :, 1] =
1 2
7 8
[:, :, 2] =
3 4
9 10
[:, :, 3] =
5 6
11 12

Converting pandas data frame into numpy ndarray [duplicate]

This question already has answers here:
Convert pandas dataframe to NumPy array
(15 answers)
Closed 4 years ago.
I am using a pandas data frame to clean and process data. However, I need to then convert it into a numpy ndarray in order to use exploit matrix multiplication. I turn the data frame into a list of lists with the following:
x = df.tolist()
This returns the following structure:
[[1, 2], [3, 4], [5, 6], [7, 8] ...]
I then convert it into a numpy array like this:
x = np.array(x)
However, the following print:
print(type(x))
print(type(x[0]))
gives this result:
'numpy.ndarray'
'numpy.float64'
However, I need them both to be numpy arrays. If it's not from a pandas data frame and I just convert a hard-coded list of lists then they are both ndarrays. How do I get the list, and the lists in that list to be ndarrays when that list has been made from a data frame? Many thanks for reading, this has had me stumped for hours.
I think you need values:
df = pd.DataFrame({'C':[7,8,9,4,2,3],
'D':[1,3,5,7,1,0]})
print (df)
C D
0 7 1
1 8 3
2 9 5
3 4 7
4 2 1
5 3 0
x = df.values
print (x)
[[7 1]
[8 3]
[9 5]
[4 7]
[2 1]
[3 0]]
And then select by indexing:
print (x[:,0])
[7 8 9 4 2 3]
print (x[:,1])
[1 3 5 7 1 0]
print (type(x[:,0]))
<class 'numpy.ndarray'>
Also is possible transpose array:
x = df.values.T
print (x)
[[7 8 9 4 2 3]
[1 3 5 7 1 0]]
print (x[0])
[7 8 9 4 2 3]
print (x[1])
[1 3 5 7 1 0]
How about as_matrix:
x = df.as_matrix()
You may want to try df.get_values(), and eventually np.reshape it.

how to convert from Array{Array{Int64,2},1} to Array{Int64,2}

In Julia, I want to convert data defined as Vector of 2D array to a 2D array of Matrix.
As described in the following example, I want to convert data s into data t, but I have not been successful so far.
How should I deal with the case?
julia> s = [[1 2 3], [4 5 6], [7 8 9]]
3-element Array{Array{Int64,2},1}:
[1 2 3]
[4 5 6]
[7 8 9]
julia> t = [[1 2 3]; [4 5 6]; [7 8 9]]
3××3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
julia> s |> typeof
Array{Array{Int64,2},1}
julia> t |> typeof
Array{Int64,2}
julia> convert(Array{Int64, 2}, s)
ERROR: MethodError: Cannot `convert` an object of type Array{Array{Int64,2},1} to an object of type Array{Int64,2}
This may have arisen from a call to the constructor Array{Int64,2}(...),
since type constructors fall back to convert methods.
julia> reshape(s, 3, 3)
ERROR: DimensionMismatch("new dimensions (3,3) must be consistent with array size 3")
in reshape(::Array{Array{Int64,2},1}, ::Tuple{Int64,Int64}) at .\array.jl:113
in reshape(::Array{Array{Int64,2},1}, ::Int64, ::Int64, ::Vararg{Int64,N}) at .\reshapedarray.jl:39
As in the following example, if you define 2D array or 1D array as source data, I can then reshape them successfully into a 2D array of Matrix.
julia> u = [1 2 3 4 5 6 7 8 9]
1××9 Array{Int64,2}:
1 2 3 4 5 6 7 8 9
julia> u |> typeof
Array{Int64,2}
julia> reshape(u, 3, 3)
3××3 Array{Int64,2}:
1 4 7
2 5 8
3 6 9
julia> v = [1, 2, 3, 4, 5, 6, 7, 8, 9]
9-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9
julia> v |> typeof
Array{Int64,1}
julia> reshape(v, 3, 3)
3××3 Array{Int64,2}:
1 4 7
2 5 8
3 6 9
You can use vcat and splatting
julia> t = vcat(s...)
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9

How to convert a cell array of struct to a cell array

I have a cell array of such:
data{1}.field1 = [1 2 3 4 5 6];
data{2}.field1 = [7 8 9 10];
...
data{1}.field2 = {[1 2 3]; [4 5 6]};
data{2}.field2 = {[7 8 9]};
...
I there a simple trick to convert that so I have:
field1 = {[1 2 3 4 5 6], [7 8 9 10], ...}
field2 = {{[1 2 3];[4 5 6]}, {[7 8 9]}, ...}
Ok I've found a solution that seem to work
tmp = [data{:}];
field1 = {tmp.field1};
field2 = {tmp.field2};`

How to plot each column of a cell array?

I have a cell array that looks like this:
Column1 Column2
[1 2 3 4] [2 5 6 9]
[1 3 4] [3 4 7 8]
[2 3 4] [1 3 7 9]
[1 2 4] [1 4 6 8]
There are a few more columns that have similar styles of data. I need to create a way to make a graph of each column (separate graphs for each column of the array), that plots each point as a number from each double as the x-coordinate, and the row as the y-coordinate. It should look something like this:
(Row)
1 x x x x
2 x x x
3 x x x
4 x x x
1 2 3 4
X is just a point on the graph.
Does this make enough sense? I feel like I'm making 0 progress in explaining what I want. If anyone doesn't understand this, feel free to ask questions and I'll answer them as best I can.
Something like this?
cin = { {[1 2 3 4] , [1 3 4], [2 3 4], [1 2 4]}, {[1 2 3 8] , [1 3 4], [2 3 4], [1 2 4]} };
for k=1:numel(cin)
col_k = cin{k};
figure(); %// 1 figure per column
for y=1:numel(col_k)
plot(col_k{y}, y);
hold on;
end
end

Resources