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

This commit is contained in:
2024-11-05 19:14:23 +08:00
parent e4187092cc
commit efdd3712a9
+10 -10
View File
@@ -55,19 +55,19 @@ def predict_logreg(inX, weights):
# 从文件中加载数据
def load_data(filepath):
data = pd.read_csv(filepath, header=None, na_values='?', delim_whitespace=True)
for col in data.columns:
if pd.api.types.is_numeric_dtype(data[col]):
data[col].fillna(data[col].median(), inplace=True)
else: # 处理非数值列,例如用众数填充或其他策略
data[col].fillna(data[col].mode()[0], inplace=True)
X = data.iloc[:, :-1].to_numpy()
y = data.iloc[:, -1].to_numpy()
# 使用SimpleImputer进行中位数填充
imputer = SimpleImputer(strategy='median')
data_imputed = imputer.fit_transform(data)
data = pd.DataFrame(data_imputed)
X = data.iloc[:, :-1].to_numpy() # 特征
y = data.iloc[:, -1].to_numpy() # 标签
# 使用MinMaxScaler进行特征缩放
scaler = MinMaxScaler()
X = scaler.fit_transform(X) # 缩放特征
return X, y
X = scaler.fit_transform(X)
return X, y
# 准确率计算函数
def calculate_accuracy(X, y, weights):
predictions = np.array([predict_logreg(x, weights) for x in X])