![]() |
Convert indexing For Loop from MATLAB (uses numpy and pandas) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Convert indexing For Loop from MATLAB (uses numpy and pandas) (/thread-9070.html) |
Convert indexing For Loop from MATLAB (uses numpy and pandas) - bentaz - Mar-19-2018 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: thickness = (finaly2values - finaly1values) %%% Creates a 1D array of the difference between the largest and smallest yvalues that correspond to each xvaluemeanthickness = (mean(thickness))%%% Finds the average of the thickness array The input array (scaletestmatrix.csv) is: The finaly1values and finaly2values are: Which results in: meanthickness = 14.5This 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 PythonThis is where I am stuck. Thank you in advance for your help. Max values from index - bentaz - Mar-20-2018 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! RE: Convert indexing For Loop from MATLAB (uses numpy and pandas) - nilamo - Mar-20-2018 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) RE: Convert indexing For Loop from MATLAB (uses numpy and pandas) - bentaz - Mar-20-2018 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 thickArrayThank 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. |