generated from LufsX/Experiment-LogReg
This commit is contained in:
+72
-40
@@ -27,54 +27,86 @@ name = "邓龙树"
|
|||||||
# user_code.py
|
# user_code.py
|
||||||
|
|
||||||
import numpy as np
|
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):
|
def sigmoid(intX):
|
||||||
num_samples, num_features = X.shape
|
return 1.0 / (1 + np.exp(-intX))
|
||||||
weights = np.zeros(num_features)
|
|
||||||
for _ in range(iterations):
|
|
||||||
z = np.dot(X, weights)
|
def stocGradAscent1(X, y, numIter=150):
|
||||||
h = 1 / (1 + np.exp(-z))
|
m, n = X.shape
|
||||||
gradient = np.dot(X.T, (h - y)) / num_samples
|
weights = np.ones(n)
|
||||||
weights -= learning_rate * gradient
|
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
|
return weights
|
||||||
|
|
||||||
def predict_logreg(inX, weights):
|
|
||||||
z = np.dot(inX, weights)
|
def predict(X, weights):
|
||||||
return 1 if 1 / (1 + np.exp(-z)) > 0.5 else 0
|
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):
|
def load_data(filepath):
|
||||||
data = np.genfromtxt(filepath, delimiter=',', dtype=np.float64)
|
data = []
|
||||||
if len(data.shape) == 1:
|
labels = []
|
||||||
data = data.reshape(1, -1)
|
with open(filepath, 'r') as f:
|
||||||
# 处理缺失值
|
lines = f.readlines()
|
||||||
data[np.isnan(data)] = 0
|
for line in lines:
|
||||||
X = data[:, :-1]
|
line = line.strip().split()
|
||||||
y = data[:, -1]
|
if len(line) >= 1 and line[-1].isdigit():
|
||||||
# 处理特征 23,合并取值 2 和 3 为 0
|
line_data = [1] + [0 if x == '?' else float(x) for x in line[:-1]]
|
||||||
y[y == 2] = 0
|
data.append(line_data)
|
||||||
y[y == 3] = 0
|
labels.append(int(line[-1]))
|
||||||
# 去掉特征 3、特征 24~28
|
data = np.array(data)
|
||||||
X = np.delete(X, [2] + list(range(22, X.shape[1])), axis=1)
|
labels = np.array(labels)
|
||||||
return X, y
|
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")
|
|
||||||
|
|
||||||
# 对训练集特征进行归一化
|
def accuracy(X, y, weights):
|
||||||
scaler = MinMaxScaler()
|
predictions = predict(X, weights)
|
||||||
X_train = scaler.fit_transform(X_train)
|
return np.mean(predictions == y)
|
||||||
|
|
||||||
# 加载测试集
|
|
||||||
X_test, y_test = load_data("./data/horse-colic.test")
|
|
||||||
|
|
||||||
# 对测试集特征进行归一化
|
def colicTest():
|
||||||
X_test = scaler.transform(X_test)
|
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)
|
|
||||||
|
|
||||||
# 测试单个数据
|
def colicSklearn():
|
||||||
y_pred = predict_logreg(X_test[11], weights)
|
X_train, y_train = load_data("./horse-colic.data")
|
||||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
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