Skip to content

Instantly share code, notes, and snippets.

@yamaguchiyuto
Created March 16, 2017 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamaguchiyuto/02957ad25f0f444d270f1fc228d542a4 to your computer and use it in GitHub Desktop.
Save yamaguchiyuto/02957ad25f0f444d270f1fc228d542a4 to your computer and use it in GitHub Desktop.
ILFM experiment
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"from ilfm import ILFM"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# データ生成\n",
"N = 300\n",
"K = 3\n",
"D = 10\n",
"var_x=1.0\n",
"var_y=0.1\n",
"X = np.random.normal(loc=0, scale=np.sqrt(var_x), size=(K,D))\n",
"Z = np.random.randint(low=0,high=2,size=(N,K))\n",
"true_Y = Z.dot(X)\n",
"Y = true_Y + np.random.normal(loc=0,scale=np.sqrt(var_y),size=(N,D))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<ilfm.ILFM instance at 0x106308290>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# フィッティング\n",
"alpha=0.01\n",
"max_iter=100\n",
"model = ILFM(var_x=var_x, var_y=var_y, alpha=alpha, max_iter=max_iter)\n",
"model.fit(Y)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0642104688117\n"
]
}
],
"source": [
"# 相対誤差\n",
"print abs(model.Z.dot(model.X)-true_Y).sum()/abs(true_Y).sum()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n"
]
}
],
"source": [
"# 推定された潜在次元数(K)\n",
"print model.sampled_features.sum()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment