Python Forum
Trying to replace for loops with numpy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to replace for loops with numpy
#2
Maybe can you provide an example in order to give you some advice(s)?

Maybe you can reduce the number of loop(s) (only onbe one per time step typically), and you 'll be able to speedup your calculation for sure. An example, using append is very costly (dynamic memory allocation = a new matrix is created for each append call, to add a new row) and you can easily avoid it either by creating a matrix, or by adding a column a matrix; numpy is implicitly vertorized and it's fast if it's used correctly.

Share a bit more and the community will help you
(I've demonstrated to an intern that it's possible to reduce the duration from more than an hour to 40 seconds Wink - 3 nested loops were used against no one finally; it depends on the topic).

here after a basic example from the numpy arrays/matrixes you get:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np
import time

n = 1_000_000;

# elt1 and elt2 are supposed to be 2 matrixes; the dimensions are (n,4)
# column 1 = number of element
#column 2/3/4 = X/Y/Z coordinates

## Matrixes build-up; the first column are the elements number
elt1 = np.random.random((n,4));
elt2 = np.random.random((n,4));

i = np.arange(1,n+1, dtype = np.float);
elt1[:,0] = i;
elt2[:,0] = i;

# now the dimension is supposed to be unknown
t0 = time.time();
nr = np.shape(elt1)[0]; # nr = number of rows
dist = np.sqrt( (elt2[:, 1] - elt1[:, 1])**2 + (elt2[:, 2] - elt1[:, 2])**2 + (elt2[:, 3] - elt1[:, 3])**2 )
t1 = time.time();
loc_minimum = np.where(dist == min(dist));
minimum = float(dist[loc_minimum]);
print("The minimum is {} and located at the row number {}".format(minimum,int(loc_minimum[0])));
print("Duration : {}".format(t1-t0));
Paul
Reply


Messages In This Thread
RE: Trying to replace for loops with numpy - by paul18fr - Jul-30-2019, 06:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 637 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,643 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  replace sets of values in an array without using loops paul18fr 7 1,763 Jun-20-2022, 08:15 PM
Last Post: paul18fr
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 3,010 Apr-09-2019, 04:54 PM
Last Post: PhysChem

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020