I have a numpy array that looks like the following:
np.array([
[23, 12, 4, 103, 87, 0.6],
[32, 18, 3, 120, 70, 0.6],
[43, 12, 8, 109, 89, 0.4],
[20, 13, 7, 111, 77, 0.8]
])
I want to transform this array where the last column becomes its own array, such that it will look like this:
np.array([
[[23, 12, 4, 103, 87], [0.6]],
[[32, 18, 3, 120, 70], [0.6]],
[[43, 12, 8, 109, 89], [0.4]],
[[20, 13, 7, 111, 77], [0.8]]
])
What would be the best way to go about this? I am relatively new to Python and have tried out some loops but to no avail. Thanks!
1 Answer 1
numpy requires consistent dimensions in its array; that would give two different sizes. You can either use two separate variables (i.e. parallel arrays):
X = data[:, :-1]
y = data[:, -1]
X = np.array([
[23, 12, 4, 103, 87],
[32, 18, 3, 120, 70],
[43, 12, 8, 109, 89],
[20, 13, 7, 111, 77],
])
y = np.array([
0.6, 0.6, 0.4, 0.8
])
Or you can store a list of pairs:
my_list = [(row[:-1], [row[-1]]) for row in data]
my_list = [
([23, 12, 4, 103, 87], [0.6]),
([32, 18, 3, 120, 70], [0.6]),
([43, 12, 8, 109, 89], [0.4]),
([20, 13, 7, 111, 77], [0.8])
]
The best strategy depends on your use case.
3 Comments
Explore related questions
See similar questions with these tags.
list, not anp.array