Issue
Could you please someone advice me why my function 'X = xdf[selected_features] ' does not work. I use Windows 11 and python 3.12. I get this error when I run (Passing a set as an indexer is not supported. Use a list instead)
selected_features = {'NDRE_20', 'NDRE_16', 'CSM_20', 'CSM_16', 'EVI2_16',
'EVI2_20', 'GCVI_16', 'GCVI_20', 'CIred_16', 'CIred_20', 'SCCCI_16',
'SCCCI_20', 'NDVI_16', 'NDVI_20', 'Sol_ME_1606', 'Sol_ME_2005',
'SAVI_20', 'SAVI_16', 'NDWI_16', 'NDWI_B_16', 'NDWI_20', 'NDWI_B_20',
'soil'}
selected_features
y = xdf['yield_1'] # Targeted Values Selection
X = xdf[selected_features] # Independent Values
print(X)
print(y)
TypeError
Cell In[42], line 2
1 y = xdf['yield_1'] # Targeted Values Selection
----> 2 X = xdf[selected_features] # Independent Values
3 print(X)
4 print(y)
File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\indexing.py:2687, in check_dict_or_set_indexers(key)
2679 """
2680 Check if the indexer is or contains a dict or set, which is no longer allowed.
2681 """
2682 if (
2683 isinstance(key, set)
2684 or isinstance(key, tuple)
2685 and any(isinstance(x, set) for x in key)
2686 ):
-> 2687 raise TypeError(
2688 "Passing a set as an indexer is not supported. Use a list instead."
2689 )
Solution
Since you have used curly {} brackets, ‘selected_features’ is taken by Python to be a set. However, you actually require a list. To define a list, you need to use square brackets []. Modified code:
selected_features = ['NDRE_20', 'NDRE_16', 'CSM_20', 'CSM_16', 'EVI2_16',
'EVI2_20', 'GCVI_16', 'GCVI_20', 'CIred_16', 'CIred_20', 'SCCCI_16',
'SCCCI_20', 'NDVI_16', 'NDVI_20', 'Sol_ME_1606', 'Sol_ME_2005',
'SAVI_20', 'SAVI_16', 'NDWI_16', 'NDWI_B_16', 'NDWI_20', 'NDWI_B_20',
'soil']
y = xdf['yield_1'] # Targeted Values Selection
X = xdf[selected_features] # Independent Values
print(X)
print(y)
Answered By - Dinux
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.