0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SIGNATEでアヤメの分類に参加した

Last updated at Posted at 2023-12-08

SIGNATEで、タイタニックの生存予測に続いて、アヤメの分類に参加しました。

アヤメの分類

初心者がチャレンジしやすいアヤメの分類に参加しました。

このコンペでは、3種類のアヤメの品種を分類するモデルを作成します。

まず、データタブから学習用データ(train.tsv)、評価用データ(test.tsv)、応募用サンプルファイル(sample_submit.csv)をダウンロードしました。
解析環境にGoogle Colaboratoryを用いるため、ダウンロードしたデータはGoogle Driveに移動します。
私は、Google Driveにirisディレクトリを作成し、irisディレクトリにtrain.tsv、test.tsv、sample_submit.csvを移動しました。

そして、Google Colaboratoryを起動し、コードを書いていきます。

ライブラリのインポート

# NumPy、pandas、seaborn、matplotlib.pyplotのインポート
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

データの読み込み

# データの読み込み
train = pd.read_csv("/content/drive/MyDrive/iris/train.tsv", sep='\t')
test = pd.read_csv("/content/drive/MyDrive/iris/test.tsv", sep='\t')
sample_submit = pd.read_csv("/content/drive/MyDrive/iris/sample_submit.csv", header=None)

データの可視化

sns.countplot(x="class", data=train)
plt.show()

Unknown-5.png

# データの可視化
sns.pairplot(train[['class', 'sepal length in cm', 'sepal width in cm', 'petal length in cm', 'petal width in cm']], hue="class")
plt.show()

Unknown-4.png

ラベルエンコーディング

# ラベルエンコーディング
from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()
train['class'] = le.fit_transform(train['class'])
print(le.classes_)
['Iris-setosa' 'Iris-versicolor' 'Iris-virginica']

trainとtestの連結

# trainとtestの連結
data = pd.concat([train, test], sort=False)
# 欠損値の確認
data.isnull().sum()

学習データとテストデータの抽出

# 学習データとテストデータの抽出
train = data[:len(train)]
test = data[len(train):]

y_train = train['class']
X_train = train.drop('class', axis = 1)
X_test = test.drop('class', axis = 1)

ランダムフォレストで学習

from sklearn.ensemble import RandomForestClassifier

clf = RandomForestClassifier()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
clf.score(X_train, y_train)

スコアは1.0でした。

y_pred[:10]
array([1., 1., 2., 1., 0., 0., 2., 1., 0., 2.])

y_predをint型に変換します。

y_pred = y_pred.astype(int)
y_pred[:10]
array([1, 1, 2, 1, 0, 0, 2, 1, 0, 2])

デコーディングを行います。

y_pred = le.inverse_transform(y_pred)
y_pred[:10]
array(['Iris-versicolor', 'Iris-versicolor', 'Iris-virginica',
       'Iris-versicolor', 'Iris-setosa', 'Iris-setosa', 'Iris-virginica',
       'Iris-versicolor', 'Iris-setosa', 'Iris-virginica'], dtype=object)
print(le.classes_)
['Iris-setosa' 'Iris-versicolor' 'Iris-virginica']

データの出力

# データの出力
sub = sample_submit
sub[1] = y_pred
sub.to_csv("/content/drive/MyDrive/iris/submit.csv", index=False, header=False)

submit.csvが出力されたことを確認したら、コンペのサイトに移動します。
https://signate.jp/competitions/115
スクリーンショット 2023-12-09 0.27.20.png

右下の投稿をクリックし、ファイルを選択からsubmit.csvをアップロードします。
これにより、結果を提出することができました。
暫定評価は0.9733333でした。
アヤメの分類は取り組みやすいコンペなので、初心者におすすめです。
SIGNATEには、アヤメの分類以外にも練習問題があるので、データ分析スキル向上のために取り組みたいです。

参考文献

https://www.kaggle.com/code/sishihara/upura-kaggle-tutorial-01-first-submission
https://ct-innovation01.xyz/DL-Freetime/kaggle-003/
https://gotutiyan.hatenablog.com/entry/2020/09/08/122621

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?