Skip to main content
Code Review

Return to Question

Became Hot Network Question
Tweeted twitter.com/StackCodeReview/status/1379901820471013379
Rollback to Revision 1
Source Link
Reinderien
  • 70.9k
  • 5
  • 76
  • 256

Here is the problem:

Given a numpy array 'a' that contains n elements, denote by b the set of its unique values ​​in ascending order, denote by m the size of array b. You need to create a numpy array with dimensions n×ばつm , in each row of which there must be a value of 1 in case it is equal to the value of the given index of array b, in other places it must be 0.

import numpy as np
def convert(a):
 b = np.unique(sorted(a))
 result = []
 for i in a:
 result.append((b == i) * 1)
 return np.array(result)
a = np.array([1, 1, 2, 3, 2, 4, 5, 2, 3, 4, 5, 1, 1])
b = np.unique(sorted(a))
print(convert(a))

This is my solution. is there some improvments that I can make? I'm not sure about declaring regular list to the result and then converting it into np.array.

Here is the problem:

Given a numpy array 'a' that contains n elements, denote by b the set of its unique values ​​in ascending order, denote by m the size of array b. You need to create a numpy array with dimensions n×ばつm , in each row of which there must be a value of 1 in case it is equal to the value of the given index of array b, in other places it must be 0.

import numpy as np
def convert(a):
 b = np.unique(sorted(a))
 result = []
 for i in a:
 result.append((b == i) * 1)
 return np.array(result)
a = np.array([1, 1, 2, 3, 2, 4, 5, 2, 3, 4, 5, 1, 1])
print(convert(a))

This is my solution. is there some improvments that I can make? I'm not sure about declaring regular list to the result and then converting it into np.array.

Here is the problem:

Given a numpy array 'a' that contains n elements, denote by b the set of its unique values ​​in ascending order, denote by m the size of array b. You need to create a numpy array with dimensions n×ばつm , in each row of which there must be a value of 1 in case it is equal to the value of the given index of array b, in other places it must be 0.

import numpy as np
def convert(a):
 b = np.unique(sorted(a))
 result = []
 for i in a:
 result.append((b == i) * 1)
 return np.array(result)
a = np.array([1, 1, 2, 3, 2, 4, 5, 2, 3, 4, 5, 1, 1])
b = np.unique(sorted(a))
print(convert(a))

This is my solution. is there some improvments that I can make? I'm not sure about declaring regular list to the result and then converting it into np.array.

deleted 30 characters in body
Source Link

Here is the problem:

Given a numpy array 'a' that contains n elements, denote by b the set of its unique values ​​in ascending order, denote by m the size of array b. You need to create a numpy array with dimensions n×ばつm , in each row of which there must be a value of 1 in case it is equal to the value of the given index of array b, in other places it must be 0.

import numpy as np
def convert(a):
 b = np.unique(sorted(a))
 result = []
 for i in a:
 result.append((b == i) * 1)
 return np.array(result)
a = np.array([1, 1, 2, 3, 2, 4, 5, 2, 3, 4, 5, 1, 1])
b = np.unique(sorted(a))
print(convert(a))

This is my solution. is there some improvments that I can make? I'm not sure about declaring regular list to the result and then converting it into np.array.

Here is the problem:

Given a numpy array 'a' that contains n elements, denote by b the set of its unique values ​​in ascending order, denote by m the size of array b. You need to create a numpy array with dimensions n×ばつm , in each row of which there must be a value of 1 in case it is equal to the value of the given index of array b, in other places it must be 0.

import numpy as np
def convert(a):
 b = np.unique(sorted(a))
 result = []
 for i in a:
 result.append((b == i) * 1)
 return np.array(result)
a = np.array([1, 1, 2, 3, 2, 4, 5, 2, 3, 4, 5, 1, 1])
b = np.unique(sorted(a))
print(convert(a))

This is my solution. is there some improvments that I can make? I'm not sure about declaring regular list to the result and then converting it into np.array.

Here is the problem:

Given a numpy array 'a' that contains n elements, denote by b the set of its unique values ​​in ascending order, denote by m the size of array b. You need to create a numpy array with dimensions n×ばつm , in each row of which there must be a value of 1 in case it is equal to the value of the given index of array b, in other places it must be 0.

import numpy as np
def convert(a):
 b = np.unique(sorted(a))
 result = []
 for i in a:
 result.append((b == i) * 1)
 return np.array(result)
a = np.array([1, 1, 2, 3, 2, 4, 5, 2, 3, 4, 5, 1, 1])
print(convert(a))

This is my solution. is there some improvments that I can make? I'm not sure about declaring regular list to the result and then converting it into np.array.

Source Link

Creating nxm index list of array a

Here is the problem:

Given a numpy array 'a' that contains n elements, denote by b the set of its unique values ​​in ascending order, denote by m the size of array b. You need to create a numpy array with dimensions n×ばつm , in each row of which there must be a value of 1 in case it is equal to the value of the given index of array b, in other places it must be 0.

import numpy as np
def convert(a):
 b = np.unique(sorted(a))
 result = []
 for i in a:
 result.append((b == i) * 1)
 return np.array(result)
a = np.array([1, 1, 2, 3, 2, 4, 5, 2, 3, 4, 5, 1, 1])
b = np.unique(sorted(a))
print(convert(a))

This is my solution. is there some improvments that I can make? I'm not sure about declaring regular list to the result and then converting it into np.array.

lang-py

AltStyle によって変換されたページ (->オリジナル) /