Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: argument must be a string or number

I'm using the code below:

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])

It gives an error as shown below:

TypeError: argument must be a string or number

like image 617
Mamta Gupta Avatar asked Oct 17 '25 04:10

Mamta Gupta


1 Answers

Here is the solution to the problem

this is the code I wrote. (ps: luckily i have the house price prediction dataset with me :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')

Thus just you have to modify this:

dataset_train.iloc[:,i] =le.fit_transform(dataset_train.iloc[:,i])

with

dataset_train.iloc[:,i] = dataset_train.apply(lambda i:le.fit_transform(dataset_train[i].astype(str)), axis=0, result_type='expand')

The above lamda function will convert each column and its data points(row wise axis=0) to "str" and then pass it through the "le" or LableEncoder function via the "fit_transform" to LabelEncode it.

like image 153
Rupanjan Nayak Avatar answered Oct 19 '25 18:10

Rupanjan Nayak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!