Python Forum
Convert indexing For Loop from MATLAB (uses numpy and pandas)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert indexing For Loop from MATLAB (uses numpy and pandas)
#1
Hello,

I am trying to convert a program to Python (using numpy and pandas as well) from MATLAB. The input is a (n by 2) array called scaletestmatrix (in this case a 6x2 array). Column one is the x values, which contains some values more than once. Column two is the y values. I need to find the largest and smallest y values that corresponds to each x value, then calculate the difference between the two y values for each x value, and finally find the mean of the differences.

The MATLAB code looks like this:
Output:
[hr] data = importdata('C:\\Users\\...\\scaletestmatrix.csv'); xvalues = data(:,1); %%% Extract column 1 of the data as xvalues yvalues = data(:,2); %%% Extract column 2 of the data as yvalues smallest = min(xvalues); %%% Finds the smallest xvalue biggest = max(xvalues); %%% Finds the largest xvalue length = (biggest - smallest + 1); %%% Finds number of xvalues to evaluate in the impending For Loop startvalue = smallest %%% Set the starting x value of the impending For Loop %%% During each loop we find the indices for all of the xvalues equal to startvalue (which increases by 1 each loop). %%% Then we find the minimum and maximum yvalue that corresponds to the set of indices obtained during that loop. %%% We then assign those values to finaly1values(y) and finaly2values(y) where y is the index specified by the loop number. for y=1:length %%% Set the number of loops to run finaly1values(y) = min(yvalues(xvalues == startvalue)) finaly2values(y) = max(yvalues(xvalues == startvalue)) startvalue = (startvalue +1); end
thickness = (finaly2values - finaly1values) %%% Creates a 1D array of the difference between the largest and smallest yvalues that correspond to each xvalue

meanthickness = (mean(thickness))%%% Finds the average of the thickness array

The input array (scaletestmatrix.csv) is:
Output:
5, 20, 5, 30, 7, 25, 7, 15, 7, 30, 9, 50, 9, 20, 9, 35, 6, 25, 6, 35, 10, 33, 10, 23, 10, 13, 8, 17, 8, 15, 8, 16, [hr]
The finaly1values and finaly2values are:

Output:
finaly1values, finaly2values, 20, 30, 25, 35, 15, 30, 15, 17, 20, 50, 13, 33, [hr]
Which results in: meanthickness = 14.5

This is what I have before the For Loop in Python:

import pandas as pd
import numpy as np
filename = 'C:\\Users\\...\\scaletestmatrix.csv'
data = pd.read_csv(filename, header = None);  ### Read the data file
data = data.values ;  ### Convert DataFrame to array of values (numbers)
data = data.astype(np.float); ### Convert from scientific notation to floating

xvalues = data[:,0]; ### Extract column 1 of the data as xvalues
yvalues = data[:,1]; ### Extract column 2 of the data as yvalues

smallest = min(xvalues); ### Find the smallest xvalue, his may not be necessary in Python
biggest= max(xvalues);  ### Find the largest xvalue, this may not be necessary in Python

startvalue = smallest ### This may not be necessary in Python.

length = int((biggest - smallest) + 1); ### This may not be necessary in Python
This is where I am stuck. Thank you in advance for your help.
Reply
#2
Hi,

If you had an array like:

5, 20,
5, 30,
7, 25,
7, 15,
7, 30,
9, 50,
9, 20,
9, 35,
6, 25,
6, 35,
10, 33,
10, 23,
10, 13,
8, 17,
8, 15,
8, 16,


How would you you find the biggest value from column 2 for each number in column 1 at the same time? E.g. for 7 the biggest value is 30.

Thanks!
Reply
#3
Seems fairly easy with a dict, right? The key is whatever's on the left, and the value is the max value.

data = [ ] # I have no idea where this is coming from

import collections
max_values = collections.defaultdict(int)

for pair in data:
    key, value = pair
    if value > max_values[key]:
        max_values[key] = value

print(max_values)
Reply
#4
That is much cleaner than the way I just figured out! I need the minimum value too (and then the mean of the difference between max and min). I tried tweaking your code by switching '>' to '<', but it returned all zeros.

data = [ ] # I have no idea where this is coming from
 
import collections
max_values = collections.defaultdict(int)
 
for pair in data:
    key, value = pair
    if value > max_values[key]:
        max_values[key] = value

#### My attempt at minimum value
       
min_values = collections.defaultdict(int)
 
for pair2 in data:
    key2, value2 = pair2
    if value2 < min_values[key2]:
        min_values[key2] = value2
 
print(max_values)
print(min_values)
The way I clunkily did it:
xvalues = data[:,0]; ### Extract column 1 of the data as xvalues
yvalues = data[:,1]; ### Extract column 2 of the data as yvalues

xmin = min(xvalues) ### Find smallest x value
xmax = max(xvalues) ### Find largest x value

thickArray = [] ### Create empty array thickArray

itt = xmin  ## Set starting value for iteration

while itt < (xmax+1): # first while loop code
  i = np.where(xvalues==itt) ### Gets indicies of xvalues = itt for each loop
  i = np.asarray(i) ### Converts i to array
  i = (yvalues[i])  ### Gets values for indices as array
  size = np.shape(i) ### Returns array dimensions. Not Used
  i = max(i)  ### Idk. Reduces to [] from [[]]
  maxi = max(i) ### Finds max of array
  mini = min(i) ### Finds min of array
  diff = maxi - mini ### Find difference between max and min
  thickArray.append(diff) ### Adds diff for that iteration to thickArray
  itt = itt + 1 ### Increment iteration by 1

FinalThickness1 = np.mean(thickArray) ### Find the mean of thickArray
Thank you for your advice! I've only been using Python for a few days, and I am not at all familiar with all the commands/functions/etc.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 294 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Pandas dataframes and numpy arrays bytecrunch 1 1,292 Oct-11-2022, 08:08 PM
Last Post: Larz60+
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,530 Jul-19-2022, 06:31 AM
Last Post: paul18fr
Question about Numpy indexing. water 1 1,409 Jan-18-2022, 09:52 PM
Last Post: paul18fr
  change for loop into numpy functions ToffiFaye 1 1,943 Feb-06-2021, 11:38 AM
Last Post: paul18fr
  Python PDF merging from an excel pandas for loop siraero 0 2,164 Aug-16-2020, 09:34 AM
Last Post: siraero
  How to compare two json and write to third json differences with pandas and numpy onenessboy 0 4,638 Jul-24-2020, 01:56 PM
Last Post: onenessboy
  What is the mechanism of numpy function returning pandas object? Ibaraki 2 2,454 Apr-04-2020, 10:57 PM
Last Post: Ibaraki
  Python Pandas for loop/while loop question mrashy 1 3,836 Mar-24-2020, 04:39 AM
Last Post: deanhystad
  convert 'A B C' to numpy float matrix rezabma 4 2,491 Feb-27-2020, 09:48 AM
Last Post: rezabma

Forum Jump:

User Panel Messages

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