我如何从异常值中清除我的数据集,因为它包含 Python 中的数值和分类变量?
How can i clean my dataset from outliers as it includes numerical and categorical variables in Python?
我想从异常值中清除我的数据集,但只在三个特定的列中,因为其他 10 个包含分类变量。那么,如何仅通过引用这些特定列来清理数据?
我想使用 iqr 范围方法。这是我 运行 到目前为止的代码:
import numpy as np
def outliers(x):
return np.abs(x- x.median()) > 1.5*(x.quantile(.75)-x.quantile(0.25))
ath2.Age[outliers(ath2.Age)]
ath2.Height[outliers(ath2.Height)]
ath2.Weight[outliers(ath2.Weight)]
在检查了我感兴趣的列中异常值的数量后,我不知道如何进一步进行。
如果您希望代码是动态的,您可以首先通过以下代码检查非分类的列:
cols = df.columns
num_cols = df._get_numeric_data().columns
##num_cols will contains list of column names which are numeric
## In your case, it should come Age,Height etc.
或者,您也可以根据您的数据帧df.select_dtypes
使用include
或exclude
参数
在此 运行 下面的代码之后来自上面的列:
df[np.abs(df.Data-df.Data.mean()) <= (3*df.Data.std())]
## Df is the dataframe and Data is the name of the column.
#In your case, it will be Age,Height etc.
或
如果你想制作一个只有数字列的新 df 并一次性找出异常值,下面是代码:
df[df.apply(lambda x: np.abs(x - x.mean()) / x.std() < 3).all(axis=1)]
我想从异常值中清除我的数据集,但只在三个特定的列中,因为其他 10 个包含分类变量。那么,如何仅通过引用这些特定列来清理数据?
我想使用 iqr 范围方法。这是我 运行 到目前为止的代码:
import numpy as np
def outliers(x):
return np.abs(x- x.median()) > 1.5*(x.quantile(.75)-x.quantile(0.25))
ath2.Age[outliers(ath2.Age)]
ath2.Height[outliers(ath2.Height)]
ath2.Weight[outliers(ath2.Weight)]
在检查了我感兴趣的列中异常值的数量后,我不知道如何进一步进行。
如果您希望代码是动态的,您可以首先通过以下代码检查非分类的列:
cols = df.columns
num_cols = df._get_numeric_data().columns
##num_cols will contains list of column names which are numeric
## In your case, it should come Age,Height etc.
或者,您也可以根据您的数据帧df.select_dtypes
使用include
或exclude
参数
在此 运行 下面的代码之后来自上面的列:
df[np.abs(df.Data-df.Data.mean()) <= (3*df.Data.std())]
## Df is the dataframe and Data is the name of the column.
#In your case, it will be Age,Height etc.
或
如果你想制作一个只有数字列的新 df 并一次性找出异常值,下面是代码:
df[df.apply(lambda x: np.abs(x - x.mean()) / x.std() < 3).all(axis=1)]