This is an easy question but say I have an MxN matrix. All I want to do is extract specific columns and store them in another numpy array but I get invalid syntax errors. Here is the code:
JavaScript
x
2
1
extractedData = data[[:,1],[:,9]].
2
It seems like the above line should suffice but I guess not. I looked around but couldn’t find anything syntax wise regarding this specific scenario.
Advertisement
Answer
I assume you wanted columns 1
and 9
?
To select multiple columns at once, use
JavaScript
1
2
1
X = data[:, [1, 9]]
2
To select one at a time, use
JavaScript
1
2
1
x, y = data[:, 1], data[:, 9]
2
With names:
JavaScript
1
2
1
data[:, ['Column Name1','Column Name2']]
2
You can get the names from data.dtype.names
…