今回は前回記事の続きで、SUUMOから取得してきたデータのクレンジングを行います。
d-s.hatenablog.com
なお、このテーマはshokosakaさんの記事が参考となっています。
www.analyze-world.com
参考記事ではPythonでデータクレンジング(前処理)を行っていますが、私はR で実施しています。
Pythonでも似たようなコードになると思うので、Pythonユーザーの方でも問題ないと思いますが、ご希望の方はコメントくださればPythonのコードも書いて追記します。
では、さっそく内容に入っていきます。
前回の記事でスクレイピングしてきたデータをクレンジングした結果を先に見ておきます。
以下のようになりました。
> t(head(suumo_data,3))
1 2 3
name "ホーリーグランド稲毛山王305号室" "JR総武線稲毛駅4階建築52年" "大宮町貸家"
rent1 "30000" "50000" "40000"
rent2 "0" "3000" "0"
shikikin "30000" "0" "40000"
reikin "30000" "0" "0"
hoshoukin "0" "0" "0"
layout "1K" "1DK" "1DK"
area "21.06" "34.27" "33.12"
direction "東" "東" "北西"
type "マンション" "マンション" "一戸建て"
age "89" "52" "48"
rout1 "JR総武線" "JR総武線" "JR総武線"
station "稲毛" "稲毛" "千葉"
distance " 72" " 25" "100"
construction "鉄骨" "鉄筋コン" "木造"
floor "3" "2" "1"
height "4" "4" "1"
car.dum "敷地内" "近隣" "付無料"
free_rent "0" "1" "0"
変数名は日本語をローマ字で充てたものと、簡単な英単語なので何かわかると思います。
補足をしておきますと以下の通りです。
area: 専有面積、age: 築年数、rout1: 最寄駅、distance: 最寄駅までの徒歩時間、
floor: その部屋が何階か、height: その物件は何階建か、car.dum: 駐車場有無、
free_rent: フリーレントの実施有無(期間は問わず)
以下が今回作成したRのコードです。
コードを解説しながら進めていこうと思ったのですが、コメントアウトを見ていただければそれなりにわかると思いますので、説明を省略してコードを載せてしまいます。
注意点としては、変数「distance(最寄り駅までの分数)」の取り扱いがあります。抽出した時点のデータセットでは徒歩とバスでの所要分数が入り混じっていました。
今回は、「最寄り駅まで徒歩で何分か」という基準で統一することにしたので、バス~分という場合は、バス~分に×4をして徒歩~分に置き換えています。
具体的には、バス10分であれば徒歩40分に置き換える。そんな感じです。
置き換えの根拠も載せてありますが、あくまで私なりの置き換え方法なので、何かアドバイスがある方はぜひ教えてください。
次回は、予測と物件リコメンドに入ります。
library(tidyverse)
strDirPath = 'スクレイピングしてきたデータが格納されているファイルPATHを各自記入'
lf <- list.files(path = strDirPath, full.names = T)
data <- lapply(lf, read.csv, fileEncoding='cp932')
suumo_data <- do.call(rbind, data)
suumo_data %>% distinct('name', 'rent1', 'rent2', 'layout', 'area','floor_height', .keep_all = TRUE)
suumo_data <- suumo_data %>%
separate(shiki_rei, c('shikikin', 'reikin'), '/', remove = TRUE) %>%
separate(access1, c('rout1', 'distance1'), '/', remove = TRUE) %>%
separate(floor_heigth, c('floor', 'height'), '/', remove = TRUE)
suumo_data <- suumo_data %>%
separate(distance1, c('station', 'distance'), '駅', remove = TRUE)
suumo_data <- suumo_data[!is.na(suumo_data$distance),]
suumo_data$rent2 <- suumo_data$rent2 %>%
str_replace('※定期借家', '') %>%
str_replace('-', '0')
for (row in c(4, 5, 6, 7)){
suumo_data[, row] <- suumo_data[, row] %>%
str_replace('-', '0')
}
suumo_data$rent1 <- as.integer(suumo_data$rent1) * 10000
suumo_data$shikikin <- as.integer(suumo_data$shikikin) * 10000
suumo_data$reikin <- as.integer(suumo_data$reikin) * 10000
suumo_data$hoshoukin <- as.integer(suumo_data$hoshoukin) * 10000
suumo_data$shikibiki_shoukyaku <- as.integer(suumo_data$shikibiki_shoukyaku) * 10000
suumo_data$rent2 <- suumo_data$rent2 %>%
str_replace('※定期借家', '') %>%
str_replace('-', '0')
for (row in c(4, 5, 6, 7)){
suumo_data[, row] <- suumo_data[, row] %>%
str_replace('-', '0')
}
suumo_data$age[suumo_data$age == '新'] <- 0
suumo_data$height[is.na(suumo_data$height)] <- '1階建'
suumo_data$floor <- suumo_data$floor %>%
str_replace('平屋', '1階') %>%
str_replace('階', '') %>%
str_replace('0', '') %>%
str_replace('建', '') %>%
str_replace('B', '')
suumo_data$height <- suumo_data$height %>%
str_replace('地下1地上', '') %>%
str_replace('地下2地上', '') %>%
str_replace('地下3地上', '') %>%
str_replace('地下4地上', '') %>%
str_replace('地下5地上', '') %>%
str_replace('地下6地上', '') %>%
str_replace('地下7地上', '') %>%
str_replace('地下8地上', '') %>%
str_replace('地下9地上', '') %>%
str_replace('階建', '')
suumo_data$kosuu <- suumo_data$kosuu %>%
str_replace('
', '') %>%
str_replace('
', '')
suumo_data$car <- suumo_data$car %>%
str_replace('
', '') %>%
str_replace('
', '')
suumo_data$jouken <- suumo_data$jouken %>%
str_replace('
', '') %>%
str_replace('
', '') %>%
str_replace('
', '') %>%
str_replace('
', '')
temp <- c()
for (row in suumo_data$car){
if (str_detect(row, '敷地内')) {
temp <- c(temp, '敷地内')
} else if (str_detect(row, '近隣')){
temp <- c(temp, '近隣')
} else if (str_detect(row, '付無料')){
temp <- c(temp, '付無料')
} else {
temp <- c(temp, '無し')
}
}
suumo_data$car.dum <- temp
temp <- c()
for (row in suumo_data$jouken){
if (str_detect(row, 'フリーレント')) {
temp <- c(temp, 1)
} else {
temp <- c(temp, 0)
}
}
suumo_data$free_rent <- temp
suumo_data$distance_copy <- suumo_data$distance
suumo_data <- suumo_data %>%
separate(distance, c('distance', 'unused'), '分', remove = TRUE)
suumo_data$distance <- suumo_data$distance %>%
str_replace('バス', '') %>%
str_replace('歩', '') %>%
str_replace('車', '')
temp <- c()
for (i in rep(1:length(suumo_data$distance_copy))){
if (str_detect(suumo_data$distance_copy[i],'バス')) {
temp <- c(temp, as.integer(suumo_data$distance[i]) * 4)
} else if (str_detect(suumo_data$distance_copy[i], '車')) {
temp <- c(temp, as.integer(suumo_data$distance[i]) * 4)
} else {
temp <- c(temp, as.integer(suumo_data$distance[i]))
}
}
suumo_data$distance <- temp
tar <- c('name', 'rent1', 'rent2', 'shikikin', 'reikin',
'hoshoukin', 'layout', 'area', 'direction',
'type', 'age', 'rout1', 'station', 'distance',
'construction', 'floor', 'height', 'car.dum', 'free_rent'
)
suumo_data <- suumo_data[tar]
write.csv(suumo_data, 'お好みのファイル名',
row.names = F, fileEncoding = 'cp932')