LoginSignup
47
54

More than 3 years have passed since last update.

【初心者向けハンズオン】kaggleの「住宅価格を予測する」を1行ずつ読み解く(第1回:データの読み込み)

Last updated at Posted at 2020-05-18

お題

 サービスで不動産系システムを運用している仕事柄、これは現場レベルでハンズオンやっておいて損はないというお話が勃発。そんなわけで、有名なお題であるkaggleの「House Price」問題にみんなでチャレンジしていくことに。そんでこれは多分ちゃんとメモしておけば、後々のためになるだろうということで、1行ずつ読み解いて行った内容をqiitaに投稿することにしました。解説というよりはメモのまとめだったりもしますが、どこかの誰かのためになれば幸いです。

本日の作業

ライブラリの準備

 各ライブラリの説明については、作業で使用した段階で1つずつ述べていこうと思うので、一旦は呪文としてこちらをコピペしました。

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import (
    LinearRegression,
    Ridge,
    Lasso
)
%matplotlib inline

データの取り込み

 ここからが実作業。まずは使用するCSVファイルの読み込みと整形から。とりあえずこちらをコピペ。1つずつ解説。

# データの読み込み
train = pd.read_csv('train.csv') #訓練データ
test = pd.read_csv('test.csv') #テストデータ
# 学習データとテストデータのマージ
train['WhatIsData'] = 'Train'
test['WhatIsData'] = 'Test'
test['SalePrice'] = 9999999999
alldata = pd.concat([train,test],axis=0).reset_index(drop=True)
print('The size of train is : ' + str(train.shape))
print('The size of test is : ' + str(test.shape))

CSVファイルの読み込み

  • 該当ソース:train = pd.read_csv('train.csv') #訓練データ

  • 説明:「import pandas as pd」でimportしたpandasを使って、CSVファイルを取り込み、変数「train」に格納。pandasは個人的な解釈としては、データを表計算的に簡単に処理するのに使う鉄板ライブラリ。

  • 参考:https://dividable.net/programming/python-pandas/

train変数の列に一律でデータ格納

  • 該当ソース:train['WhatIsData'] = 'Train'
  • 説明:testにあって、trainに無い項目「WhatIsData(test由来なのか、train由来なのかを見分けるための項目)」「SalePrice(testにはもともと無いデータ)」を入れる。

testデータとtrainデータをガッちゃんする

  • 該当ソース:alldata = pd.concat([train,test],axis=0).reset_index(drop=True)
  • 説明:あとで使うまとめデータ、alldataを準備。
  • concat:データを連結する。axisを特に指定しなければ、行追加っぽく連結する。
  • concat参考:http://sinhrks.hatenablog.com/entry/2015/01/28/073327
  • index:そもそもデータを取り扱うにあたっての基本知識。処理するのに勝手に割り振られる番号。reset_indexで使ってますね。
  • index参考:https://techacademy.jp/magazine/24150

取り込んだデータの概要を表示

  • 該当ソース:print('The size of train is : ' + str(train.shape))
  • print:()内に入れた内容を画面表示する。
  • .shape:配列の次元とか縦横とか、概要を出力する。
  • .shape参考:http://www.kamishima.net/mlmpyja/nbayes2/shape.html

おしまい。

本日はここまで。週1時間使ってまとめようと思うので、亀みたいなスピードですがご愛顧よろしくおねがいします。

47
54
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47
54