前提・実現したいこと
kaggle の Titanic Tutorial を進めたい
発生している問題・エラーメッセージ
pandas の DataFrame において dictionary を使用するとそこで callable できないといわれます. kaggle の Notebook でそのままデータを読み込んでいます. エラーメッセージの行数はセルごとの行数ですが、ソースコードは一応全て載せるので下の方となります。
TypeError Traceback (most recent call last)
<ipython-input-31-c4b3e7ab234b> in <module>
12
13 dic_testdata = {'PassengerId': test_data.PassengerId, 'Survived': predictions}
---> 14 output = pd.DataFrame(dic_testdata, index = False)
15 output.to_csv('my_submission.csv', index=False)
16 print("Your submission was successfully saved!")
TypeError: 'dict' object is not callable
該当のソースコード
python3.0
1# This Python 3 environment comes with many helpful analytics libraries installed 2# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python 3# For example, here's several helpful packages to load 4 5import numpy as np # linear algebra 6import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv) 7 8# Input data files are available in the read-only "../input/" directory 9# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory 10 11import os 12for dirname, _, filenames in os.walk('/kaggle/input'): 13 for filename in filenames: 14 print(os.path.join(dirname, filename)) 15 16# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 17# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session 18 19 20 21 22train_data = pd.read_csv('/kaggle/input/titanic/train.csv') 23test_data = pd.read_csv('/kaggle/input/titanic/test.csv') 24train_data.head() 25test_data.head() 26 27 28 29women = train_data.loc[train_data.Sex == 'female']['Survived'] 30rate_women = sum(women)/len(women) 31print (rate_women) 32men = train_data.loc[train_data.Sex == 'male']["Survived"] 33rate_men = sum(men)/len(men) 34print(rate_men) 35 36 37 38 39from sklearn.ensemble import RandomForestClassifier 40 41y = train_data["Survived"] 42 43features = ["Pclass", "Sex", "SibSp", "Parch"] 44X = pd.get_dummies(train_data[features]) 45X_test = pd.get_dummies(test_data[features]) 46 47model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1) 48model.fit(X, y) 49predictions = model.predict(X_test) 50 51dic_testdata = {'PassengerId': test_data.PassengerId, 'Survived': predictions} 52output = pd.DataFrame(dic_testdata) 53output.to_csv('my_submission.csv', index=False) 54print("Your submission was successfully saved!")
回答1件
あなたの回答
tips
プレビュー