generated from LufsX/Experiment-LogReg
This commit is contained in:
+26
-48
@@ -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):
|
||||
"""
|
||||
从文件中加载数据,若不自行正确实现,则无法通过评测,不记录成绩。
|
||||
@@ -34,31 +9,34 @@ def load_data(filepath):
|
||||
- np.array: 特征数据。
|
||||
- np.array: 标签数据。
|
||||
"""
|
||||
try:
|
||||
# Load data with genfromtxt, filling missing values with NaN
|
||||
data = np.genfromtxt(filepath, delimiter=',', missing_values='?', filling_values=np.nan)
|
||||
|
||||
# Check if the data has been loaded correctly with at least two dimensions
|
||||
if data.ndim < 2:
|
||||
print(f"Error: File '{filepath}' may be empty or improperly formatted.")
|
||||
return None, None
|
||||
for delimiter in [',', ' ', '\t']: # 尝试不同的分隔符:逗号、空格、制表符
|
||||
try:
|
||||
# Load data, filling missing values with 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
|
||||
if data.ndim < 2 or data.size == 0:
|
||||
continue # 尝试下一个分隔符
|
||||
|
||||
# Remove rows with NaN values
|
||||
data = data[~np.isnan(data).any(axis=1)]
|
||||
|
||||
# Ensure there's still data left after removing rows with NaN
|
||||
if data.size == 0:
|
||||
print(f"Error: No valid data after removing NaN rows in file '{filepath}'.")
|
||||
return None, None
|
||||
# Remove rows with NaN values
|
||||
data = data[~np.isnan(data).any(axis=1)]
|
||||
|
||||
# Ensure there's still data left after removing NaN
|
||||
if data.size == 0:
|
||||
print(f"Error: No valid data after removing NaN rows in file '{filepath}'.")
|
||||
return None, None
|
||||
|
||||
# Split into features (X) and labels (y)
|
||||
X = data[:, :-1] # Features
|
||||
y = data[:, -1] # Labels
|
||||
|
||||
return X, y
|
||||
except Exception as e:
|
||||
print(f"Error loading data with delimiter '{delimiter}': {e}")
|
||||
|
||||
print(f"Error: File '{filepath}' may be empty or improperly formatted.")
|
||||
return None, None
|
||||
|
||||
# Split into features (X) and labels (y)
|
||||
X = data[:, :-1] # Features
|
||||
y = data[:, -1] # Labels
|
||||
|
||||
return X, y
|
||||
except Exception as e:
|
||||
print(f"Error loading data: {e}")
|
||||
return None, None
|
||||
|
||||
|
||||
def sigmoid(z):
|
||||
|
||||
Reference in New Issue
Block a user