0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

初対面の人との相性を機械学習を用いて導く

Last updated at Posted at 2019-04-24

やること

過去に自分が出会った人々のステータスを複数のカラムに分け、それぞれ何段階かで評価する。
評価したデータを基にモデルを作り、初対面の人のステータスを当てはめ相性を予測する。

目的

  • 機械学習の理解を深めるため
  • 相手との相性を考えるときに、自分が何を重要視しているのか気になったため
  • なんとなく面白そうだと思ったから

本編

今回使ったデータの例(名前などの個人情報がデータに含まれているため実際に使用したデータの掲載は差し控えさせていただきます)

  • train :muscle_tone2:
sample_train_my_friends.csv
name,age,school,affinity,sex,last_school,fun,noisy,clever
A,20,high,good,female,university,common,yes,common
B,21,university,normal,male,university,yes,yes,common
C ,20,juniorhigh,bad,male,high,no,common,no
...

  • test :writing_hand_tone2:
sample_test_my_friends.csv
name,age,school,affinity,sex,last_school,fun,noisy,clever
D,21,high,good,male,university,no,yes,no
E,20,university,bad,female,high,yes,yes,common
...

<カラムの説明>

  • name=名前
  • age=年齢
  • school=いつ頃出会ったか
  • affinity=相性(最終的にはこれを予測できるようにしたい)
  • sex=性別
  • last_school=最終学歴
  • fun=面白いやつか
  • noisy=うるさいやつか
  • clever=賢いやつか

コード

my_friends_affinity.py
#ライブラリのインポート
import pandas as pd
from sklearn import tree

#csvファイルの読み込み
train = pd.read_csv("train_my_friends_data.csv")
test = pd.read_csv("test_my_friends_data.csv")

#trainの文字列データを数字に変更
train["school"][train["school"] == "juniorhigh" ] = 0
train["school"][train["school"] == "high"] = 1
train["school"][train["school"] == "university"] = 2
train["affinity"][train["affinity"] == "sobad"] = 0
train["affinity"][train["affinity"] == "bad"] = 1
train["affinity"][train["affinity"] == "normal"] = 2
train["affinity"][train["affinity"] == "good"] = 3
train["affinity"][train["affinity"] == "verygood"] = 4 
train["sex"][train["sex"] == "male"] = 0
train["sex"][train["sex"] == "female"] = 1
train["last_school"][train["last_school"] == "high"] = 0
train["last_school"][train["last_school"] == "university"] = 1
train["fun"][train["fun"] == "no"] = 0
train["fun"][train["fun"] == "common"] = 1
train["fun"][train["fun"] == "yes"] = 2
train["noisy"][train["noisy"] == "no"] = 0
train["noisy"][train["noisy"] == "common"] = 1
train["noisy"][train["noisy"] == "yes"] = 2
train["clever"][train["clever"] == "no"] = 0
train["clever"][train["clever"] == "common"] = 1
train["clever"][train["clever"] == "yes"] = 2

#testデータの文字列データも数字に変更
test["school"][test["school"] == "juniorhigh" ] = 0
test["school"][test["school"] == "high"] = 1
test["school"][test["school"] == "university"] = 2
test["affinity"][test["affinity"] == "sobad"] = 0
test["affinity"][test["affinity"] == "bad"] = 1
test["affinity"][test["affinity"] == "normal"] = 2
test["affinity"][test["affinity"] == "good"] = 3
test["affinity"][test["affinity"] == "verygood"] = 4 
test["sex"][test["sex"] == "male"] = 0
test["sex"][test["sex"] == "female"] = 1
test["last_school"][test["last_school"] == "high"] = 0
test["last_school"][test["last_school"] == "university"] = 1
test["fun"][test["fun"] == "no"] = 0
test["fun"][test["fun"] == "common"] = 1
test["fun"][test["fun"] == "yes"] = 2
test["noisy"][test["noisy"] == "no"] = 0
test["noisy"][test["noisy"] == "common"] = 1
test["noisy"][test["noisy"] == "yes"] = 2
test["clever"][test["clever"] == "no"] = 0
test["clever"][test["clever"] == "common"] = 1
test["clever"][test["clever"] == "yes"] = 2


#trainデータの"affinity"を目的変数とし、"school","sex","last_school","fun","noisy","clever"を説明変数とする
train_target = train["affinity"].values
train_features = train[["school","sex","last_school","fun","noisy","clever"]].values

#testデータの"school","sex","last_school","fun","noisy","clever"を説明変数とする
test_target = test["affinity"].values
test_features = test[["school","sex","last_school","fun","noisy","clever"]].value

#train,testデータをそれぞれint型に変換する
train_target = train_target.astype("int")
train_features = train_features.astype("int")
test_target = test_target.astype("int")
test_features = test_features.astype("int")

#モデルを作り、機械学習を行う
my_tree = tree.DecisionTreeClassifier()
my_tree.fit(train_features,train_target)

#結果を予測する
prediction = my_tree.predict(test_features)
print(prediction)
print(test_target)

>>>[3 1 3 2 3]
   [3 3 2 4 3]

行き詰ってしまったところ

  • 目的変数と説明変数をint型に変更するところがなかなか思いつかず、詰まってしまった
  • どのモデルを使うべきかで悩んでしまった

課題と改善点

  • 文字列のデータを数字に変換する際に長々とコードを書いてしまったが、もう少しコンパクトにかけたような気がする
  • 予測の正解率が2/5(40%)と低くなってしまった⇒参考にするカラムが判断材料として適していたかどうかを再考する
  • Qiita初投稿だったが、本文を見やすくできるところは改善していく
0
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?