Extracting columns of multidimensional arrays without using numpy

Asked 1 years ago, Updated 1 years ago, 73 views

The code fits in about one line without using numpy and is fast
I would like to extract columns.

It's also
https://x1.inkenkun.com/archives/861
I've looked into this article and many other things, but
Basic calculations such as insertion and deletion of arrays and elements are
It seems that the speed is faster without numpy.
The code is easy to read when np.dot or np.exp(-x).

If you can't extract columns without using numpy
I wonder if we have no choice but to add elements to the new array one by one in the for statement. I'm thinking about it.

Even if it's not a solution, I'd like to have your own opinions.
Thank you for your cooperation.

python python3 numpy

2022-09-30 19:56

2 Answers

Using the jupyter notebook, you can easily measure the execution time of the code.
Using the number array is much faster.

n=1000000

%timeit xs = [i for i in range(n)]
10 loops, best of 3: 70.3 ms per loop

%timeit xs = [None]*n
100 loops, best of 3: 3.49 ms per loop

import numpy as np

%timeita=np.range(n)
1000 loops, best of 3: 1.08 ms per loop

%timeita=np.zero(n)
The slowest runbook 5.87 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3:456 ss per loop

If you are having trouble installing the jupyter notebook, you can use Google Colabotry for free and out of the box.


2022-09-30 19:56

It is common to use numpy's ndarray as an array.
If you do not use numpy, the nested list represents an array.

I think cytoolz is fast when processing Python basic objects.
For example:

import cytoolz
a = [[1,2,3], 
     [4, 5, 6], 
     [7, 8, 9], 
     [11, 12, 13]]
list(cytoolz.pluck(1,a))
[2, 5, 8, 12]

list(cytoolz.pluck([1,2],a))
[(2, 3), (5, 6), (8, 9), (12, 13)]


2022-09-30 19:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.