Performing Essential Data Manipulations Using Python
103
Facing repetitions
Repeated data can unfairly weight the output of an algorithm so that you get inac-
curate 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)))
b
array([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. Conse-
quently, 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.
Do'stlaringiz bilan baham: |