How to use numpy's hstack? - python

I have one large numpy.ndarray array that I want to extract the 4th and 5th columns out of and put those columns into a 2D array. The [i,0] element should be the value on the 4th column and [i,1] should be the element from the 5th column.
I trying to use the numpy.hstack function to do this.
a = numpy.asarray([1, 2, 3, 4, 5])
for i in range(5):
a = numpy.vstack([a, numpy.asarray([1, 2, 3, 4, 5])])
combined = np.hstack([a[:,3], a[:,4]])
However, this simply gives me an nx1 array. I have tried multiple approaches using concatenate that look like these examples:
combined = np.concatenate([a[:,3], a[:,4]])
combined = np.concatenate([a[:,3], a[:,4]], axis=1)
combined = np.concatenate([a[:,3].T, a[:,4].T])
I feel like hstack is the function I want, but I can't seem to figure out how to make it give me an nx2 array. Can anyone point me in the right direction? Any help is appreciated.

Just slice out your data as follows:
X = [[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]]
slicedX = X[:,3:5]
results in:
[[3 4]
[3 4]
[3 4]
[3 4]]

I think this will do what you want:
a[:,[3,4]]

You can also use zip:
>>> c = numpy.array( zip( a[:, 3], a[:, 4]) )
>>> c
array([[4, 5],
[4, 5],
[4, 5],
[4, 5],
[4, 5],
[4, 5]])

Related

How to split an two-dimension array by the second dimension?(Python)

I have an array of points, and I want to split these into two arrays by the second dimension:
points_right = points[points[:, 0] > p0[0]]
points_left = points[points[:, 0] < p0[0]]
how can I split these points in one loop?
I think np.split is what you're looking for, just use axis=1.
Example splitting a 2x4 matrix:
import numpy as np
pts = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
left_pts, right_pts = np.split(pts, indices_or_sections=2, axis=1)
The original matrix (pts):
[[1 2 3 4]
[5 6 7 8]]
left_pts:
[[1 2]
[5 6]]
right_pts:
[[3 4]
[7 8]]
https://numpy.org/doc/stable/reference/generated/numpy.split.html

Output of list.append prints word "array" after each iteration

I want to make a list in python, consisting of different entries. Each entry to the list occurs a different number of times:
import numpy as np
frequency = (1, 2, 1, 2, 1)
numbers = np.random.choice(9, size = (5, 3), replace=True)
list = []
for i in range(0, 5):
list.append(np.tile(numbers[i], (int(frequency[i]), 1)))
print(list)
This works fine. However, when I look at the output (print(list)), I get the following:
[array([[1, 8, 1]]), array([[2, 8, 0],
[2, 8, 0]]), array([[6, 8, 6]]), array([[2, 1, 8],
[2, 1, 8]]), array([[4, 6, 1]])]
Why does it include the word "array" for every iteration? I would just like to have a list like this:
[[1 8 1]
[2 8 2]
[2 8 2]
[6 8 6]
[2 1 8]
[2 1 8]
[4 6 1]]
Because you are using two different types of data - classic python list and numpy list. When you are printing an classic list it will show you [a, b, c], but for numpy list it will show you an array([a, b, c])
Why does it include the word "array" for every iteration?
You have list of numpy.arrays as numpy.tile function does return numpy.array.
I would just like to have a list like this:
Desired output suggest that you are looking for single 2D array, which could be created from list of 1D arrays using numpy.vstack function. Consider following example:
import numpy as np
mylist = [np.array([1,2,3]),np.array([4,5,6]),np.array([7,8,9])]
myarray = np.vstack(mylist)
print(myarray)
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]
As side note: please do not python built-ins function names as variables

How to insert tensor array into tensor matrix after every second position

I have a tensor array a and a tensor matrix m. Now I want to insert a into m after every second position started at index 0 ending with len(m)-2. Let's make an equivalent example using numpy and plain python:
# define m
m = np.array([[3,7,6],[4,3,1],[8,4,2],[2,8,7]])
print(m)
#[[3 7 6]
# [4 3 1]
# [8 4 2]
# [2 8 7]]
# define a
a = np.array([1,2,3])
#[1 2 3]
# insert a into m
result = []
for i in range(len(m)):
result.append(a)
result.append(m[i])
print(np.array(result))
#[[1 2 3]
# [3 7 6]
# [1 2 3]
# [4 3 1]
# [1 2 3]
# [8 4 2]
# [1 2 3]
# [2 8 7]]
I am looking for a solution in tensorflow. I am convinced that there is a solution that doesn't need a loop but I am not able to find one. I hope someone can help me out with this!
You can concatenate your target vector at the beginning of each line of your matrix, and then reshape it.
import tensorflow as tf
initial_array = tf.constant([
[3, 7, 6],
[4, 3, 1],
[8, 4, 2],
[2, 8, 7],
])
vector_to_add = [1, 2, 3]
concat = tf.concat([[vector_to_add] * initial_array.shape[0], initial_array], axis=1) # Concatenate vector_to_add to each vector of initial_array
output = tf.reshape(concat, (2 * initial_array.shape[0], initial_array.shape[1])) # Reshape
This should work,
np.ravel(np.column_stack((m, np.tile(a, (4, 1))))).reshape(8, 3)
For idea, please refer to Interweaving two numpy arrays. Apply any solution described there, and reshape.

How to assign values to indexed arrays

array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
I want to get
[1,2
4,5
7,8]
ndarray[:][0:2]
it get
array([[1, 2, 3],
[4, 5, 6]])
why!?
ndarray[:] returns an identical array and when you use [0:2], it returns an arry with the first 2 elements, and hence [[1,2,3],[4,5,6]]
What you want to do is this : ndarray[0:3,0:2] or simpler ndarray[:,:2]
This will return a slice of the array slicing in 2 dimensions
ndarray[:] gives you the whole array, and with the following [0:2] you select the first two element of it, which is [1,2,3] and [4,5,6].
You need to slice (as DavidG already suggested) in the first bracket for all desired dimensions:
ndarray[:,0:2]
P.S.: Please change your title to something, that relates closer to your problem.
Your code:
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print(a[:])
Output: same as a, as you are selecting all of the rows:
[[1 2 3] [4 5 6] [7 8 9]]
Then indexing on that will apply the index on the result, all of the rows (i.e. a[:][0:2] is equivalent to a[0:2] - selecting the first two rows)
To select the first two columns:
print(a[:, 0:2])
Output (as expected):
[[1 2] [4 5] [7 8]]
I would recommend going through Numpy's indexing documentation: https://docs.scipy.org/doc/numpy/user/basics.indexing.html

How to append a "label" to a numpy array

I have a numpy array created such as:
x = np.array([[1,2,3,4],[5,6,7,8]])
y = np.asarray([x])
which prints out
x=[[1 2 3 4]
[5 6 7 8]]
y=[[[1 2 3 4]
[5 6 7 8]]]
What I would like is an array such as
[0 [[1 2 3 4]
[5 6 7 8]]]
What's the easiest way to go about this?
Thanks!
To do what you're asking, just use the phrase
labeledArray = [0, x]
This way, you will get a standard list with 0 as the first element and a Numpy array as the second element.
However, in practice, you are probably trying to label for the purpose of later recall. In that case, I'd recommend you use a dictionary, as it is less confusing to keep track of:
myArrays = {}
myArrays[0] = x
Which can be used as follows:
>>> myArrays
{0: array([[1, 2, 3, 4],
[5, 6, 7, 8]])}
>>> myArrays[0]
array([[1, 2, 3, 4],
[5, 6, 7, 8]])

Categories