gpt4 book ai didi

python - 类型错误 : argument must be a string or number

转载 作者:行者123 更新时间:2023-12-04 15:35:49 24 4
gpt4 key购买 nike

我正在使用下面的代码:

cat_cols = ['MSZoning','Alley','LotShape','LandContour','Utilities','LotConfig','LandSlope','Neighborhood','Condition1','Condition2','BldgType','HouseStyle','RoofStyle','RoofMatl','Exterior1st','Exterior2nd','MasVnrType','ExterQual','ExterCond','Foundation','BsmtQual','BsmtCond','BsmtExposure','BsmtFinType1','BsmtFinType2','Heating','HeatingQC','CentralAir','Electrical','KitchenQual','Functional','FireplaceQu','GarageType','GarageFinish','GarageQual','GarageCond','PavedDrive','PoolQC','Fence','MiscFeature','SaleType','SaleCondition']

from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()

for col in cat_cols:
if col in dataset_train.columns:
i = dataset_train.columns.get_loc(col)
dataset_train.iloc[:,i] =le.fit_transform(dataset_train.iloc[:,i])

它给出了如下所示的错误:

TypeError: argument must be a string or number

最佳答案

这是问题的解决方案
这是我写的代码。 (ps:幸运的是我有房价预测数据集:D”)

from sklearn.preprocessing import LabelEncoder

path="....\house pricing"
filepath=os.path.join(path,"train.csv")

dataset_train=pd.read_csv(filepath)
dataset_train

cat_features=[x for x in dataset_train.columns if dataset_train[x].dtype=="object"]

le=LabelEncoder()

for col in cat_features:
if col in dataset_train.columns:
i = dataset_train.columns.get_loc(col)
dataset_train.iloc[:,i] = dataset_train.apply(lambda i:le.fit_transform(i.astype(str)), axis=0, result_type='expand')
因此你只需要修改这个:
dataset_train.iloc[:,i] =le.fit_transform(dataset_train.iloc[:,i])
dataset_train.iloc[:,i] = dataset_train.apply(lambda i:le.fit_transform(dataset_train[i].astype(str)), axis=0, result_type='expand')
上面的 lamda 函数会将每一列及其数据点(按行 axis=0 )转换为“str”,然后通过“fit_transform”通过“le”或 LableEncoder 函数对其进行 LabelEncode。

关于python - 类型错误 : argument must be a string or number,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59798644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com