
1.前提・実現したいこと
KaggleチュートリアルTitanicについて、Ageの欠損値補完をしているブログ(下記※)を見つけました。
※https://qiita.com/jun40vn/items/d8a1f71fae680589e05c「KaggleチュートリアルTitanicで上位2%以内に入るノウハウ」
上記のAgeの欠損値補完コードを、利用させてもらいましたが、エラーが発生します。対処方法をお願いします。
2.発生している問題・エラーメッセージ
3.のソースコード実行で下記のエラーメッセージが発生
ValueError Traceback (most recent call last)
<ipython-input-12-ec4b30dc9be2> in <module>()
22
23 # 推定モデルを使って、テストデータのAgeを予測し、補完
---> 24 predictedAges = rfr.predict(unknown_age[:, 1::])
25 df.loc[(df.Age.isnull()), 'Age'] = predictedAges
26
3 frames
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
806 "Found array with %d sample(s) (shape=%s) while a"
807 " minimum of %d is required%s."
--> 808 % (n_samples, array.shape, ensure_min_samples, context)
809 )
810
ValueError: Found array with 0 sample(s) (shape=(0, 5)) while a minimum of 1 is required.
3.該当のソースコード
python3
1from google.colab import drive 2drive.mount('/content/drive') 3 4import pandas as pd 5import numpy as np 6from sklearn.model_selection import train_test_split, GridSearchCV 7from pandas import DataFrame, Series 8import matplotlib.pyplot as plt 9import seaborn as sns 10import warnings 11warnings.filterwarnings('ignore') 12 13#colabの中のフォルダーへのパス 14path = "/content/drive/MyDrive/Colab Notebooks/(自分のパス)" 15train = pd.read_csv(path + 'data/train.csv') 16test = pd.read_csv(path + 'data/test.csv') 17 18# train_dataとtest_dataの連結 19test['Perished'] = np.nan 20df = pd.concat([train, test], ignore_index=True, sort=False) 21 22# ------------ Age ------------ 23# Age を Pclass, Sex, Parch, SibSp からランダムフォレストで推定 24from sklearn.ensemble import RandomForestRegressor 25 26# 推定に使用する項目を指定 27age_df = df[['Age', 'Pclass','Sex','Parch','SibSp']] 28 29# ラベル特徴量をワンホットエンコーディング 30age_df=pd.get_dummies(age_df) 31 32# 学習データとテストデータに分離し、numpyに変換 33known_age = age_df[age_df.Age.notnull()].values 34unknown_age = age_df[age_df.Age.isnull()].values 35 36# 学習データをX, yに分離 37X = known_age[:, 1:] 38y = known_age[:, 0] 39 40# ランダムフォレストで推定モデルを構築 41rfr = RandomForestRegressor(random_state=0, n_estimators=100, n_jobs=-1) 42rfr.fit(X, y) 43 44# 推定モデルを使って、テストデータのAgeを予測し、補完 45predictedAges = rfr.predict(unknown_age[:, 1::]) 46df.loc[(df.Age.isnull()), 'Age'] = predictedAges 47 48# 年齢別生存曲線と死亡曲線 49facet = sns.FacetGrid(df[0:890], hue='Perished',aspect=2) 50facet.map(sns.kdeplot,'Age',shade= True) 51facet.set(xlim=(0, df.loc[0:890,'Age'].max())) 52facet.add_legend() 53plt.show()
4.自分で調べたことや試したこと
Ageの欠損値補完をしているブログと、データ構造( dfの情報)は同じなのを
確認しているので、普通なら動くと思ったが、ダメであった。
データ構造( dfの情報)は、以下のとおり
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1309 entries, 0 to 1308
Data columns (total 12 columns):
Column Non-Null Count Dtype
0 PassengerId 1309 non-null int64
1 Survived 891 non-null float64
2 Pclass 1309 non-null int64
3 Name 1309 non-null object
4 Sex 1309 non-null object
5 Age 1046 non-null float64
6 SibSp 1309 non-null int64
7 Parch 1309 non-null int64
8 Ticket 1309 non-null object
9 Fare 1308 non-null float64
10 Cabin 295 non-null object
11 Embarked 1307 non-null object
5.使っているツールのバージョンなど補足情報
開発環境:Google Colaboratory
プログラム言語:python3
OS:windows10 Home
CPU:Intel(R) Core(TM) i7-7500U CPU@2.70GHz 2.90GHz


回答1件
あなたの回答
tips
プレビュー