generated from LufsX/Experiment-LogReg
This commit is contained in:
+72
-40
@@ -27,54 +27,86 @@ name = "邓龙树"
|
||||
# user_code.py
|
||||
|
||||
import numpy as np
|
||||
from sklearn.preprocessing import MinMaxScaler
|
||||
import matplotlib.pyplot as plt
|
||||
import random
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||
num_samples, num_features = X.shape
|
||||
weights = np.zeros(num_features)
|
||||
for _ in range(iterations):
|
||||
z = np.dot(X, weights)
|
||||
h = 1 / (1 + np.exp(-z))
|
||||
gradient = np.dot(X.T, (h - y)) / num_samples
|
||||
weights -= learning_rate * gradient
|
||||
def sigmoid(intX):
|
||||
return 1.0 / (1 + np.exp(-intX))
|
||||
|
||||
|
||||
def stocGradAscent1(X, y, numIter=150):
|
||||
m, n = X.shape
|
||||
weights = np.ones(n)
|
||||
for j in range(numIter):
|
||||
dataIndex = list(range(m))
|
||||
for i in range(m):
|
||||
alpha = 4 / (1.0 + j + i) + 0.01
|
||||
randIndex = int(random.uniform(0, len(dataIndex)))
|
||||
h = sigmoid(np.dot(X[randIndex], weights))
|
||||
error = y[randIndex] - h
|
||||
weights = weights + np.dot(alpha * error, X[randIndex])
|
||||
del(dataIndex[randIndex])
|
||||
return weights
|
||||
|
||||
def predict_logreg(inX, weights):
|
||||
z = np.dot(inX, weights)
|
||||
return 1 if 1 / (1 + np.exp(-z)) > 0.5 else 0
|
||||
|
||||
def predict(X, weights):
|
||||
predictions = []
|
||||
for x in X:
|
||||
prob = sigmoid(np.dot(x, weights))
|
||||
predictions.append(1 if prob > 0.5 else 0)
|
||||
return predictions
|
||||
|
||||
|
||||
def load_data(filepath):
|
||||
data = np.genfromtxt(filepath, delimiter=',', dtype=np.float64)
|
||||
if len(data.shape) == 1:
|
||||
data = data.reshape(1, -1)
|
||||
# 处理缺失值
|
||||
data[np.isnan(data)] = 0
|
||||
X = data[:, :-1]
|
||||
y = data[:, -1]
|
||||
# 处理特征 23,合并取值 2 和 3 为 0
|
||||
y[y == 2] = 0
|
||||
y[y == 3] = 0
|
||||
# 去掉特征 3、特征 24~28
|
||||
X = np.delete(X, [2] + list(range(22, X.shape[1])), axis=1)
|
||||
return X, y
|
||||
data = []
|
||||
labels = []
|
||||
with open(filepath, 'r') as f:
|
||||
lines = f.readlines()
|
||||
for line in lines:
|
||||
line = line.strip().split()
|
||||
if len(line) >= 1 and line[-1].isdigit():
|
||||
line_data = [1] + [0 if x == '?' else float(x) for x in line[:-1]]
|
||||
data.append(line_data)
|
||||
labels.append(int(line[-1]))
|
||||
data = np.array(data)
|
||||
labels = np.array(labels)
|
||||
target_variable = labels
|
||||
target_variable[(target_variable == 2) | (target_variable == 3)] = 0
|
||||
data = np.delete(data, [2] + list(range(23, 28)), axis=1)
|
||||
scaler = StandardScaler()
|
||||
data = scaler.fit_transform(data)
|
||||
return data, target_variable
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 加载训练集
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
|
||||
# 对训练集特征进行归一化
|
||||
scaler = MinMaxScaler()
|
||||
X_train = scaler.fit_transform(X_train)
|
||||
def accuracy(X, y, weights):
|
||||
predictions = predict(X, weights)
|
||||
return np.mean(predictions == y)
|
||||
|
||||
# 加载测试集
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
|
||||
# 对测试集特征进行归一化
|
||||
X_test = scaler.transform(X_test)
|
||||
def colicTest():
|
||||
X_train, y_train = load_data("./horse-colic.data")
|
||||
X_test, y_test = load_data("./horse-colic.test")
|
||||
trainWeights = stocGradAscent1(X_train, y_train, 500)
|
||||
errorCount = 0
|
||||
numTestVec = 0.0
|
||||
for x, true_label in zip(X_test, y_test):
|
||||
if int(predict([x], trainWeights)[0])!= int(true_label):
|
||||
errorCount += 1
|
||||
numTestVec += 1.0
|
||||
errorRate = (float(errorCount) / numTestVec) * 100
|
||||
print("测试集错误率为: %.2f%%" % errorRate)
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
||||
def colicSklearn():
|
||||
X_train, y_train = load_data("./horse-colic.data")
|
||||
X_test, y_test = load_data("./horse-colic.test")
|
||||
classifier = LogisticRegression(solver='liblinear', max_iter=20).fit(X_train, y_train)
|
||||
test_accurcy = classifier.score(X_test, y_test) * 100
|
||||
print('正确率:%f%%' % test_accurcy)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# colicTest()
|
||||
colicSklearn()
|
||||
|
||||
Reference in New Issue
Block a user