concatenate numpy 1D array in columns - arrays

I have two numpy arrays:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
and I want to concatenate them into two columns like,
1 4
2 5
3 6
is there any way to do this without transposing or reshaping the arrays?

You can try:
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.concatenate((a[np.newaxis, :], b[np.newaxis, :]), axis = 0).T
And you get :
c = array([[1, 4],
[2, 5],
[3, 6]])
Best,

Related

Creating matrix with the same vector in each row

I have p = [1,2,3,4]. I would like a 100x4 numpy matrix with p in each row. What's the best way to create that?
I tried pvect = np.array(p for i in range(10)) but that doesn't seem to be right.
Use numpy.tile:
pvect = np.tile(p, (100, 1))
output:
array([[1, 2, 3, 4],
[1, 2, 3, 4],
...
[1, 2, 3, 4],
[1, 2, 3, 4]])
Using matrix algebra: the multiplication of a column vector of ones times your row vector p effectively places the vector p in each row.
p = np.array([[1,2,3,4]])
OutputArray = np.ones((100, 1)) # p
you can try:
pvect = np.array(p*100).reshape((100,4))

Understanding np.dot multiplication example

If
X = np.array([[1, 1],[1, 2],[2, 2],[2, 3]])
and you do np.dot(X, np.array([1,2])), how does that multiply to become array([3, 5, 6, 8])?
I know X has a shape of (4,2) and the second array is (2,) (because it's 2 columns and it's 1D). I also know there is a special case from https://numpy.org/doc/stable/reference/generated/numpy.dot.html:
"If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b."
But I can't seem to apply it correctly to get the result.
In [376]: X = np.array([[1, 1],[1, 2],[2, 2],[2, 3]])
In [378]: y=np.array([1,2])
In [379]: X.shape
Out[379]: (4, 2)
In [380]: y.shape
Out[380]: (2,)
the dot/matmul approach:
In [381]: X#y
Out[381]: array([3, 5, 6, 8])
einsum lets us specify which axes combine how:
In [382]: np.einsum('ij,j->i',X,y)
Out[382]: array([3, 5, 6, 8])
elementwise multipication followed by sum. The (4,2) * (2,) -> (4,2)
In [383]: X*y
Out[383]:
array([[1, 2],
[1, 4],
[2, 4],
[2, 6]])
In [384]: (X*y).sum(1)
Out[384]: array([3, 5, 6, 8])
Or by highschool math, each row of X times y, summed
In [386]: [sum(row*y) for row in X]
Out[386]: [3, 5, 6, 8]

Slicing the last 2 columns of a numpy array

How can I slice the last 2 columns of a numpy array?
for example:
A = np.array([[1, 2, 3], [4, 5, 6]])
and I want to get B as the last 2 columns of A which is [[2, 3], [5, 6]]
I know that I can index it from start of the array such as B = A[:, 1:3]. But I am looking for a general form to slice A by indexing from the end as the number of columns for A changes in my case.
Here you go
>>> A = np.array([[1, 2, 3],[4, 5, 6]])
>>> A[:,[-2,-1]]
array([[2, 3],
[5, 6]])
A generalised method to get the last n rows can be
>>> A = np.array([[1, 2, 3,4],[4, 5, 6,7]])
>>> A[:,-3:]
array([[2, 3, 4],
[5, 6, 7]])

concatenate multiple numpy arrays in one array?

Assume I have many numpy array:
a = ([1,2,3,4,5])
b = ([2,3,4,5,6])
c = ([3,4,5,6,7])
and I want to generate a new 2-D array:
d = ([[1,2,3,4,5],[2,3,4,5,6],[3,4,5,6,7]])
What should I code?
I tried used:
d = np.concatenate((a,b),axis=0)
d = np.concatenate((d,c),axis=0)
It returns:
d = ([1,2,3,4,5,2,3,4,5,6,3,4,5,6,7])
As mentioned in the comments you could just use the np.array function:
>>> import numpy as np
>>> a = ([1,2,3,4,5])
>>> b = ([2,3,4,5,6])
>>> c = ([3,4,5,6,7])
>>> np.array([a, b, c])
array([[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7]])
In the general case that you want to stack based on a "not-yet-existing" dimension, you can also use np.stack:
>>> np.stack([a, b, c], axis=0)
array([[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7]])
>>> np.stack([a, b, c], axis=1) # not what you want, this is only to show what is possible
array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6],
[5, 6, 7]])

Sum the elements of two equal count arrays [duplicate]

Is there a concise way in Swift of creating an array by applying a binary operation on the elements of two other arrays?
For example:
let a = [1, 2, 3]
let b = [4, 5, 6]
let c = (0..<3).map{a[$0]+b[$0]} // c = [5, 7, 9]
If you use zip to combine the elements, you can refer to + with just +:
let a = [1, 2, 3]
let b = [4, 5, 6]
let c = zip(a, b).map(+) // [5, 7, 9]
Update:
You can use indices like this:
for index in a.indices{
sum.append(a[index] + b[index])
}
print(sum)// [5, 7, 9]
(Thanks to Alexander's comment this is better, because we don't have to deal with the element itself and we just deal with the index)
Old answer:
you can enumerate to get the index:
var sum = [Int]()
for (index, _) in a.enumerated(){
sum.append(a[index] + b[index])
}
print(sum)// [5, 7, 9]

Resources