ためすう
やったこと
Rails5 で rspec の before, after, around を使ってみます。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
$ rails --version
Rails 5.2.3
$ gem list | grep rspec-rails
rspec-rails (3.8.2)
調査
spec/models/task_spec.rb
require 'rails_helper'
RSpec.describe Task, type: :model do
around(:example) do |example|
puts 'around example before'
example.run
puts 'around example after'
end
before(:context) do
puts 'before context'
end
after(:context) do
puts 'after context'
end
it 'gets return' do
puts 'in the example'
end
end
出力結果
$ bundle exec rspec spec/models/task_spec.rb -f d
Task
before context
around example before
in the example
around example after
gets return
after context
Finished in 0.00188 seconds (files took 0.85554 seconds to load)
1 example, 0 failures
参考
Rails5 で時間に関わる rspec のテストを書く
2019-05-27やったこと
Rails5 で時間に関わるテストを rspec で書いてみます。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
$ rails --version
Rails 5.2.3
$ gem list | grep rspec-rails
rspec-rails (3.8.2)
調査
$ rails g rspec:model Task
spec/rails_helper.rb
RSpec.configure do |config|
...
config.include ActiveSupport::Testing::TimeHelpers
...
end
spec/models/task_spec.rb
require 'rails_helper'
RSpec.describe Task, type: :model do
it 'is whether now time' do
travel_to('2019-05-26 23:00'.to_time) do
expect(Time.zone.now).to eq(Time.new(2019, 5, 26, 23, 0, 0))
end
end
end
参考
Rails5 で json を返す
2019-05-26やったこと
Rails5 で json を返す方法について調べました。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
$ rails --version
Rails 5.2.3
$ gem list | grep jbuilder
jbuilder (2.8.0)
調査
hash を使う方法、Jbuilder を使う方法をそれぞれ試してみます。
hash を使う方法
app/controllers/tasks_controller.rb
class TasksController < ApplicationController
def show
res = {
'a': 'val1',
'b': 'val2'
}
render :json => res
end
end
URL例
Jbuilder を使う方法
JbuilderはBuilderと似ていますが、XMLではなくJSONを生成するのに使われます。
URL に json を含む場合
app/views/tasks/show.json.jbuilder
json.a "val1"
json.b "val2"
URL例
http://localhost:3000/tasks/1.json
URL に json を含まない場合
app/views/tasks/show.json.jbuilder
json.a "val1"
json.b "val2"
app/controllers/tasks_controller.rb
class TasksController < ApplicationController
def show
respond_to do |format|
format.html { render 'show.json.jbuilder' }
format.json
end
end
end
URL例 http://localhost:3000/tasks/1
参考
Rails5 で rspec の基礎文法を試す
2019-05-26やったこと
rspec の基礎文法について調べました。
確認環境
$ ruby --version
ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin17]
$ rails --version
Rails 5.2.3
$ gem list | grep rspec-rails
rspec-rails (3.8.2)
feature test
spec/features/session_spec.rb
require 'rails_helper'
RSpec.feature 'session management', :type => :feature do
scenario 'scenario test' do
end
end
出力結果
$ bundle exec rspec -f d spec/features/session_spec.rb
session management
scenario test
Finished in 0.00364 seconds (files took 1.08 seconds to load)
1 example, 0 failures
model test
spec/models/task2_spec.rb
require 'rails_helper'
RSpec.describe Task2, type: :model do
context 'is context1' do
it 'is one thing' do
end
end
describe 'is context2' do
specify 'is one thing in specify' do
end
example 'is one thing in example' do
end
end
end
- context, describe は ExampleGroup を作成します
- it, specify, example は Example を作成します
出力結果
$ bundle exec rspec -f d spec/models/task2_spec.rb
Task2
is context1
is one thing
is context2
is one thing in specify
is one thing in example
Finished in 0.0125 seconds (files took 1.02 seconds to load)
3 examples, 0 failures
参考
Validation
学習目標
- Describe validation process and its purpose
- Compare validation strategies
- Identify train/test split in a competition
- Identify and analyze validation problems
Validation and overfitting
コンペで leaderboard から落ちる2つの理由
- public leaderboard に対して、1番高いものを提出したこと
- public/private に対して、一貫した分割を行なっていない または、データが少ない場合
次から
- validation、overfitting (オーバーフィッティング) の概念を理解すること
- 安定した validation が行われるべき分割数
- コンペでよく使われる訓練データ/テストデータの分割方法
- validation でよく発生する問題
モデルの品質は、訓練データと、将来新しく発生するテストデータで異なる可能性がある
自分たちが持っているデータを下記のように分ける
※ テストデータの分割の仕方は kaggle のコンペの特性上2つに分ける
- 訓練データ
- バリデーションデータ
- テストデータ (public)
- テストデータ (private)
モデル選択をするため、繰り返し validation にかけ、スコアをチェックする
モデル選択には overfitting, underfitting していないか確認する
=> overfitting と underfitting の中間のモデルを選ぶ
一般的な overfitting と、コンペでいう overfitting は異なる
一般的な overfitting
訓練データセットの品質 > テストデータセットの品質 の時
コンペでいう overfitting
テストデータセットの品質が予想より低くなった時のみ
訓練データとバリデーションデータ
- underfitting の時、両方高いエラー率
- overfitting の時、訓練データはエラー率低く、バリデーションデータはエラー率高い
参考
EDA examples
学習目標
- Describe the major visualization tools
- Generate hypotheses about data
- Inspect the data and find golden features
- Examine and analyze various plots and other data visualizations
Springleaf competition EDA II
データの種類を決定する
特徴ごとのユニークな値の数を取得する
例
nunique = train.nunique(dropna=False)
ヒストグラムを作成する
XGBoost は同じ特徴を見つけるのに苦労する
例 訓練データの特徴を数値とカテゴリで分けます
cat_cols = list(train.select_dtypes(include=['object']).columns)
num_cols = list(train.select_dtypes(exclude=['object']).columns)
2つ目の特徴が1つ目の特徴より大きいならば、時系列の累積データの可能性がある
例えば、月ごとの累積データから、月ごとのデータを抽出するのは、線形モデル、ニューラルネットワークなら行うことができる。(tree-based model はできない)
参考
EDA examples
学習目標
- Describe the major visualization tools
- Generate hypotheses about data
- Inspect the data and find golden features
- Examine and analyze various plots and other data visualizations
Springleaf competition EDA II
Numerai competition EDA
コンペの例
- ゴール: データセットに対して、二項分類のタスク
- データ; 21の匿名化された数値的特徴データ
まずやることは、最近傍点を見つけ、21全ての特徴を追加すること
下記の 42 個の特徴を用いた、シンプルなロジスティクス回帰で TOP10 に入ることができた。
- 元のデータから 21 個の特徴
- 近傍点から 21 個の特徴
参考
EDA examples
学習目標
- Describe the major visualization tools
- Generate hypotheses about data
- Inspect the data and find golden features
- Examine and analyze various plots and other data visualizations
Springleaf competition EDA I
データの形状を知る
x.shape
訓練データはターゲットのカラムが1つ多い(モデルを構築するときは削る)
何行か出力してみる
訓練データ、テストデータともに出力してみる
x.head
欠損値を調べる
各特徴、各行に対して調べる
例 (各行に対して)
train.isnull().sum(axis=1).head(15)
例 (各特徴に対して)
train.isnull().sum(axis=0).head(15)
データをクリーニングする
まずデータの種別を決めていく
- 特徴ごとのユニークな値の数を調べる
例
feats_counts = train.nunique(dropna = False)
feats_counts.sort_values()[:10]
- 一意の特徴を削除する
例
constant_features = [削除するカラム]
xxx.drop(constant_features,axis = 1,inplace=True)
- 重複を削除する
欠損値を埋める
例
traintest.fillna('NaN', inplace=True)
ラベルエンコードする
例
train_enc[col] = train[col].factorize()[0]
訓練データ(生)、訓練データ(ラベルエンコード済み)で重複カラムを見つける
削除処理
例
traintest.drop(dup_cols.keys(), axis = 1,inplace=True)
処理に時間がかかった場合、dump しておくと便利
参考
Ruby の Gem のバージョン指定方法
2019-05-26やったこと
Gem のバージョンを設定する方法について調べます。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
$ rails --version
Rails 5.2.3
調査
$ gem ライブラリ [バージョン]
例. hoge というライブラリをインストールします。
最新版インストール (バージョン指定なし)
gem 'hoge'
バージョン固定 (4.5.6 に固定)
gem 'hoge' '4.5.6'
x.x.x 以上のバージョン (4.5.6 以上)
gem 'hoge' '>= 4.5.6'
x.x.x 以上、y.y.y 未満 (4.5.6 以上 4.7.0 未満のバージョン)
gem 'hoge' '>= 4.5.6, < 4.7.0'
~> (4.5 以上 4.9 以下)
gem 'hoge' '~> 4.5'
参考
Rails5 で rspec をインストールする
2019-05-26やったこと
Rails5 で rspec を使えるようにします。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
$ rails --version
Rails 5.2.3
調査
Gem のインストール
Gemfile
group :development, :test do
gem 'rspec-rails', '~> 3.8'
end
bundle インストール
$ bundle install
rspec のインストール
$ rails generate rspec:install
Running via Spring preloader in process 3674
create .rspec
create spec
create spec/spec_helper.rb
create spec/rails_helper.rb
rspec を実行 (※ まだテストは空です)
$ bundle exec rspec
No examples found.
Finished in 0.00034 seconds (files took 0.09629 seconds to load)
0 examples, 0 failures
これで rspec の準備が整いました。