Home > Net >  Numpy appending from a list
Numpy appending from a list

Time:09-21

I have an assignment in which I have to map a name from the list to a specific integer in the array (hope I used the terminology correctly). I wrote this code but it is not working. Why is this so, and how can I make it work?

list_1 = ['sentosa', 'versicolor', 'virginica', 'virginica', 'sentosa']
beta = np.array([])
for x in list_1:
  if x == "sentosa":
    np.append(beta, [1], axis = 0)
  elif x == "versicolor":
    np.append(beta, [2], axis = 0)
  elif x == "virginica":
    np.append(beta, [3], axis = 0)

CodePudding user response:

NumPy arrays have a fixed size, they are not really appendable. What you're doing will be dead slow even if you fix the bugs. Use a plain list instead, and convert it to a NumPy array at the end if necessary.

CodePudding user response:

how to fix your code

numpy.append is not in place, so you would need to assign back the output:

list_1 = ['sentosa', 'versicolor', 'virginica', 'virginica', 'sentosa']
beta = np.array([])
for x in list_1:
    if x == "sentosa":
        beta = np.append(beta, [1], axis = 0)
    elif x == "versicolor":
        beta = np.append(beta, [2], axis = 0)
    elif x == "virginica":
        beta = np.append(beta, [3], axis = 0)

how to do what you want efficiently

You can use numpy.unique with the return_inverse=True option to factorize your data:

np.unique(list_1, return_inverse=True)[1] 1

output: array([1, 2, 3, 3, 1])

CodePudding user response:

To map the list of strings to unique integers, one has to first find the unique strings and then do 1-1 mapping of the strings to integers in the original list of strings.

list_1 = ['sentosa', 'versicolor', 'virginica', 'virginica', 'sentosa']

# create unique list of  names
unique_list = set(list_1) # Unique list {'sentosa', 'versicolor', 'virginica'}

# create mappings from names to integer (id)
string_to_integer = {name: idx 1 for idx, name in enumerate(unique_list)}

# map initial list of names to ids
k = [string_to_integer [name] for name in list_1 ]

output:

3, 2, 1, 1, 3

Another possible way -

list_1 = ['sentosa', 'versicolor', 'virginica', 'virginica', 'sentosa']

mydict={}
i = 0
for item in list_1:
    if(i>0 and item in mydict):
        continue
    else:    
       i = i 1
       mydict[item] = i

k=[]

for item in list_1:
    k.append(mydict[item])
1, 2, 3, 3, 1

Then shift to numpy array

arr = numpy.asarray(k) 

I hope this will help you to do your assignment.

  • Related