Skip to content

Instantly share code, notes, and snippets.

@myui
Last active February 5, 2018 04:46
Show Gist options
  • Save myui/f1042828b65e6b388b5f8954136d19f1 to your computer and use it in GitHub Desktop.
Save myui/f1042828b65e6b388b5f8954136d19f1 to your computer and use it in GitHub Desktop.

予測モデルの作成 (毎日 過去2-3週間分でモデルを作成)

WITH train_input as (
	select
		array_concat(
			array('bias'),
			categorical_features(
				'hour', 'week',
				hour, week
			)
		) as features,
		if(y_axis > 2500, 1.0 0.0) as label -- 0 or 1
	from
		data
)
INSERT OVERWRITE TABLE lr_model as
select
	logress(features, label) as (feature, weight)
from (
	select 
		amplify(5, features, label) as features, label -- 5x input
	train_input
		train_input
	CLUSTER BY rand() -- random shuffle 
) train_input_shuffled

予測(毎日)

Prestoでも動作するように修正

WITN test_exploded as (
	select
	  rowid, 
	  'bias', as bias
	  'hour#' || hour as hour,
	  'week#' || week as week
	from
	   data
)
select
    -- sigmoid(sum(m.weight)) as prob -- 0.0~1.0
	1.0 / (1.0 + EXP(- sum(m.weight) ))
from
	test_exploded t 
	LEFT OUTER JOIN lr_model m ON (t.feature = m.feature)

sigmoid(x) = 1.0 / (1.0 + EXP(-x)); http://hivemall.incubator.apache.org/userguide/tips/rt_prediction.html#onlinerealtime-prediction-on-mysql select

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment