質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

374閲覧

条件に対応した期待値を取り出す

syen2501

総合スコア38

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

1グッド

2クリップ

投稿2017/09/18 05:11

編集2017/09/21 16:12

#前提・実演したいこと
条件の組み合わせから、期待値を取り出し表示できるようにしたい。

例えば、表の表側(front_table)と表頭(table_head)、そして期待値として、expected_valueをそれぞれ下記のようにリストにまとめる。

python

1front_table = { 2 '操作':['電源ボタン','ビデオボタン','静止ボタン'] 3} 4 5table_head = { 6 '期待動作':['動作'] 7} 8 9expected_value = [ 10 ['電源ボタンを押す'], 11 ['ビデオボタンを押す'], 12 ['静止ボタンを押す'] 13]

そして表側と表頭の条件から下記のように真偽を網羅的に表示させる。

python

1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2電源ボタン T T T T T T T T F F F F F F F F 3ビデオボタン T T T T F F F F T T T T F F F F 4静止ボタン T T F F T T F F T T F F T T F F 5動作 T F T F T F T F T F T F T F T F 6``` 7実装したいことは、例えば、 8「電源ボタン」と「動作」が「T」でそれ以外が「F」だったら 99列目のところに 10``` 11 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 12電源ボタン T T T T T T T T F F F F F F F F 13ビデオボタン T T T T F F F F T T T T F F F F 14静止ボタン T T F F T T F F T T F F T T F F 15動作 T F T F T F T F T F T F T F T F 16------------------------------------------------------------ 17アクション1 電源ボタンを押す 18``` 19のように対応させて表として表示させたい。 20残りの条件も 21・「ビデオボタン」と「動作」 22・「静止ボタン」と「動作」 23と対応している。 24 25出来れば、条件や期待値が変わっても対応できるように一般化したい。 26 27#行ったこと 28条件を与えて、真偽を網羅的に表示するところまでを行った。 29コードは下記の通り。 30```python 31#表側,表頭を指定して、それに対応した期待値を抽出 32class Table: 33 def __init__(self, row_names, column_names, data): 34 self.row_names = row_names 35 self.column_names = column_names 36 self.data = data 37 38 def get_datum(self, row_name, column_name): 39 row_index = self.row_names.index(row_name) 40 column_index = self.column_names.index(column_name) 41 42 return self.data[row_index][column_index] 43 44table = Table(front_table['操作'],table_head['期待動作'],expected_value) 45dat = table.get_datum('ビデオボタン','動作') 46 47print(dat) 48 49 50#真偽のマーク 51true_mark = 'T' 52false_mark = 'F' 53 54#条件(表側,表頭)に対しての真偽の組み合わせを表示 55def combination_TF(front_table,table_head): 56 table_index = front_table + table_head 57 58 #真偽の直積 59 cartesian_product = itertools.product(*([true_mark,false_mark],)*len(table_index)) 60 61 #真偽の直積の結果を表で表示 62 dataTF = [[] for element_count in table_index] 63 for com in cartesian_product: 64 for element_count,authenticity in enumerate(com): 65 dataTF[element_count].append(authenticity) 66 return pd.DataFrame(dataTF,index=table_index) 67 68print(combination_TF(front_table['操作'],table_head['期待動作'])) 69 70``` 71#実行結果 72``` 73ビデオボタンを押す 74 75 76 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 77電源ボタン T T T T T T T T F F F F F F F F 78ビデオボタン T T T T F F F F T T T T F F F F 79静止ボタン T T F F T T F F T T F F T T F F 80動作 T F T F T F T F T F T F T F T F 81``` 82長々と申し訳ありませんが、よろしくお願いします。 83上記のコードにあるクラスなどは無視しても構いません。 84 85とりあえず、条件の「T」と「F」の組み合わせから、期待値を取り出すのを一般化できればと思っています。 86 87#自分の解決方法 88自分の解決方法として、 89例えば、実行結果の下の方の表で「電源ボタン」と「動作」が'T'でそれ以外が'F'だった時にexpected_valueの中の「電源ボタンを押す」という期待値をその列上に出したい。 90 91プログラムとして、 92・列と行をfor文で回していき、「電源ボタン」と「動作」が'T'だったらその場所の列の 93位置(数値)を返す。 94 95例は以下のコードで実現しようと考えている。 96 97```python 98expected_data = [[] for j in range(len(df.index))] 99for index_number in range(len(df.index)): 100 for columns_number in range(len(df.columns)): 101 if df[columns_number][index_number] == 'T': 102 index_number += len(df.index) - 1 103 if df[columns_number][index_number] == 'T': 104 expected_data[index_number].append(columns_number) 105print(expected_data) 106``` 107#実行結果 108Traceback (most recent call last): 109 File "C:\Anaconda\lib\site-packages\pandas\core\indexes\base.py", line 2428, in get_value 110 tz=getattr(series.dtype, 'tz', None)) 111 File "pandas\_libs\index.pyx", line 98, in pandas._libs.index.IndexEngine.get_value (pandas\_libs\index.c:4363) 112 File "pandas\_libs\index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value (pandas\_libs\index.c:4046) 113 File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5085) 114 File "pandas\_libs\hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20405) 115 File "pandas\_libs\hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20359) 116KeyError: 4 117 118During handling of the above exception, another exception occurred: 119 120Traceback (most recent call last): 121 File "C:\atom\decision.py", line 72, in <module> 122 if df[columns_number][index_number] == 'T': 123 File "C:\Anaconda\lib\site-packages\pandas\core\series.py", line 601, in __getitem__ 124 result = self.index.get_value(self, key) 125 File "C:\Anaconda\lib\site-packages\pandas\core\indexes\base.py", line 2434, in get_value 126 return libts.get_value_box(s, key) 127 File "pandas\_libs\tslib.pyx", line 923, in pandas._libs.tslib.get_value_box (pandas\_libs\tslib.c:18843) 128 File "pandas\_libs\tslib.pyx", line 939, in pandas._libs.tslib.get_value_box (pandas\_libs\tslib.c:18560) 129IndexError: index out of bounds 130 131実行結果はエラーが出ているので、教えてほしいと思っています。
manzyun👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

回答として、以下のコードを作成した。
表示結果はhtmlで表示した。

python

1import pandas as pd 2import itertools 3pd.set_option('display.unicode.east_asian_width',True) 4 5front_table = { 6 '操作':['ボタン','ビデオ','静止'] 7} 8 9expected_value = [ 10 '電源オフする', 11 'ビデオモードオン', 12 '静止する' 13} 14 15#真偽のマーク 16true_mark = 'T' 17false_mark = 'F' 18 19#真偽の組み合わせを網羅的に行う関数 20def combination_TF(front_table): 21 table_index = front_table 22 combination = itertools.product(*[[true_mark,false_mark]]*len(table_index)) 23 return (combination,table_index) 24 25data, conditions = combination_TF(front_table['操作']) 26condition_df = pd.DataFrame(list(data),columns=conditions) 27 28#条件の組み合わせ(T,F)によって期待値を表示 29action_df = condition_df.copy() 30action_df.columns = [s + '_action' for s in action_df.columns] 31 32action_df = action_df.apply(lambda series:[expected_value[i] if truth_value == true_mark else '' for i,truth_value in enumerate(series)],axis=1) 33print(action_df.T) 34whole_table = pd.concat([condition_df,action_df],axis=1) 35with open('table.html','w') as f: 36 whole_table.T.to_html(f) 37

#実行結果
イメージ説明

投稿2017/09/24 15:20

syen2501

総合スコア38

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ごめんなさい。とりあえず解決のヒントになればと思いついた実装方法です。

とりあえず思いついた方法としては

  • T / F の値を真偽値の値に置きなおして、予め組んだリスト(このリストはCSVから取り込むでもOK)とand演算する。
  • T / F0 / 1 に変換し、合算。そしてビット乗算で判定する

の2つです。おそらくわかりやすいのは前者ですが、データ圧縮や処理速度を考えると後者がお勧めです。

実装例は後程時間があれば書いてみようと思います。

投稿2017/09/20 06:36

manzyun

総合スコア2244

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

syen2501

2017/09/20 06:59

ありがとうございます。 ヒントを考えてくれただけでも嬉しいです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問