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

This commit is contained in:
2024-11-02 15:50:31 +08:00
parent a7a97ef162
commit 1bf2f4ebd8
+10 -32
View File
@@ -1,28 +1,3 @@
import numpy as np
"""
实验说明:
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
注意事项:
- 在 function.py 中实现对率回归算法。
- 可以自行导入其他库,需要在 requirements.txt 添加依赖,若未在 requirements.txt 中添加使用到的依赖,则无法自动评测,成绩为零。
- 自动评测脚本仅会加载 train_logreg、predict_logreg 和 load_data 函数,请勿修改函数名。
测评环境:
- CPU: Intel Xeon(R) CPU E3-1231 v3 @ 3.40GHz
- OS: Ubuntu 22.04.4 LTS
- Python: 3.10.12
- numpy: 2.1.1
"""
"""
请在这里填写学号和姓名,不填写即成绩无效。
"""
id = "202210423118"
name = "刘国庆"
def load_data(filepath): def load_data(filepath):
""" """
从文件中加载数据,若不自行正确实现,则无法通过评测,不记录成绩。 从文件中加载数据,若不自行正确实现,则无法通过评测,不记录成绩。
@@ -34,19 +9,19 @@ def load_data(filepath):
- np.array: 特征数据。 - np.array: 特征数据。
- np.array: 标签数据。 - np.array: 标签数据。
""" """
for delimiter in [',', ' ', '\t']: # 尝试不同的分隔符:逗号、空格、制表符
try: try:
# Load data with genfromtxt, filling missing values with NaN # Load data, filling missing values with NaN
data = np.genfromtxt(filepath, delimiter=',', missing_values='?', filling_values=np.nan) data = np.genfromtxt(filepath, delimiter=delimiter, missing_values='?', filling_values=np.nan)
# Check if the data has been loaded correctly with at least two dimensions # Check if the data has been loaded correctly with at least two dimensions
if data.ndim < 2: if data.ndim < 2 or data.size == 0:
print(f"Error: File '{filepath}' may be empty or improperly formatted.") continue # 尝试下一个分隔符
return None, None
# Remove rows with NaN values # Remove rows with NaN values
data = data[~np.isnan(data).any(axis=1)] data = data[~np.isnan(data).any(axis=1)]
# Ensure there's still data left after removing rows with NaN # Ensure there's still data left after removing NaN
if data.size == 0: if data.size == 0:
print(f"Error: No valid data after removing NaN rows in file '{filepath}'.") print(f"Error: No valid data after removing NaN rows in file '{filepath}'.")
return None, None return None, None
@@ -57,10 +32,13 @@ def load_data(filepath):
return X, y return X, y
except Exception as e: except Exception as e:
print(f"Error loading data: {e}") print(f"Error loading data with delimiter '{delimiter}': {e}")
print(f"Error: File '{filepath}' may be empty or improperly formatted.")
return None, None return None, None
def sigmoid(z): def sigmoid(z):
""" """
Sigmoid activation function. Sigmoid activation function.