Path: blob/master/tensorflow_tts/utils/outliers.py
1558 views
# -*- coding: utf-8 -*-1# Copyright 2020 Minh Nguyen (@dathudeptrai)2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""Outliers detection and remove."""15import numpy as np161718def is_outlier(x, p25, p75):19"""Check if value is an outlier."""20lower = p25 - 1.5 * (p75 - p25)21upper = p75 + 1.5 * (p75 - p25)22return x <= lower or x >= upper232425def remove_outlier(x, p_bottom: int = 25, p_top: int = 75):26"""Remove outlier from x."""27p_bottom = np.percentile(x, p_bottom)28p_top = np.percentile(x, p_top)2930indices_of_outliers = []31for ind, value in enumerate(x):32if is_outlier(value, p_bottom, p_top):33indices_of_outliers.append(ind)3435x[indices_of_outliers] = 0.03637# replace by mean f0.38x[indices_of_outliers] = np.max(x)39return x404142