Sklearn和导入CSV的不可用的类型错误



我正在尝试执行以下代码,但我不明白我在做什么错。代码的目的是使用Python's&Sklearn的Train_testrongplit功能将数据分配为培训和测试块。

数据(可在此处下载)是各种房屋/公寓的租金数据以及每个房屋/公寓的属性。最终,我正在尝试使用预测性建模来预测租金价格(因此租金价格是目标)。这是代码:

import pandas as pd
rentdata = pd.read_csv('6000_clean.csv')
import sklearn as sk
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cross_validation import train_test_split
#trying to make a all rows of the first column and b all rows of columns 2-46, i.e., a will be only target data (rent prices) and b will be the data.
a, b = rentdata[ : ,0], rentdata[ : ,1:46]

什么结果是以下错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-24-789fb8e8c2f6> in <module>()
      8 from sklearn.cross_validation import train_test_split
      9 
---> 10 a, b = rentdata[ : ,0], rentdata[ : ,1:46]
     11 
C:UsersNickAnacondalibsite-packagespandascoreframe.pyc in __getitem__(self, key)
   2001             # get column
   2002             if self.columns.is_unique:
-> 2003                 return self._get_item_cache(key)
   2004 
   2005             # duplicate columns
C:UsersNickAnacondalibsite-packagespandascoregeneric.pyc in _get_item_cache(self, item)
    665             return cache[item]
    666         except Exception:
--> 667             values = self._data.get(item)
    668             res = self._box_item_values(item, values)
    669             cache[item] = res
C:UsersNickAnacondalibsite-packagespandascoreinternals.pyc in get(self, item)
   1653     def get(self, item):
   1654         if self.items.is_unique:
-> 1655             _, block = self._find_block(item)
   1656             return block.get(item)
   1657         else:
C:UsersNickAnacondalibsite-packagespandascoreinternals.pyc in _find_block(self, item)
   1933 
   1934     def _find_block(self, item):
-> 1935         self._check_have(item)
   1936         for i, block in enumerate(self.blocks):
   1937             if item in block:
C:UsersNickAnacondalibsite-packagespandascoreinternals.pyc in _check_have(self, item)
   1939 
   1940     def _check_have(self, item):
-> 1941         if item not in self.items:
   1942             raise KeyError('no item named %s' % com.pprint_thing(item))
   1943 
C:UsersNickAnacondalibsite-packagespandascoreindex.pyc in __contains__(self, key)
    317 
    318     def __contains__(self, key):
--> 319         hash(key)
    320         # work around some kind of odd cython bug
    321         try:
TypeError: unhashable type

您可以下载CSV以在此处查看数据:http://wikisend.com/download/776790/6000_clean.csv

我下载了您的数据并修改了您的问题行:

a, b = rentdata.iloc[0], rentdata.iloc[1:46]

iLoc逐个位置选择行,请参阅文档:http://pandas.pydata.org/pandas-docs/stable/indexing.html#selection-by-position

现在选择第一行和行2-46(请记住,切片是开放的,包括范围的开始,但不包括范围的末端)

注意,您始终可以使用head选择第一行:

a, b = rentdata.head(0), rentdata.iloc[1:46]

也可以工作

In [5]:
a
Out[5]:
Monthly $ rent                                                    1150
Location                                                       alameda
# of bedrooms                                                        1
# of bathrooms                                                       1
# of square feet                                                   NaN
Latitude                                                      37.77054
Longitude                                                    -122.2509
Street address                                  1500-1598 Lincoln Lane
# more rows so trimmed for brevity here
.......
In [9]: b
Out[9]:
# too large to paste here
.....
45 rows × 46 columns

相关内容

  • 没有找到相关文章

最新更新