当前位置:网站首页>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)
边栏推荐
- Hundreds of continuous innovation to create free low code office tools
- antv x6节点拖拽到画布上后的回调事件(踩大坑记录)
- Tensorflow 2. Chapter 15 of X (keras) source code explanation: migration learning and fine tuning
- 多进程编程(五):信号量
- node_modules删不掉
- [shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
- Introduction of UART, RS232, RS485, I2C and SPI
- 百度智能云牵头打造智能云综合标准化平台
- 【AutoSAR 十 IO架构】
- About the practice topic of screen related to unity screen, unity moves around a certain point inside
猜你喜欢
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
Teach you JDBC hand in hand -- structure separation
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
1.11 - 总线
【AutoSAR 十三 NVM】
字符设备注册常用的两种方法和步骤
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
pod生命周期详解
Hdu3507 (slope DP entry)
University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
随机推荐
【AutoSAR 十 IO架构】
【日常训练】871. 最低加油次数
Andorid gets the system title bar height
Multiprocess programming (4): shared memory
Set up nacos2 X cluster steps and problems encountered
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
Preview word documents online
lex && yacc && bison && flex 配置的問題
Rust string slicing, structs, and enumeration classes
NC50528 滑动窗口
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
Program analysis and Optimization - 9 appendix XLA buffer assignment
Rust ownership (very important)
Shell implements basic file operations (cutting, sorting, and de duplication)
Is there a free text to speech tool to help recommend?
【AutoSAR 一 概述】
DotNet圈里一个优秀的ORM——FreeSql
关于XML一些介绍和注意事项
Arduino开发之按键检测与正弦信号输出
【AutoSAR 八 OS】