generated from LufsX/Experiment-LogReg
This commit is contained in:
+28
-37
@@ -1,7 +1,7 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.impute import SimpleImputer
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
import csv
|
||||
|
||||
"""
|
||||
实验说明:
|
||||
@@ -27,7 +27,7 @@ id = "202210423219"
|
||||
name = "李燕如"
|
||||
|
||||
|
||||
# 这里写自己的代码实现
|
||||
|
||||
def sigmoid(x):
|
||||
return 1 / (1 + np.exp(-x))
|
||||
|
||||
@@ -46,38 +46,6 @@ def predict_logreg(inX, weights):
|
||||
probability = sigmoid(linear_output)
|
||||
return 1 if probability > 0.5 else 0
|
||||
|
||||
def load_data(filepath):
|
||||
# 使用csv模块读取数据
|
||||
data = []
|
||||
with open(filepath, 'r') as file:
|
||||
reader = csv.reader(file, delimiter=' ')
|
||||
for row in reader:
|
||||
if row: # 确保行不为空
|
||||
# 将非数值和空字符串转换为np.nan,并确保所有行具有相同的列数
|
||||
row = [float(x) if x.replace('.', '', 1).isdigit() or x == '' else np.nan for x in row]
|
||||
# 确保所有行具有相同的列数,可以通过填充缺失的列或截断多余的列来实现
|
||||
while len(row) < 30: # 假设数据集应该有30列
|
||||
row.append(np.nan)
|
||||
while len(row) > 30:
|
||||
row.pop()
|
||||
data.append(row)
|
||||
|
||||
# 将列表转换为numpy数组
|
||||
data = np.array(data, dtype=float)
|
||||
|
||||
# 使用SimpleImputer填充缺失值
|
||||
imputer = SimpleImputer(missing_values=np.nan, strategy='constant', fill_value=0)
|
||||
data = imputer.fit_transform(data)
|
||||
|
||||
# 分割特征和标签
|
||||
X = data[:, :-1]
|
||||
y = data[:, -1].astype(int)
|
||||
|
||||
# 预处理数据
|
||||
X, y = preprocess_data(X, y)
|
||||
|
||||
return X, y
|
||||
|
||||
def preprocess_data(X, y):
|
||||
# 合并目标变量的类别
|
||||
y[y == 2] = 0
|
||||
@@ -89,11 +57,34 @@ def preprocess_data(X, y):
|
||||
|
||||
return X, y
|
||||
|
||||
# 确保其他函数(sigmoid, train_logreg, predict_logreg)保持不变
|
||||
def load_data(filepath):
|
||||
# 使用Pandas读取数据,可以更好地处理缺失值和字符串
|
||||
try:
|
||||
data = pd.read_csv(filepath, header=None, sep=r'\s+', on_bad_lines='skip')
|
||||
except pd.errors.ParserError as e:
|
||||
print(f"Error parsing file: {e}")
|
||||
return None, None
|
||||
|
||||
# 将'?'替换为np.nan
|
||||
data.replace('?', np.nan, inplace=True)
|
||||
|
||||
# 使用SimpleImputer填充缺失值
|
||||
imputer = SimpleImputer(missing_values=np.nan, strategy='constant', fill_value=0)
|
||||
data.iloc[:, :-1] = imputer.fit_transform(data.iloc[:, :-1])
|
||||
|
||||
# 将所有列转换为数值类型
|
||||
for col in data.columns[:-1]:
|
||||
data[col] = pd.to_numeric(data[col], errors='coerce')
|
||||
|
||||
X = data.iloc[:, :-1].values
|
||||
y = data.iloc[:, -1].values.astype(int)
|
||||
|
||||
# 预处理数据
|
||||
X, y = preprocess_data(X, y)
|
||||
|
||||
return X, y
|
||||
|
||||
# 确保其他函数(sigmoid, train_logreg, predict_logreg)保持不变
|
||||
def main():
|
||||
# 更新文件路径以匹配实际位置
|
||||
X_train, y_train = load_data("C:/Users/HP/Desktop/机器学习/Experiment-LogReg/data/horse-colic.data")
|
||||
X_test, y_test = load_data("C:/Users/HP/Desktop/机器学习/Experiment-LogReg/data/horse-colic.test")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user