分享
三行代码  ›  专栏  ›  技术社区  ›  user1149518

熊猫数据帧相关问题。获取语法错误

  •  0
  • user1149518  · 技术社区  · 5 天前

    https://app.pluralsight.com/guides/machine-learning-concepts-python-and-scikit-learn

    我被挂在这里,有人能解释一下如何解决这个问题吗?

    import pandas as pd
    wine_data_frame = pd.DataFrame(data=wine_data['data'], columns=wine_data['feature_names'])
    wine_data_frame['class'] = wine_data['target']
    
    wine_classes = [wine_data_frame[wine_data_frame['class'] == x for x in range(3)]
    

    我在wine_classes行中得到错误“Invalid syntax”,并试图修复错误,但没有任何运气!有人能帮忙吗?

    3 回复  |  直到 5 天前
        1
  •  1
  •   fmarm    5 天前

    首先,在后面加一个括号 ==x .loc 在支架前

    wine_classes = [wine_data_frame.loc[wine_data_frame['class'] == x] for x in range(3)]
    

        2
  •  0
  •   Searching_answers    5 天前

    wine_data_frame=葡萄酒数据_frame.重命名(列={'target':'class'})

    wine_classes=葡萄酒数据框[葡萄酒数据框['class'].isin(范围(3))]

        3
  •  0
  •   Scott Boston    5 天前

    “x”后缺少括号,该网站上的文档不正确。

    wine_classes = [wine_data_frame[wine_data_frame['class'] == x] for x in range(3)]
    

    完整代码:

    import pandas as pd
    from sklearn.datasets import load_wine
    
    wine_data = load_wine()
    wine_data_frame = pd.DataFrame(data=wine_data['data'], columns=wine_data['feature_names'])
    wine_data_frame['class'] = wine_data['target']
    
    wine_classes = [wine_data_frame[wine_data_frame['class'] == x] for x in range(3)]
    
    testing_data = []
    for wine_class in wine_classes:
        row = wine_class.sample()
        testing_data.append(row)
        wine_data_frame = wine_data_frame.drop(row.index)
    

    [   alcohol  malic_acid   ash  alcalinity_of_ash  magnesium  total_phenols  flavanoids  nonflavanoid_phenols  proanthocyanins  color_intensity   hue  od280/od315_of_diluted_wines  proline  class
    2    13.16        2.36  2.67               18.6      101.0            2.8        3.24                   0.3             2.81             5.68  1.03                          3.17   1185.0      0,     alcohol  malic_acid   ash  alcalinity_of_ash  magnesium  total_phenols  flavanoids  nonflavanoid_phenols  proanthocyanins  color_intensity   hue  od280/od315_of_diluted_wines  proline  class
    91     12.0        1.51  2.42               22.0       86.0           1.45        1.25                   0.5             1.63              3.6  1.05                          2.65    450.0      1,      alcohol  malic_acid   ash  alcalinity_of_ash  magnesium  total_phenols  flavanoids  nonflavanoid_phenols  proanthocyanins  color_intensity   hue  od280/od315_of_diluted_wines  proline  class
    150     13.5        3.12  2.62               24.0      123.0            1.4        1.57                  0.22             1.25              8.6  0.59                           1.3    500.0      2]
    

         alcohol  malic_acid   ash  alcalinity_of_ash  magnesium  total_phenols  flavanoids  nonflavanoid_phenols  proanthocyanins  color_intensity   hue  od280/od315_of_diluted_wines  proline  class
    0      14.23        1.71  2.43               15.6      127.0           2.80        3.06                  0.28             2.29             5.64  1.04                          3.92   1065.0      0
    1      13.20        1.78  2.14               11.2      100.0           2.65        2.76                  0.26             1.28             4.38  1.05                          3.40   1050.0      0
    3      14.37        1.95  2.50               16.8      113.0           3.85        3.49                  0.24             2.18             7.80  0.86                          3.45   1480.0      0
    4      13.24        2.59  2.87               21.0      118.0           2.80        2.69                  0.39             1.82             4.32  1.04                          2.93    735.0      0
    5      14.20        1.76  2.45               15.2      112.0           3.27        3.39                  0.34             1.97             6.75  1.05                          2.85   1450.0      0
    ..       ...         ...   ...                ...        ...            ...         ...                   ...              ...              ...   ...                           ...      ...    ...
    173    13.71        5.65  2.45               20.5       95.0           1.68        0.61                  0.52             1.06             7.70  0.64                          1.74    740.0      2
    174    13.40        3.91  2.48               23.0      102.0           1.80        0.75                  0.43             1.41             7.30  0.70                          1.56    750.0      2
    175    13.27        4.28  2.26               20.0      120.0           1.59        0.69                  0.43             1.35            10.20  0.59                          1.56    835.0      2
    176    13.17        2.59  2.37               20.0      120.0           1.65        0.68                  0.53             1.46             9.30  0.60                          1.62    840.0      2
    177    14.13        4.10  2.74               24.5       96.0           2.05        0.76                  0.56             1.35             9.20  0.61                          1.60    560.0      2
    
    [175 rows x 14 columns]