如何选择分类特征和数值特征来运行训练测试?

问题描述 投票:0回答:1

我试图连接两个不同长度的特征列表,这导致了 ValueError,因为两个列表的形状无法一起广播。我试图从数据框中选择分类和数字特征。

我写了这个:

categorical_features = df.select_dtypes(include=['object', 'category']).columns 
df = df.dropna(subset=categorical_features)
numerical_features = df.select_dtypes(include=['int64', 'float64']).columns
X = df[categorical_features + numerical_features].copy()
y = df['JobSatisfaction'].copy() 

我得到了这个:

ValueError: operands could not be broadcast together with shapes (87,) (42,) 
python pandas dataframe data-analysis data-cleaning
1个回答
0
投票

Pandas 不喜欢连接不同长度的对象。

尝试使用联合:

categorical_features = df.select_dtypes(include=['object', 'category']).columns
df = df.dropna(subset=categorical_features) 
numerical_features = df.select_dtypes(include=['int64', 'float64']).columns

all_features = categorical_features.union(numerical_features) 

X = df[all_features].copy()
y = df['JobSatisfaction'].copy()
© www.soinside.com 2019 - 2024. All rights reserved.