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

This commit is contained in:
hao
2024-10-31 17:05:26 +08:00
parent 7e90dfab2b
commit 763559089a
+12 -4
View File
@@ -60,7 +60,6 @@ def predict_logreg(inX, weights):
y_predicted = 1 / (1 + np.exp(-linear_model))
return 1 if y_predicted > 0.5 else 0
# 这里也写自己的代码实现
def load_data(filepath):
"""
从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
@@ -72,9 +71,18 @@ def load_data(filepath):
- np.array: 特征数据。
- np.array: 标签数据。
"""
data = np.genfromtxt(filepath, delimiter=',', dtype=float, filling_values=0)
X = data[:, :-1]
y = data[:, -1].astype(int) # 将标签列转换为整数类型
# 使用pandas读取数据,假设逗号为分隔符
data = pd.read_csv(filepath, header=None)
# 将所有非数值列转换为数值型,非数值的转换为NaN,然后填充NaN为0
for col in data.columns:
if data[col].dtype == 'object':
data[col] = pd.to_numeric(data[col], errors='coerce').fillna(0)
# 将数据转换为numpy数组
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
return X, y
if __name__ == "__main__":