当前位置:网站首页>Machine learning: numpy version linear regression predicts Boston house prices
Machine learning: numpy version linear regression predicts Boston house prices
2022-07-03 00:44:00 【HanZee】
machine learning :numpy Boston house price predicted by version linear regression
Data links
link : https://pan.baidu.com/s/1uDG_2IZVZCn9kndZ_ZIGaA?pwd=nec2 Extraction code : nec2
Import data
import numpy as np
path = 'Desktop/ Boston prices /trian.csv'
data = np.loadtxt(path, delimiter = ",", skiprows=1)
data.shape
Divide the data
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)
Model
class Network:
def __init__(self, num_weights):
self.num_weights = num_weights
self.w = np.random.rand(num_weights, 1)
self.b = 0
# Forward calculation
def forward(self, x):
z = np.dot(x, self.w) + self.b
return z
# Calculation loss
def loss(self, z, y):
cost = (z-y)*(z-y)
cost = np.mean(cost)
return cost
# Calculate the gradient
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
# Update parameters
def update(self, gradient_w, gradient_b, eta):
self.w = self.w - eta*gradient_w
self.b = self.b - eta*gradient_b
# Training
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)
# test
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)
边栏推荐
- MySQL 23 classic interview hanging interviewer
- 2022 list of manufacturers of Chinese 3D vision enterprises (guided positioning and sorting scenes)
- NC20806 区区区间间间
- 微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
- Vulkan performance and refinement
- 1.11 - bus
- 机器学习:numpy版本线性回归预测波士顿房价
- Win10 多种方式解决无法安装.Net3.5的问题
- University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量
- NC24325 [USACO 2012 Mar S]Flowerpot
猜你喜欢
The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
Redis21 classic interview questions, extreme pull interviewer
1.11 - 总线
【AutoSAR 七 工具链简介】
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
[MCU project training] eight way answering machine
Rust ownership (very important)
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
【AutoSAR 十一 通信相关机制】
Shell 实现文件基本操作(切割、排序、去重)
随机推荐
百数不断创新,打造自由的低代码办公工具
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
Vulkan practice first bullet
【Pulsar文档】概念和架构/Concepts and Architecture
多进程编程(二):管道
Free we media essential tools sharing
Rust字符串切片、结构体和枚举类
NC24840 [USACO 2009 Mar S]Look Up
【AutoSAR 十三 NVM】
NC50965 Largest Rectangle in a Histogram
【案例分享】让新时代教育发展与“数”俱进
【AutoSAR 三 RTE概述】
Shell 实现文件基本操作(切割、排序、去重)
为什么网站打开速度慢?
免费自媒体必备工具分享
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
JSON conversion tool class
pod生命周期详解
lex && yacc && bison && flex 配置的問題
kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)