ウェブサイト検索

Rにおける外れ値分析 - 外れ値を検出して除去する


こんにちは、読者の皆さん!この記事では、Rプログラミングにおける異常値分析について詳しく焦点を当てていきます。

それでは、始めましょう!!

データの外れ値とは何ですか?

異常値の概念に深く入る前に、データ値の前処理に焦点を当てましょう。

データサイエンスと機械学習の分野では、データ値の前処理が重要なステップです。前処理とは、モデル化の前にデータからすべてのエラーやノイズを除去することを指します。

前回の投稿では、Rプログラミングにおける欠損値分析について理解しました。

今日は、同じテーマの高度なレベルであるRにおける外れ値の検出と除去に焦点を当てます。

外れ値は、その名前が示すように、データセットの他のデータポイントから離れた位置にあるデータポイントです。それは、他のデータ値から離れて現れるデータ値であり、そのためデータセットの全体的な分布を乱すことになります。

これは通常、データ値の異常分布として仮定されます。

モデルに対する外れ値の影響 -

  1. データは偏った形式であることが判明しました。
  2. 平均や分散などの観点からデータの全体的な統計分布を変更します。
  3. モデルの精度レベルにバイアスを得ることにつながります。

外れ値の影響を理解したので、今はその実装に取り組む時です。

外れ値分析 - セットして出発!

最初に、データセットにおける外れ値の存在を検出することが非常に重要です。

では、始めましょう。私たちは自転車レンタル数予測データセットを利用しました。データセットはここにあります!

1. データセットの読み込み

最初に、read.csv() 関数を使用してデータセットを R 環境に読み込みました。

外れ値検出の前に、NULLや欠損値の存在を確認するために欠損値分析を実施しました。そのために、sum(is.na(data)) 関数を使用しました。

#Removed all the existing objects
rm(list = ls())

#Setting the working directory
setwd("D:/Ediwsor_Project - Bike_Rental_Count/")
getwd()

#Load the dataset
bike_data = read.csv("day.csv",header=TRUE)

### Missing Value Analysis ###
sum(is.na(bike_data))
summary(is.na(bike_data))

#From the above result, it is clear that the dataset contains NO Missing Values.

ここにあるデータには欠損値がありません

2. ボックスプロット関数を使用して外れ値を検出する

これを言った上で、今がデータセット内の外れ値の存在を検出する時です。これを達成するために、c() 関数を使用して数値データの列を別のデータ構造/変数に保存しました。

さらに、数値変数の外れ値を検出するために boxplot() 関数を使用しました。

ボックスプロット:

外れ値検出 - ボックスプロット法

視覚的に見ると、変数「湿度」と「風速」のデータ値には外れ値が含まれていることが明らかです。

3. 外れ値をNULL値で置き換える

さて、Rで外れ値分析を実施した後、boxplot()メソッドによって特定された外れ値をNULL値に置き換えて、以下のようにそれを操作します。

##############################Outlier Analysis -- DETECTION###########################

1. Outliers in the data values exists only in continuous/numeric form of data variables. Thus, we need to store all the numeric and categorical independent variables into a separate array structure.
col = c('temp','cnt','hum','windspeed')
categorical_col = c("season","yr","mnth","holiday","weekday","workingday","weathersit")

2. Using BoxPlot to detect the presence of outliers in the numeric/continuous data columns.
boxplot(bike_data[,c('temp','atemp','hum','windspeed')])

From the above visualization, it is clear that the data variables 'hum' and 'windspeed' contains outliers in the data values.
#OUTLIER ANALYSIS -- Removal of Outliers
1. From the boxplot, we have identified the presence of outliers. That is, the data values that are present above the upper quartile and below the lower quartile can be considered as the outlier data values.
2. Now, we will replace the outlier data values with NULL.

for (x in c('hum','windspeed'))
{
  value = bike_data[,x][bike_data[,x] %in% boxplot.stats(bike_data[,x])$out]
  bike_data[,x][bike_data[,x] %in% value] = NA
} 

#Checking whether the outliers in the above defined columns are replaced by NULL or not
sum(is.na(bike_data$hum))
sum(is.na(bike_data$windspeed))
as.data.frame(colSums(is.na(bike_data)))

4. すべての外れ値がNULLに置き換えられていることを確認します

さて、欠損データの存在を確認します。すなわち、外れ値が正しく欠損値に変換されているかを sum(is.na()) 関数を使用して確認します。

出力:

> sum(is.na(bike_data$hum))
[1] 2
> sum(is.na(bike_data$windspeed))
[1] 13
> as.data.frame(colSums(is.na(bike_data)))
           colSums(is.na(bike_data))
instant                            0
dteday                             0
season                             0
yr                                 0
mnth                               0
holiday                            0
weekday                            0
workingday                         0
weathersit                         0
temp                               0
atemp                              0
hum                                2
windspeed                         13
casual                             0
registered                         0
cnt                                0

その結果、‘hum’ 列の 2 つの外れ値と ‘windspeed’ 列の 13 の外れ値を欠損値 (NA) に変換しました。

5. 欠損値のある列を削除する

最後に、‘tidyr’ライブラリのdrop_na()関数を使用してNULL値を削除することによって、欠損値を処理します。

#Removing the null values
library(tidyr)
bike_data = drop_na(bike_data)
as.data.frame(colSums(is.na(bike_data)))

出力:

その結果、全ての外れ値が効果的に削除されました!

> as.data.frame(colSums(is.na(bike_data)))
           colSums(is.na(bike_data))
instant                            0
dteday                             0
season                             0
yr                                 0
mnth                               0
holiday                            0
weekday                            0
workingday                         0
weathersit                         0
temp                               0
atemp                              0
hum                                0
windspeed                          0
casual                             0
registered                         0
cnt                                0

結論

これで、このトピックは終了です。質問がある場合は、下にコメントしてください。Rプログラミングに関連する投稿がもっと知りたい方は、ぜひ注目してください!!

それまで、楽しく学びましょう!!:)

関連記事: