2017-11-20 13 views
0

これはおそらくかなり個人的な質問ですが、私は誰に助けてくれると思いますか? Anacondaを使ってpythonをインストールし、Jupyterノートブックを使用しています。私は2つのCSVファイルを持っています。Numpy TypeError:整数が必要です

products.head() 
    ID_FUPID FUPID 
    0 1 674563 
    1 2 674597 
    2 3 674606 
    3 4 694776 
    4 5 694788 

製品には、製品IDと製品番号が含まれています。顧客が製品に与える

ratings.head() 
ID_CUSTOMER ID_FUPID RATING 
0 1 216  1 
1 2 390  1 
2 3 851  5 
3 4 5897 1 
4 5 9341 1 

評価containt顧客のID、のproductIDと評価。 私が作成した表のように:行としてのproductID =列と得意先とのマトリックスで正しくデータを示している

M = ratings.pivot_table(index=['ID_CUSTOMER'],columns=['ID_FUPID'],values='RATING') 

。私はここに製品間のピアソンcollerationをカウントしたい

は、ピアソン関数である:

def pearson(s1, s2): 
    import numpy as np 
    """take two pd.series objects and return a pearson correlation""" 
    s1_c = s1 - s1.mean() 
    s2_c = s2 - s2.mean() 
    return np.sum(s1_c * s2_c)/np.sqrt(np.sum(s1_c ** 2) * np.sum(s2_c ** 2)) 

私は( '[M '21]、M ['17]')ピアソンをカウントしようとしているとき私はエラーを次しまっ:

TypeError         Traceback (most recent call last) 
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() 

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item() 

TypeError: an integer is required 

During handling of the above exception, another exception occurred: 

KeyError         Traceback (most recent call last) 
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 
    2441    try: 
-> 2442     return self._engine.get_loc(key) 
    2443    except KeyError: 

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() 

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() 

KeyError: '17' 

During handling of the above exception, another exception occurred: 

TypeError         Traceback (most recent call last) 
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() 

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item() 

TypeError: an integer is required 

During handling of the above exception, another exception occurred: 

KeyError         Traceback (most recent call last) 
<ipython-input-277-d4ead225b6ab> in <module>() 
----> 1 pearson(M['17'], M['21']) 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key) 
    1962    return self._getitem_multilevel(key) 
    1963   else: 
-> 1964    return self._getitem_column(key) 
    1965 
    1966  def _getitem_column(self, key): 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key) 
    1969   # get column 
    1970   if self.columns.is_unique: 
-> 1971    return self._get_item_cache(key) 
    1972 
    1973   # duplicate columns & possible reduce dimensionality 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item) 
    1643   res = cache.get(item) 
    1644   if res is None: 
-> 1645    values = self._data.get(item) 
    1646    res = self._box_item_values(item, values) 
    1647    cache[item] = res 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath) 
    3588 
    3589    if not isnull(item): 
-> 3590     loc = self.items.get_loc(item) 
    3591    else: 
    3592     indexer = np.arange(len(self.items))[isnull(self.items)] 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 
    2442     return self._engine.get_loc(key) 
    2443    except KeyError: 
-> 2444     return self._engine.get_loc(self._maybe_cast_indexer(key)) 
    2445 
    2446   indexer = self.get_indexer([key], method=method, tolerance=tolerance) 

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() 

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() 

KeyError: '17' 

私は任意の助けを本当に感謝します!感謝万円。

+0

あなたは ''(M ['17 ']、M ['21'])での誤差を示しています。これはサンプルデータにありますか? –

答えて

1

次の行でエラーメッセージの2箇所がありました:

KeyError: '17' 

これはMにはキー'17'がないことを示します。これは、インデックスが整数である可能性が高いためです。ただし、現在DataFrame Mに文字列でアクセスしています。次のようにpearsonを呼び出すためのコードは次のようになります。

pearson(M[17], M[21]) 
関連する問題