Читать книгу Algorithms For Dummies - John Paul Mueller, John Mueller Paul, Luca Massaron - Страница 91

Facing repetitions

Оглавление

Repeated data can unfairly weight the output of an algorithm so that you get inaccurate results. Sometimes you need unique values to determine the outcome of a data manipulation. Fortunately, Python makes it easy to remove certain types of repeated data. Consider this example:

a = np.array([1,2,3,4,5,6,6,7,7,1,2,3])b = np.array(list(set(a))) print(b)

The output contains only the unique elements:

[1, 2, 3, 4, 5, 6, 7]

In this case, a begins with an assortment of numbers in no particular order and with plenty of repetitions. In Python, a set never contains repeated data. Consequently, by converting the list in a to a set and then back to a list, and then placing that list in an array, you obtain a vector that has no repeats.

Algorithms For Dummies

Подняться наверх