0

I am new to python. The code below creates a range of numerical values based on inputs from a csv data set, for each key in newprobValues. I would like to be able to identify which values were created via each arange() function, by assigning each new numerical values another value (i.e. 0, 1, 2)

Right now, all of the new numerical values are aggregated into one list, and I'm pulling numbers randomly out of that list and deleting them after use (in a psychology PsychoPy task). Data from the task is output into another csv file, and i would like to have a column of 0s, 1s, and 2s next to the column of new numerical values so that I can parse them for analysis.

for example: each value to come out of newprobValues1 = np.arange(low1_p, high1_p, step = SV) should have a second value of 1 associated it, so that I can output those values to a separate column in a csv file.

code:

newprobValues = {.17:[],
         .28:[],
         .54:[],
         .84:[],
         .96:[],
         .99:[],}

for npv in newprobValues: 
    mu = probs.ix[(probs['DDPD']==npv),['Value']]
    SV = hyperlaw(((1-npv)/npv), k_prob) #yields subjective value of one dollar at given delay
    mu = float(mu['Value'])
    generating_values_prob[npv] = {'mu':mu, 'SV':SV}
    if mu >= 25.50:
        #set desired ran
        low1_p, high1_p = float(mu-(4*SV)), float(mu-.01) # -.01 is hack to prevent arange() from      rounding uneven ceil() up to a fifth iteration
        low2_p, high2_p = float(mu+SV), float(mu+(5*SV)-.01) 
        low3_p, high3_p = 20.5, 80
        newprobValues1 = np.arange(low1_p, high1_p, step = SV) #generate hard values below indiff point
        newprobValues2 = np.arange(low2_p, high2_p, step = SV) #generate hard values above indiff point
        newprobValues3 = np.arange(low3_p, high3_p, step = 6.612)**
        newprobValues[npv] = np.concatenate([newprobValues1, newprobValues2, newprobValues3])
        newprobValues[npv] = np.insert(newprobValues[npv], 0, mu)
        newprobValues[npv] = newprobValues[npv].tolist()

I have tried the code:

    newprobValues1 = {'value': np.arange(low1_p, high1_p, step = SV), 'type': 0}

but I get a dictionary with 0 in it, when I really need 0 to be assigned to each number -- so that when I concatenate shuffle the arrays I can still identify the arange() function that generated each value

6
  • I find it very hard to understand what exactly you would like to do. It may help if you create a very simple dummy data frame probs for us and tell us, what output you expect for this data frame. Commented Dec 28, 2014 at 16:07
  • Can you change the data structure of newprobValues[npv]? You can separate the values within the data structure if you use np.vstack instead of np.concatenate. Then it should be easy to extract them later. Is mu a scaler? Commented Dec 28, 2014 at 16:45
  • @wwii mu is a scalar. using np.vstack, do you think it would be possible to identify which array a number is in? That is, when I pull and delete a number from one of the a arrays in the stack, how would I also be able to get info on which array in the stack it was pulled from? e.g. array([[1, 2, 3], [4, 5, 6]]), and I pull out 6, is it possible to also identify that 6 came from the second array? Commented Dec 29, 2014 at 12:19
  • @cel sorry for any confusion. in general, i would like to create new scalars using the arange() function, save those new numbers to a dictionary, later pull out and delete those numbers from the dictionary (for use in an experimental task) -- importantly, while also being able to identify which arange() function generated the number. Commented Dec 29, 2014 at 12:22
  • @cel part of the issue is that I'm not sure what the best approach is. Wwii's vstack tip, sounds promising, but I also wonder if it would work to create a list of tuples (i, j), where i of the tuple is the scalar returned by the arange() function, and the j identifies the arange() function in which scalar was generated. for example: (4, 0), the 4 was generated by the arange() function, and the 0 means the 4 was generated in the arange() function. please let me know if example code would be more helpful. thank you for taking the time to look this over! Commented Dec 29, 2014 at 12:25

2 Answers 2

1

I see that the last thing you do with the newly computed ndarray is using its tolist() method.

# last line of your code
newprobValues[npv] = newprobValues[npv].tolist()

If what you want is a list, you needn't go through the hoops of numpy... a more natural approach would be

newprobValues[npv].append(mu)
newprobValues[npv].append(newprobValues1)
newprobValues[npv].append(newprobValues2)
newprobValues[npv].append(newprobValues3)

later, when you want to use the sequence number i you can do

result  = use(newprobValues[npv][i])

or, using list unpacking

for npv in newprobValues:
    mu, seq1, seq2, seq3 = newprobValues[npv]
    ...
Sign up to request clarification or add additional context in comments.

2 Comments

Or .append(newprobValuesX.tolist()) if that is the desired container.
@wwii Thank you for the clarification, I hope that the OP can decide which approach to choose
0

I'm not sure this is what you want, but the built-in enumerate() function will generate pairs of values, the first member of each pair being an index. So, for example, enumerate([12, 35, 17) will produce the sequence (0, 12), (1, 35), (2, 17).

Would it work for you to change to for loop to read

for index, npv in enumerate(newprobValues):
    ...

    newprobValues1 = {'value': np.arange(low1_p, high1_p, step = SV), 'type': index}

I wonder?

1 Comment

I think she wants to enumerate newprobValues1, newprobValues2, and newprobValues3. Which could be done by making a list of (lowx_p, highx_p) tuples and iterating over it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.