加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
nonlinear_classification.py 1.75 KB
一键复制 编辑 原始数据 按行查看 历史
nnstrings 提交于 2023-03-08 17:08 . day1
import numpy as np
import matplotlib.pyplot as plt
class LogisticRegress:
def __init__(self):
self.theta = theta
def f(self, x):
return 1 / (1 + np.exp(-np.dot(x, self.theta)))
def learning(self, x, y):
epoch = 5000
ETA = 1e-3
count = 0
accuraries = []
for _ in range(epoch):
self.theta = self.theta - ETA * np.dot(self.f(x) - y, x)
result = self.classify(x) == y
accurary = len(result[result == True]) / len(result)
accuraries.append(accurary)
count += 1
# print("Times:{} theta:{}".format(count, self.theta))
plt.plot(accuraries)
plt.show()
def classify(self, x):
return (self.f(x) >= 0.5).astype(np.int)
def draw(self):
x = np.linspace(-2.5, 2.5, 100)
plt.plot(train_z[train_y == 1, 0], train_z[train_y == 1, 1], 'o')
plt.plot(train_z[train_y == 0, 0], train_z[train_y == 0, 1], 'x')
plt.plot(x, -(self.theta[0] + self.theta[1] * x + self.theta[3] * (x ** 2)) / self.theta[2])
plt.show()
def standardise(x):
return (x - mu) / sigma
def to_matrix(x):
x0 = np.ones([x.shape[0], 1])
return np.hstack([x0, x, x[:, 0, np.newaxis] ** 2])
if __name__ == "__main__":
train = np.loadtxt("data3.csv", delimiter=',', skiprows=1)
train_x = train[:, 0:2]
train_y = train[:, 2]
theta = np.random.rand(4)
mu = train_x.mean(axis=0)
sigma = train_x.std(axis=0)
train_z = standardise(train_x)
X = to_matrix(train_z)
plt.plot(train_z[train_y == 1, 0], train_z[train_y == 1, 1], 'o')
plt.plot(train_z[train_y == 0, 0], train_z[train_y == 0, 1], 'x')
plt.show()
LR = LogisticRegress()
LR.learning(X, train_y)
LR.draw()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化