generated from LufsX/Experiment-LogReg
This commit is contained in:
+15
-1
@@ -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__":
|
||||
|
||||
Reference in New Issue
Block a user