2017-12-08 2 views
0

私はPythonと線形回帰を学んでいます。私は初心者なので、問題は解決できません。KeyError: 'DystopiaResidual'、パンダのエラー

マイコード:

import numpy as np # linear algebra 
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv) 
from sklearn.utils import shuffle 
from sklearn.metrics import confusion_matrix 
import seaborn as sns 
import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 
from sklearn.preprocessing import StandardScaler 
from sklearn.manifold import TSNE 

from subprocess import check_output 

print(check_output(["ls", "input"]).decode("utf8")) 
df1 = pd.read_csv("input/2015.csv") 
df2 = pd.read_csv("input/2016.csv") 
df1.columns 
df1.head(2) 
sns.regplot(x='Standard Error',y='Happiness Score' ,data=df1) 
sns.regplot(x='Economy (GDP per Capita)',y='Happiness Score' ,data=df1) 

fr=['Standard Error', 'Economy (GDP per Capita)', 'Family','Health (Life Expectancy)', 'Freedom', 'Trust (Government Corruption)','Generosity', 'DystopiaResidual'] 


plt.figure(figsize=(12,28*4)) 
gs = gridspec.GridSpec(28, 1) 
for i, cn in enumerate(fr): 
    ax = plt.subplot(gs[i]) 
    #sns.distplot(df1[cn], bins=50) 
    sns.regplot(x=df1[cn],y='Happiness Score' ,data=df1) 
    ax.set_xlabel('') 
    ax.set_title('Regrassion of feature: ' + str(cn)) 
plt.show() 

エラー:私は問題が何であるかを理解していない

Traceback (most recent call last): 
    File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexes/base.py", 
line 2522, in get_loc 
    return self._engine.get_loc(key) 
    File "pandas/_libs/index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc 
    File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc 
    File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item 
    File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item 
KeyError: 'DystopiaResidual' 

答えて

1

サンプルデータセットとしてWorld Happiness Reportを使用しているようです。

コードに入力ミスがあります。あなたはスペースが不足している

fr=['Standard Error', 'Economy (GDP per Capita)', 'Family','Health (Life Expectancy)', 'Freedom', 'Trust (Government Corruption)','Generosity', 'Dystopia Residual']

フィールド参照はする必要があります。

関連する問題