前提・実現したいこと
pytonを用いたデータ分析の学習中、下記のエラーが発生しました。
解決方法ほご教示ください。
※学習時に参考していたサイト
(https://qiita.com/takapy0210/items/834558aa182e3cf6a8b4)
発生している問題・エラーメッセージ
エラーメッセージ --------------------------------------------------------------------------- KeyError Traceback (most recent call last) /Users/NishidaMasanori/anaconda/lib/python3.6/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance) 2133 try: -> 2134 return self._engine.get_loc(key) 2135 except KeyError: pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4433)() pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4279)() pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13742)() pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13696)() KeyError: 'y' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-18-1bf90a15a19d> in <module>() 3 4 X_train.head() ----> 5 y_train = train['y'] /Users/NishidaMasanori/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key) 2057 return self._getitem_multilevel(key) 2058 else: -> 2059 return self._getitem_column(key) 2060 2061 def _getitem_column(self, key): /Users/NishidaMasanori/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in _getitem_column(self, key) 2064 # get column 2065 if self.columns.is_unique: -> 2066 return self._get_item_cache(key) 2067 2068 # duplicate columns & possible reduce dimensionality /Users/NishidaMasanori/anaconda/lib/python3.6/site-packages/pandas/core/generic.py in _get_item_cache(self, item) 1384 res = cache.get(item) 1385 if res is None: -> 1386 values = self._data.get(item) 1387 res = self._box_item_values(item, values) 1388 cache[item] = res /Users/NishidaMasanori/anaconda/lib/python3.6/site-packages/pandas/core/internals.py in get(self, item, fastpath) 3541 3542 if not isnull(item): -> 3543 loc = self.items.get_loc(item) 3544 else: 3545 indexer = np.arange(len(self.items))[isnull(self.items)] /Users/NishidaMasanori/anaconda/lib/python3.6/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance) 2134 return self._engine.get_loc(key) 2135 except KeyError: -> 2136 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2137 2138 indexer = self.get_indexer([key], method=method, tolerance=tolerance) pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4433)() pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4279)() pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13742)() pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13696)() KeyError: 'y'
該当のソースコード
python
1ソースコード(エラー出力部分) 2# 説明変数として使用する week と temperature をダミー変数化しながら X_trainに設定 3X_train = pd.get_dummies(train[['week', 'temperature']]) 4 5X_train.head() 6y_train = train['y'] 7 8 9ソースコード(全体) 10import pandas as pd 11import numpy as np 12from matplotlib import pyplot as plt 13from sklearn.linear_model import LinearRegression as LR # 線形回帰のモデル 14%matplotlib inline 15 16train = pd.read_csv('/Users/ユーザ名/Downloads/test.csv') # 学習データの読み込み 17test = pd.read_csv('/Users/ユーザ名/Downloads/train.csv') # 検証データの読み込み 18sample = pd.read_csv('/Users/ユーザ名/Downloads/sample.csv', header=None) # 提出用サンプルデータの読み込み 19 20# データ確認 21train.head() 22 23train['week'].value_counts() 24 25# 説明変数として使用する week と temperature をダミー変数化しながら X_trainに設定 26X_train = pd.get_dummies(train[['week', 'temperature']]) 27 28X_train.head() 29y_train = train['y'] 30
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。