当前位置:网站首页>机器学习:numpy版本线性回归预测波士顿房价
机器学习:numpy版本线性回归预测波士顿房价
2022-07-02 23:36:00 【HanZee】
数据链接
链接: https://pan.baidu.com/s/1uDG_2IZVZCn9kndZ_ZIGaA?pwd=nec2 提取码: nec2
导入数据
import numpy as np
path = 'Desktop/波士顿房价/trian.csv'
data = np.loadtxt(path, delimiter = ",", skiprows=1)
data.shape
划分数据
train = data[:int(data.shape[0]*0.8)]
test = data[int(data.shape[0]*0.8):]
print(train.shape, test.shape)
train_x = train[:,:-1]
train_y = train[:,13:]
test_x = test[:,:-1]
test_y = test[:,13:]
print(train_x.shape, train_y.shape)
模型
class Network:
def __init__(self, num_weights):
self.num_weights = num_weights
self.w = np.random.rand(num_weights, 1)
self.b = 0
#前向计算
def forward(self, x):
z = np.dot(x, self.w) + self.b
return z
#计算loss
def loss(self, z, y):
cost = (z-y)*(z-y)
cost = np.mean(cost)
return cost
#计算梯度
def gradient(self, z, y):
w = (z-y)*train_x
w = np.mean(w, axis = 0)
w = np.array(w).reshape([13,1])
b = z-y
b = np.mean(b)
return w, b
#更新参数
def update(self, gradient_w, gradient_b, eta):
self.w = self.w - eta*gradient_w
self.b = self.b - eta*gradient_b
#训练
def train(self, items, eta):
for i in range(items):
z = self.forward(train_x)
loss = self.loss(z, train_y)
gradient_w, gradient_b = self.gradient(z, train_y)
self.update(gradient_w, gradient_b, eta)
if i%100 ==0:
test_loss = self.test()
print('item:',i,'loss:', loss, 'test_loss:', test_loss)
#测试
def test(self):
z = self.forward(test_x)
loss = self.loss(z,test_y)
return loss
net = Network(13)
net.train(1000000, eta= 6e-6)
边栏推荐
- 多进程编程(一):基本概念
- One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
- How do educators find foreign language references?
- Introduction of UART, RS232, RS485, I2C and SPI
- NC24840 [USACO 2009 Mar S]Look Up
- Cmake basic use
- TypeError: Cannot read properties of undefined (reading ***)
- Program analysis and Optimization - 9 appendix XLA buffer assignment
- Nc17059 queue Q
- Free we media essential tools sharing
猜你喜欢
随机推荐
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
布隆过滤器
[shutter] Introduction to the official example of shutter Gallery (learning example | email application | retail application | wealth management application | travel application | news application | a
Automated defect analysis in electronic microscopic images
Linux Software: how to install redis service
【Pulsar文档】概念和架构/Concepts and Architecture
AttributeError: ‘tuple‘ object has no attribute ‘layer‘问题解决
Understanding and application of least square method
多进程编程(五):信号量
Don't want teachers to see themselves with cameras in online classes? Virtual camera you deserve!
Rust所有权(非常重要)
About the practice topic of screen related to unity screen, unity moves around a certain point inside
An excellent orm in dotnet circle -- FreeSQL
Mutual exclusion and synchronization of threads
Briefly talk about other uses of operation and maintenance monitoring
[target detection] r-cnn, fast r-cnn, fast r-cnn learning
使用jenkins之二Job
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
简单聊聊运维监控的其他用途
Multiprocess programming (I): basic concepts









