Skip to main content
deleted 1 character in body
Source Link
riskypenguin
  • 3.5k
  • 1
  • 10
  • 28

Remove sorted

From the docs: numpy.unique returns the sorted unique elements of an array.

You can simply remove the call to sorted:

b = np.unique(sorted(a))

# produces the same result as

b = np.unique(a)

List comprehension

In most cases you can and should avoid this pattern of list creation:

result = []
for i in a:
    result.append((b == i) * 1)

It can be replaced by a concise list comprehension and directly passed to np.array:

result = np.array([(b == i) * 1 for i in a])

# or directly return it (if applicable)
return np.array([(b == i) * 1 for i in a])

List comprehensions are more pythonic, and often faster. Generally, not mutating the list object is also less error-prone.


There might be a better way to map lambda x: (uniques == x) * 1 over the input array a. Here's a discussion on the topic on StackOverflow: Most efficient way to map function over numpy array. Seems like using np.vectorize should be avoided for performance reasons.

Using map might be similiar to the list comprehension performance-wise (I did not properly test performance here):

def convert_listcomp(a):
    uniques = np.unique(a)
    return np.array([(b == i) * 1 for i in a])

def convert_map(a):
    uniques = np.unique(a)
    return np.array(list(map(lambda x: (uniques == x) * 1, a)))

Remove sorted

From the docs: numpy.unique returns the sorted unique elements of an array.

You can simply remove the call to sorted:

b = np.unique(sorted(a))

# produces the same result as

b = np.unique(a)

List comprehension

In most cases you can and should avoid this pattern of list creation:

result = []
for i in a:
    result.append((b == i) * 1)

It can be replaced by a concise list comprehension and directly passed to np.array:

result = np.array([(b == i) * 1 for i in a])

# or directly return it (if applicable)
return np.array([(b == i) * 1 for i in a])

List comprehensions are more pythonic, and often faster. Generally, not mutating the list object is also less error-prone.


There might be a better way to map lambda x: (uniques == x) * 1 over the input array a. Here's a discussion on the topic on StackOverflow: Most efficient way to map function over numpy array. Seems like using np.vectorize should be avoided for performance reasons.

Using map might be similiar to the list comprehension performance-wise (I did not properly test performance here):

def convert_listcomp(a):
    uniques = np.unique(a)
    return np.array([(b == i) * 1 for i in a])

def convert_map(a):
    uniques = np.unique(a)
    return np.array(list(map(lambda x: (uniques == x) * 1, a)))

Remove sorted

From the docs: numpy.unique returns the sorted unique elements of an array.

You can simply remove the call to sorted:

b = np.unique(sorted(a))

# produces the same result as

b = np.unique(a)

List comprehension

In most cases you can and should avoid this pattern of list creation:

result = []
for i in a:
    result.append((b == i) * 1)

It can be replaced by a concise list comprehension and directly passed to np.array:

result = np.array([(b == i) * 1 for i in a])

# or directly return it (if applicable)
return np.array([(b == i) * 1 for i in a])

List comprehensions are more pythonic and often faster. Generally, not mutating the list object is also less error-prone.


There might be a better way to map lambda x: (uniques == x) * 1 over the input array a. Here's a discussion on the topic on StackOverflow: Most efficient way to map function over numpy array. Seems like using np.vectorize should be avoided for performance reasons.

Using map might be similiar to the list comprehension performance-wise (I did not properly test performance here):

def convert_listcomp(a):
    uniques = np.unique(a)
    return np.array([(b == i) * 1 for i in a])

def convert_map(a):
    uniques = np.unique(a)
    return np.array(list(map(lambda x: (uniques == x) * 1, a)))
Source Link
riskypenguin
  • 3.5k
  • 1
  • 10
  • 28

Remove sorted

From the docs: numpy.unique returns the sorted unique elements of an array.

You can simply remove the call to sorted:

b = np.unique(sorted(a))

# produces the same result as

b = np.unique(a)

List comprehension

In most cases you can and should avoid this pattern of list creation:

result = []
for i in a:
    result.append((b == i) * 1)

It can be replaced by a concise list comprehension and directly passed to np.array:

result = np.array([(b == i) * 1 for i in a])

# or directly return it (if applicable)
return np.array([(b == i) * 1 for i in a])

List comprehensions are more pythonic, and often faster. Generally, not mutating the list object is also less error-prone.


There might be a better way to map lambda x: (uniques == x) * 1 over the input array a. Here's a discussion on the topic on StackOverflow: Most efficient way to map function over numpy array. Seems like using np.vectorize should be avoided for performance reasons.

Using map might be similiar to the list comprehension performance-wise (I did not properly test performance here):

def convert_listcomp(a):
    uniques = np.unique(a)
    return np.array([(b == i) * 1 for i in a])

def convert_map(a):
    uniques = np.unique(a)
    return np.array(list(map(lambda x: (uniques == x) * 1, a)))