Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Axtraxia

New Coder
What am trying to do is in each task I have elements and am going to randomly choose an element and create a matrix the same size as the data and put 1 for the selected element and 0 for the non-selected one

The issues I have faced are that my data is not equal in length so that posed as a problem when I used pandas dataframe so I made it equal but another problem arose mentioned below

If you can help me solve the issues or suggest a better way to do this other than what I tried


here is when I tried list of lists but I couldn't find the index to add the 1s to the matrix

Python:
import random, numpy as np
l=[[[10,20],[2,3]],
   [[5,2]],
   [[10,55],[2,0],[12,0]]]
ln=len(l)  
n=int(0)
if n<ln:
    for k in l:
        print(random.choice(l[n]))
        n+=1
m=max(len(x) for x in l)
zeroarr = np.zeros((ln,m),dtype=int)

and this is when I tried pandas dataframe and I couldn't select randomly from a column

Python:
import pandas as pd, random, numpy as np
task1 = [[2,3],[6,5],[0,9]]
task2 = [[10,7],[6,2],[2,1]]
task3 = [[3,8],[6,7],[4,3]]
data=pd.DataFrame({'t1':task1,'t2':task2,'t3':task3})
zeroar=np.zeros(data.shape,dtype=int)
zeroar
 

Latest posts

Buy us a coffee!

Back
Top Bottom