0
0

More than 1 year has passed since last update.

SIGNATEの【練習問題】”天秤のバランス分類”をやってみた

Posted at

必要なライブラリのインポート

#必要なライブラリのインポート
import pandas as pd

ファイル取得

訓練データと検証データをそれぞれ読み取る。

#訓練データはtrain,検証データはtest
train = pd.read_table('train.tsv', index_col=0)
test = pd.read_table('test.tsv')

testは後々の操作のためにindex_colを設定していない。

データ形式の確認

train

SS 2021-10-01 0.19.52.jpg

test

SS 2021-10-01 0.22.39.jpg

分類用の関数を作成

(距離)×(重さ)が左右のどちらに寄っているか、で分類した。平衡は”1”、左は”0”、右は”2”。

def position_judge(l_weight,l_distance,r_weight,r_distance,results_list):
    left = l_weight*l_distance
    right = r_weight*r_distance
    #平衡
    if left == right:
        results_list.append(int(1))
        pass
    #左
    elif left > right:
        results_list.append(int(0))
        pass
    #右
    elif right > left:
        results_list.append(int(2))
        pass

結果をリストに格納

# print(train.iloc[c]["left_distance"])
results = []
for c in range(len(test)):
    position_judge(test.iloc[c]["left_weight"],test.iloc[c]["left_distance"],test.iloc[c]["right_weight"],test.iloc[c]["right_distance"],results)

idと結果をまとめて1つのDataFrameにする

#結果リストをDataGrame型に変換
df_result = pd.DataFrame(results)
#testのid
zero_index = pd.DataFrame(test.iloc[:,0])

#idと結果を一つのDataFrameにして、カラム名を整理
df_results = pd.concat([zero_index,df_result],axis = 1)
df_results = df_results.rename(columns={0:1,'Unnamed: 0': 0})

結果をcsvファイルに出力

df_results.to_csv('results.csv',index = False,header = False)

  

  
結果は評価関数Accuracyで1.0000000でした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0