Created
May 9, 2016 13:40
-
-
Save kurubushi--rm/69cd697dab2b201119261cac6fc584d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# fsQCAを手作業で\n", | |
"\n", | |
"ソース:石田淳. (2009). ファジィセット質的比較分析の応用可能性. 理論と方法, 24(2), 203-218.\n", | |
"\n", | |
"の前半でファジィセット質的比較分析の解説で使われる仮想データセットを使って、ファジィセット質的比較分析をなぞってみる。" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>A</th>\n", | |
" <th>B</th>\n", | |
" <th>C</th>\n", | |
" <th>Y</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>1.0</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.9</td>\n", | |
" <td>1.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>0.9</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.4</td>\n", | |
" <td>0.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>0.9</td>\n", | |
" <td>0.9</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.9</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>0.8</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.3</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>0.7</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.4</td>\n", | |
" <td>0.5</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>0.4</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.3</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>0.3</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.9</td>\n", | |
" <td>0.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>0.4</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.6</td>\n", | |
" <td>0.7</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>9</th>\n", | |
" <td>0.1</td>\n", | |
" <td>0.4</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>10</th>\n", | |
" <td>0.0</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.2</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" A B C Y\n", | |
"1 1.0 0.8 0.9 1.0\n", | |
"2 0.9 0.7 0.4 0.8\n", | |
"3 0.9 0.9 0.8 0.9\n", | |
"4 0.8 0.3 0.7 0.3\n", | |
"5 0.7 0.8 0.4 0.5\n", | |
"6 0.4 0.3 0.7 0.3\n", | |
"7 0.3 0.7 0.9 0.8\n", | |
"8 0.4 0.8 0.6 0.7\n", | |
"9 0.1 0.4 0.1 0.0\n", | |
"10 0.0 0.3 0.8 0.2" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import pandas as pd\n", | |
"df = pd.read_csv('ex-fuzz.csv' )\n", | |
"df.index = range(1, 11)\n", | |
"df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def m_value(df, idx, abc):\n", | |
" '''dfの表からmembership_valueを得る'''\n", | |
" if abc in 'ABC':\n", | |
" return float(df.ix[idx][abc])\n", | |
" elif abc in 'abc':\n", | |
" return 1.0 - float(df.ix[idx][abc.upper()])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>ABC</th>\n", | |
" <th>ABc</th>\n", | |
" <th>AbC</th>\n", | |
" <th>Abc</th>\n", | |
" <th>aBC</th>\n", | |
" <th>aBc</th>\n", | |
" <th>abC</th>\n", | |
" <th>abc</th>\n", | |
" <th>Y</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>0.8</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.0</td>\n", | |
" <td>1.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>0.4</td>\n", | |
" <td>0.6</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>0.8</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.9</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>0.3</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.3</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>0.4</td>\n", | |
" <td>0.6</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.5</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>0.3</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.4</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.6</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.3</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>0.3</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>0.4</td>\n", | |
" <td>0.4</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.6</td>\n", | |
" <td>0.4</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.7</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>9</th>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.4</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.6</td>\n", | |
" <td>0.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>10</th>\n", | |
" <td>0.0</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.2</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" ABC ABc AbC Abc aBC aBc abC abc Y\n", | |
"1 0.8 0.1 0.2 0.1 0.0 0.0 0.0 0.0 1.0\n", | |
"2 0.4 0.6 0.3 0.3 0.1 0.1 0.1 0.1 0.8\n", | |
"3 0.8 0.2 0.1 0.1 0.1 0.1 0.1 0.1 0.9\n", | |
"4 0.3 0.3 0.7 0.3 0.2 0.2 0.2 0.2 0.3\n", | |
"5 0.4 0.6 0.2 0.2 0.3 0.3 0.2 0.2 0.5\n", | |
"6 0.3 0.3 0.4 0.3 0.3 0.3 0.6 0.3 0.3\n", | |
"7 0.3 0.1 0.3 0.1 0.7 0.1 0.3 0.1 0.8\n", | |
"8 0.4 0.4 0.2 0.2 0.6 0.4 0.2 0.2 0.7\n", | |
"9 0.1 0.1 0.1 0.1 0.1 0.4 0.1 0.6 0.0\n", | |
"10 0.0 0.0 0.0 0.0 0.3 0.2 0.7 0.2 0.2" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from collections import OrderedDict\n", | |
"\n", | |
"abc_list_list = [[ai,bi,ci] for ai in ['A','a'] for bi in ['B','b'] for ci in ['C','c']]\n", | |
"combi_df = pd.DataFrame()\n", | |
"\n", | |
"for idx in df.index:\n", | |
" s_dict = OrderedDict()\n", | |
" for abc_list in abc_list_list:\n", | |
" m_value_list = [m_value(df, idx, abc) for abc in abc_list]\n", | |
" #print (idx, ''.join(abc_list), ('%03.1f' % min(m_value_list)))\n", | |
" s_dict[''.join(abc_list)] = ('%03.1f' % min(m_value_list)),\n", | |
" combi_df = combi_df.append(pd.DataFrame(s_dict))\n", | |
"combi_df.index = range(1, 11)\n", | |
"\n", | |
"combi_df['Y'] = df['Y']\n", | |
"\n", | |
"combi_df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>ABC</th>\n", | |
" <th>ABc</th>\n", | |
" <th>AbC</th>\n", | |
" <th>Abc</th>\n", | |
" <th>aBC</th>\n", | |
" <th>aBc</th>\n", | |
" <th>abC</th>\n", | |
" <th>abc</th>\n", | |
" <th>Y</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>0.8</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.0</td>\n", | |
" <td>1.0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>0.8</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.9</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" ABC ABc AbC Abc aBC aBc abC abc Y\n", | |
"1 0.8 0.1 0.2 0.1 0.0 0.0 0.0 0.0 1.0\n", | |
"3 0.8 0.2 0.1 0.1 0.1 0.1 0.1 0.1 0.9" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# 組み合わせ条件ABC のメンバーシップ値が 0.5 以上となるケースを抜き出す\n", | |
"combi_df[combi_df['ABC'].apply(lambda x: float(x)) > 0.5]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0.97368421052631571" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#ABCとYの包含係数の計算\n", | |
"#combi_df.apply(lambda x: float(min(x['ABC'], x['Y'])), axis=1) #ABCとYのメンバーシップ値の小さい方\n", | |
"min_sum = sum(combi_df.apply(lambda x: min(float(x['ABC']), float(x['Y'])), axis=1)) #ABCとYのメンバーシップ値の小さい方の合計\n", | |
"nowcomb_sum = sum(combi_df['ABC'].apply(lambda x: float(x))) #ABCのメンバーシップ値の合計\n", | |
"min_sum / nowcomb_sum" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def inclusion_index(df, num, denom):\n", | |
" '''包含係数の計算'''\n", | |
" min_sum = sum(df.apply(lambda x: float(min(float(x[denom]), float(x[num]))), axis=1)) #メンバーシップ値の小さい方の合計\n", | |
" denom_sum = sum(df[denom].apply(lambda x: float(x))) #当該組み合わせのメンバーシップ値の合計\n", | |
" return ('%03.3f' % (min_sum / denom_sum))\n", | |
"\n", | |
"#inclusion_index(combi_df, 'Y', 'ABc')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>A</th>\n", | |
" <th>B</th>\n", | |
" <th>C</th>\n", | |
" <th>comb</th>\n", | |
" <th>0.5over</th>\n", | |
" <th>I(Xi,Y)</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>1</td>\n", | |
" <td>1</td>\n", | |
" <td>1</td>\n", | |
" <td>ABC</td>\n", | |
" <td>2</td>\n", | |
" <td>0.974</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1</td>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" <td>ABc</td>\n", | |
" <td>2</td>\n", | |
" <td>0.926</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" <td>AbC</td>\n", | |
" <td>1</td>\n", | |
" <td>0.760</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" <td>0</td>\n", | |
" <td>Abc</td>\n", | |
" <td>0</td>\n", | |
" <td>0.941</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" <td>1</td>\n", | |
" <td>aBC</td>\n", | |
" <td>2</td>\n", | |
" <td>0.926</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" <td>aBc</td>\n", | |
" <td>0</td>\n", | |
" <td>0.810</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>0</td>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" <td>abC</td>\n", | |
" <td>2</td>\n", | |
" <td>0.640</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>0</td>\n", | |
" <td>0</td>\n", | |
" <td>0</td>\n", | |
" <td>abc</td>\n", | |
" <td>1</td>\n", | |
" <td>0.700</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" A B C comb 0.5over I(Xi,Y)\n", | |
"1 1 1 1 ABC 2 0.974\n", | |
"2 1 1 0 ABc 2 0.926\n", | |
"3 1 0 1 AbC 1 0.760\n", | |
"4 1 0 0 Abc 0 0.941\n", | |
"5 0 1 1 aBC 2 0.926\n", | |
"6 0 1 0 aBc 0 0.810\n", | |
"7 0 0 1 abC 2 0.640\n", | |
"8 0 0 0 abc 1 0.700" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"table_df = pd.DataFrame()\n", | |
"for abc_list in abc_list_list:\n", | |
" s_dict = OrderedDict()\n", | |
" for nowABC in ['A','B','C']:\n", | |
" if nowABC in abc_list:\n", | |
" s_dict[nowABC] = 1,\n", | |
" else:\n", | |
" s_dict[nowABC] = 0,\n", | |
" combination_condition = ''.join(abc_list)\n", | |
" s_dict['comb'] = combination_condition\n", | |
" s_dict['0.5over'] = len(combi_df[combi_df[combination_condition].apply(lambda x: float(x)) > 0.5])\n", | |
" s_dict['I(Xi,Y)'] = inclusion_index(combi_df, 'Y', combination_condition)\n", | |
" table_df = table_df.append(pd.DataFrame(s_dict))\n", | |
" \n", | |
"table_df.index = range(1, len(table_df)+1)\n", | |
"table_df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"from sympy import *\n", | |
"init_printing()\n", | |
"\n", | |
"def minimize(df, cond_list, reminder_col, result_col, cutpoint):\n", | |
" '''\n", | |
" 真理表から縮約した論理式を返す\n", | |
" df : 真理表のデータフレーム\n", | |
" cond_list : 条件が入ってるコラムのリスト\n", | |
" reminder_col : このコラムが0ならリマインダーとして除外する\n", | |
" result_col : 結果が入っているコラム\n", | |
" cutpoint : 現象が生起とみなせる下限値\n", | |
" '''\n", | |
" true_case_df = df[df[result_col].apply(lambda x: float(x)) >= cutpoint]\n", | |
" true_case_df = true_case_df[true_case_df[reminder_col] != 0]\n", | |
" #return true_case_df\n", | |
" minterms = true_case_df[cond_list].values.tolist()\n", | |
" return SOPform(cond_list, minterms )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAI8AAAASCAYAAABim40OAAAABHNCSVQICAgIfAhkiAAABLJJREFU\naIHt2WmoFXUYx/FPN5dWcclSM7JFjIpKMiO11aSFgkqoBC0oCCvIVqOiBdIMggrrRWFagXG1VV8o\n+aIyzepFWmKFItFCC9GekZWVvXjmcI/jzJn/mSvSi/uFyz0zz/zn+c3v/Of5L4ceeqjJHgXneuNg\nfJZ4j0ewBffsIk0pzMfo7O93vI1/slgfDMErmIV++As/7UZ9ZR62o3vr7hCKY3AlTsGfwqef8YDQ\n/4z4bn9V4WEv3It9EhOPEQ+/oH3N1mBUjXYNRmA75hTEThIdZhk68CD2q7jfMtHRqlgoOkYZVR6O\nkKY7lbo+9sM8fIGp6NsUG4yX8TQ2SvTwFhyXmLwDrwkjlrejGhPwKea22a6ZaVnus0via7P4YAwV\nFbIVt+GGimuGY3XFNVUetqO7iro+HoaPReUbVHLNUZmOx7Pjlh7uj5faEDAd14o3ZV0b7WARxuFD\n1RWhjHmizO5dEOvA12I47ZOde1K89WUMFM9RNJQ3mCXe0jJSPGxXdyvq+DgQm7AZAyqu3YiLm45L\nPbwEtyYKGCyqTge+FA+cylBRauEuXNdG22Y2Ka8C08Vbc03TuUsxs+Ke8zGpJNYHG7T+UlM8bFd3\nGXV97MxyTEy4djX6Nx2XejgXpycKWIDx2ee1+Ft0pBTuExM0ohOuTWzXzEHCgPtz5/viZnyFK3Kx\nw7G04r4nYElJbCpmV7Sv8rCO7jLu076PJ2f5VyXmuCx3vIOHvZoCw/F9wg3HY09dvf7b7Hhw9rkV\nvTFZTAzhO3yEs/B6Qu4Gp2X/jxATOaIijBergjNEWW7mB/HwrfhAvGmH4vNcbDqmVLSv8rCO7iLq\n+nhV9n9+Qg5YnDsu9XAFjqy4WS+8JUpmgwWiNx+fIGaKLtMajBUz+3Z4TMwb9iqIzRZzhvzw01sM\nGVVcbueV0Bi8mNC2ysM6uouo6+Nm8V0NT8hRRKmHC3X15DJusvOYPicTdE5C8lXirc6zBocktG+w\nXlfly9Mbv+EbUREbHKh6pdRov96OS9dncWZC2yoP6+guoq6PW7McKYwsOLeDh83zlI1a98hhouOc\ni1eb/i7K4kMqxIzGj3YeDuAJsXJLoT+OVd4RtuGPTM/ApvOHiGesYpvYqGuM9wdk+d5IaNvKw7q6\n83THx1/E8FhFB2YUnC/18EQ83OKGnWJMzTNZVJ7bKwQtUF6S+4rlZt+SeDMXZvkuKImPzOL5+dcM\nsRpKYagYnuEO6SuZVh7W1Z2nOz4uEZ1034oc14s9pDwtPewU5TPPRF2bRXkaM/hWm3AD8b7qPZSU\nlcZD+Ff5HsULmZ78G7hYWudssEhs2W/Q3l5UmYd1dTfTXR9PFb8ItNoMnYQ7S2ItPRyra0beYJTY\nwt6/pM0I8dCdLQTNlLZ7WzYfaOY98YXm6Y+nhDn35mLni4lwO0zAJ9rfvS3ykHq68+wKH28U855p\ndpy2DMLdYqe9iCQPz8PRYjn5jihz20U5HJa7dqkYe7eLydjKrH2e5dJ+N3pO8ZxhgFiCrstybcGb\nWb6VeFesJBaKoaOZYbg6IXcRdX83anjYHd1FdNfHBuPwvNgbWiFWko9mmovojoc99NBDDz308H/g\nP7HXN//G6W8fAAAAAElFTkSuQmCC\n", | |
"text/latex": [ | |
"$$\\left(A \\wedge B\\right) \\vee \\left(B \\wedge C\\right)$$" | |
], | |
"text/plain": [ | |
"(A ∧ B) ∨ (B ∧ C)" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sufficient_condition = minimize(table_df, ['A','B','C'], '0.5over', 'I(Xi,Y)', 0.9)\n", | |
"sufficient_condition" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"#十分条件のメンバーシップ値を計算するための補助関数\n", | |
"#ファジィ集合間の集合演算\n", | |
"def fuzzy_and(x, y):\n", | |
" return min(x, y)\n", | |
"\n", | |
"def fuzzy_or(x,y):\n", | |
" return max(x,y)\n", | |
"\n", | |
"def fuzzy_not(x):\n", | |
" return 1 - x\n", | |
"\n", | |
"#論理式をファジィ集合演算のルーチンに置き換え\n", | |
"def boolean2fuzzy(s):\n", | |
" s = s.replace('And','fuzzy_and')\n", | |
" s = s.replace('Or','fuzzy_or')\n", | |
" s = s.replace('Not','fuzzy_not')\n", | |
" return s\n", | |
"\n", | |
"def eval_fuzzy(s, mydict):\n", | |
" '''\n", | |
" 辞書で与えた代入をしてファジィ集合演算しメンバーシップ値を返す\n", | |
" \n", | |
" 例:eval_fuzzy(sufficient_condition, {'A':1.0, 'B':0.8, 'C':0.9}) \n", | |
" '''\n", | |
" s = boolean2fuzzy(str(s))\n", | |
" for k,v in mydict.items():\n", | |
" s = s.replace(k, str(v))\n", | |
" return eval(s)\n", | |
"\n", | |
"#eval_fuzzy(sufficient_condition, {'A':1.0, 'B':0.8, 'C':0.9}) \n", | |
"# =>0.8" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def add_membershipvalues(origin_df, cond_list, add_form_list):\n", | |
" result_df = origin_df\n", | |
" for add_form in add_form_list:\n", | |
" now_membership_value = []\n", | |
" for idx in result_df.index:\n", | |
" value_dict = {}\n", | |
" for cond in cond_list:\n", | |
" value_dict[cond] = result_df[cond][idx]\n", | |
" now_membership_value.append(eval_fuzzy(add_form, value_dict) )\n", | |
" result_df[str(add_form)] = now_membership_value\n", | |
" return result_df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"image/png": "iVBORw0KGgoAAAANSUhEUgAAADUAAAASCAYAAAD7T5b+AAAABHNCSVQICAgIfAhkiAAAAlVJREFU\nSInt1k2ITlEYB/CfMWYoNKYmQ2Rq0myU5GNhJPlIipWFLLDTsFAkimLKxygLxUZhVjRWPhZmhca3\nhVFCkoVokMTCCOOjsTjnbV5v977vvVMs5F9v97znf5//8z/nPM/p8g9iRM73j6Afe/6AlzScwqz4\n+4zb+Bm5GjTiPPbjS17xOVGscxjGbqFlGHEFNGEQHQncXHzDpbyiVbgShbtzxi7AcxzNm7QI62Lu\npSl8b+Qb8oi2YZOwI/dzGjqL+XiEsTljCziBAYxJ4KrwWmiLmqyCDcIpVaEvCmTFJKH0YDc254gt\nxlPcSOHahFPamEewE61x3IsfwgKzoB0b4rghxufFRMH0vpL5WmzDK6wvJqorCLZipKHdfhv/N8Rx\nOYzCaqGR4R0eYzGuVogtxsL4bMahOK6J3j5iEZ5lFavGTaGECugUdm1mhvi1RSYKmIdzWQ1EHBP6\naXQCd0DopWVZxbZie8lch7Co5Rnir2NawvwtTM1qAg8MVUopRuET3ggVhPTymyws6Infr9GCycYK\nRmbhA14kcMeFm3RXBQ2owwwcTuG/42v0Uy+UeCq6hNovxWrhpHZWMNMpvSRqheu9toIGrIr5Vqbw\n0yP/W38n3WJL8F5yM/fFZ7mTqhdO6nIKP4ALWFNGo4CFgum08jsYn+3lRFrwEuNS+KaYpKuMxg5s\nKZcEU6QbLcY9PEyYr8NJ4bNtb1pwM+4INToolMfkkncuCj0yKHw49mBFglY3xmcwfEZYXCkmCFVy\nP+bqx7WYrwd3hSv8NGZnyPMf//E38Qt8gHtDqthFywAAAABJRU5ErkJggg==\n", | |
"text/latex": [ | |
"$$A \\wedge B$$" | |
], | |
"text/plain": [ | |
"A ∧ B" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#十分条件から、第2項を取り除く には?=>第2項.args[1]をFalseで置き換えると、Orで結ばれているので、それ以外が残る\n", | |
"sufficient_condition.subs(sufficient_condition.args[1],False) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>A</th>\n", | |
" <th>B</th>\n", | |
" <th>C</th>\n", | |
" <th>Y</th>\n", | |
" <th>Or(And(A, B), And(B, C))</th>\n", | |
" <th>And(A, B)</th>\n", | |
" <th>And(B, C)</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>1.0</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.9</td>\n", | |
" <td>1.0</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>0.9</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.4</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>0.9</td>\n", | |
" <td>0.9</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.9</td>\n", | |
" <td>0.9</td>\n", | |
" <td>0.9</td>\n", | |
" <td>0.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>0.8</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.3</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>0.7</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.4</td>\n", | |
" <td>0.5</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>0.4</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.3</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>0.3</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.9</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.7</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>0.4</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.6</td>\n", | |
" <td>0.7</td>\n", | |
" <td>0.6</td>\n", | |
" <td>0.4</td>\n", | |
" <td>0.6</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>9</th>\n", | |
" <td>0.1</td>\n", | |
" <td>0.4</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" <td>0.1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>10</th>\n", | |
" <td>0.0</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.8</td>\n", | |
" <td>0.2</td>\n", | |
" <td>0.3</td>\n", | |
" <td>0.0</td>\n", | |
" <td>0.3</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" A B C Y Or(And(A, B), And(B, C)) And(A, B) And(B, C)\n", | |
"1 1.0 0.8 0.9 1.0 0.8 0.8 0.8\n", | |
"2 0.9 0.7 0.4 0.8 0.7 0.7 0.4\n", | |
"3 0.9 0.9 0.8 0.9 0.9 0.9 0.8\n", | |
"4 0.8 0.3 0.7 0.3 0.3 0.3 0.3\n", | |
"5 0.7 0.8 0.4 0.5 0.7 0.7 0.4\n", | |
"6 0.4 0.3 0.7 0.3 0.3 0.3 0.3\n", | |
"7 0.3 0.7 0.9 0.8 0.7 0.3 0.7\n", | |
"8 0.4 0.8 0.6 0.7 0.6 0.4 0.6\n", | |
"9 0.1 0.4 0.1 0.0 0.1 0.1 0.1\n", | |
"10 0.0 0.3 0.8 0.2 0.3 0.0 0.3" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df_s = add_membershipvalues(df, ['A','B','C'],\n", | |
" [sufficient_condition,\n", | |
" sufficient_condition.args[0],\n", | |
" sufficient_condition.args[1],\n", | |
" sufficient_condition.subs(sufficient_condition.args[0],False),\n", | |
" sufficient_condition.subs(sufficient_condition.args[1],False)])\n", | |
"#今回はたまたま、第1項と十分条件から第2項を取り除いたもの、第2項と十分条件から第1項を取り除いたものが同じだったので3列しか増えてない\n", | |
"df_s" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'0.926'" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Or(And(A, B), And(B, C))の十分性\n", | |
"inclusion_index(df_s, 'Y', 'Or(And(A, B), And(B, C))')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'0.909'" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#S が Y をどの程度カバーしているかを示す被覆度(粗被覆度)\n", | |
"inclusion_index(df_s, 'Or(And(A, B), And(B, C))', 'Y')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'0.933'" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# And(A, B)の十分性\n", | |
"inclusion_index(df_s, 'Y', 'And(A, B)')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'0.764'" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# And(A, B)の粗被覆度\n", | |
"inclusion_index(df_s, 'And(A, B)', 'Y' )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'0.091'" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#And(A, B)の固有被覆度 = 十分条件の被覆度 - 十分条件からAnd(A, B)を除いた時の被覆度\n", | |
"('%03.3f' % (float(inclusion_index(df_s, 'Or(And(A, B), And(B, C))', 'Y')) - float(inclusion_index(df_s, 'And(B, C)', 'Y' ))))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'0.957'" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# And(B, C)の十分性\n", | |
"inclusion_index(df_s, 'Y', 'And(B, C)')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'0.818'" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# And(B, C)の粗被覆度\n", | |
"inclusion_index(df_s, 'And(B, C)', 'Y' )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'0.145'" | |
] | |
}, | |
"execution_count": 21, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#And(B, C)の固有被覆度 = 十分条件の被覆度 - 十分条件からAnd(B, C)を除いた時の被覆度\n", | |
"('%03.3f' % (float(inclusion_index(df_s, 'Or(And(A, B), And(B, C))', 'Y')) - float(inclusion_index(df_s, 'And(A, B)', 'Y' ))))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.4.3" | |
}, | |
"widgets": { | |
"state": {}, | |
"version": "1.1.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment