更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 11s

This commit is contained in:
hao
2024-11-02 12:59:38 +08:00
parent 92ada08ba9
commit c6d6bfc0ff
+15 -1
View File
@@ -79,8 +79,22 @@ def load_data(filepath):
- np.array: 标签数据。
"""
data = np.loadtxt(filepath, delimiter=',')
# 将分类数据和缺失值处理后转换为浮点数
X = data[:, :-1]
y = data[:, -1]
y = data[:, -1].astype(int) # 假设标签是整数类型
if X.shape[1] > 1:
# 将第一列分类数据转换为独热编码
from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder(sparse=False)
X[:, 0] = X[:, 0].astype(str) # 确保是字符串类型
X_encoded = encoder.fit_transform(X[:, 0].reshape(-1, 1))
# 将独热编码结果替换原来的分类列
X = np.hstack((X_encoded, X[:, 1:]))
# 将缺失值填充为0
X[np.isnan(X)] = 0
return X, y
if __name__ == "__main__":