generated from LufsX/Experiment-LogReg
This commit is contained in:
+23
-15
@@ -29,6 +29,15 @@ name = "Tanjunfeng"
|
||||
# 这里写自己的代码实现
|
||||
|
||||
def sigmoid(z):
|
||||
"""
|
||||
Compute the sigmoid of z.
|
||||
|
||||
Args:
|
||||
- z (numpy array): Input array.
|
||||
|
||||
Returns:
|
||||
- numpy array: Sigmoid of input.
|
||||
"""
|
||||
return 1 / (1 + np.exp(-z))
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||
@@ -42,7 +51,7 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||
- iterations (int): 迭代次数,默认为 1000。
|
||||
|
||||
Returns:
|
||||
- numpy array: 训练得到的权重。
|
||||
- Tuple[numpy array, float]: 训练得到的权重和偏置。
|
||||
"""
|
||||
m, n = X.shape
|
||||
weights = np.zeros(n)
|
||||
@@ -56,23 +65,23 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||
weights -= learning_rate * dw
|
||||
bias -= learning_rate * db
|
||||
|
||||
return weights
|
||||
return weights, bias
|
||||
|
||||
def predict_logreg(inX, weights):
|
||||
def predict_logreg(inX, weights, bias):
|
||||
"""
|
||||
对率回归预测函数。
|
||||
|
||||
Args:
|
||||
- inX (numpy array): 输入待分类的样本。
|
||||
- weights (numpy array): 训练得到的权重。
|
||||
- bias (float): 训练得到的偏置。
|
||||
|
||||
Returns:
|
||||
- int: 预测的分类标签。
|
||||
"""
|
||||
z = np.dot(inX, weights)
|
||||
z = np.dot(inX, weights) + bias
|
||||
h = sigmoid(z)
|
||||
# 调整分类阈值
|
||||
return 1 if h > 0.6 else 0
|
||||
return 1 if h >= 0.5 else 0
|
||||
|
||||
def load_data(filepath):
|
||||
"""
|
||||
@@ -82,23 +91,22 @@ def load_data(filepath):
|
||||
- directory (str): 数据文件路径。
|
||||
|
||||
Returns:
|
||||
- np.array: 特征数据。
|
||||
- np.array: 标签数据。
|
||||
- Tuple[np.array, np.array]: 特征数据和标签数据。
|
||||
"""
|
||||
data = []
|
||||
with open(filepath, 'r') as f:
|
||||
for line in f:
|
||||
row = []
|
||||
values = line.strip().split()
|
||||
if '?' in values:
|
||||
continue # Ignore lines with missing data
|
||||
for value in values:
|
||||
try:
|
||||
row.append(float(value))
|
||||
except ValueError:
|
||||
row.append(-1)
|
||||
row.append(float(value))
|
||||
data.append(row)
|
||||
np_data = np.array(data)
|
||||
X = np_data[:, :-1]
|
||||
y = np_data[:, -1]
|
||||
y = np.array([0 if label == 1 else (1 if label == 2 else -1) for label in y])
|
||||
return X, y
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -110,14 +118,14 @@ if __name__ == "__main__":
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=5000)
|
||||
weights, bias = train_logreg(X_train, y_train, learning_rate=0.001, iterations=5000)
|
||||
|
||||
# 测试单个数据
|
||||
correct_count = 0
|
||||
total_count = len(y_test)
|
||||
for i in range(total_count):
|
||||
y_pred = predict_logreg(X_test[i], weights)
|
||||
y_pred = predict_logreg(X_test[i], weights, bias)
|
||||
if y_pred == y_test[i]:
|
||||
correct_count += 1
|
||||
accuracy = correct_count / total_count
|
||||
print(f"准确率: {accuracy}")
|
||||
print(f"测试准确率: {accuracy:.2f}")
|
||||
|
||||
Reference in New Issue
Block a user