当前位置:网站首页>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)
边栏推荐
- Multiprocess programming (II): Pipeline
- 免费自媒体必备工具分享
- Lex & yacc & bison & flex configuration problems
- Rust字符串切片、结构体和枚举类
- Shell 实现文件基本操作(sed-编辑、awk-匹配)
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
- File operation io-part2
- 数组与集合性能比较
- [jetcache] jetcache configuration description and annotation attribute description
猜你喜欢

1.12 - Instructions

MySQL 23 classic interview hanging interviewer

Rust字符串切片、结构体和枚举类

Shell implements basic file operations (SED edit, awk match)

Multiprocess programming (I): basic concepts

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

【AutoSAR 十二 模式管理】

Logback configuration file

【AutoSAR 八 OS】

【案例分享】让新时代教育发展与“数”俱进
随机推荐
指针初阶(基础)
[daily training] 871 Minimum refueling times
Hdu3507 (slope DP entry)
Sentry developer contribution Guide - configure pycharm
字符设备注册常用的两种方法和步骤
Nc50528 sliding window
Wechat applet obtains the information of an element (height, width, etc.) and converts PX to rpx.
Linux Software: how to install redis service
helm 基础学习
One of the reasons why setinterval timer does not take effect in ie: the callback is the arrow function
Two common methods and steps of character device registration
[shutter] image component (load network pictures | load static pictures | load local pictures | path | provider plug-in)
Win10 多种方式解决无法安装.Net3.5的问题
Vulkan is not a "panacea"“
Nc20806 District interval
[MCU project training] eight way answering machine
Baidu AI Cloud takes the lead in building a comprehensive and standardized platform for smart cloud
MySQL 23 classic interview hanging interviewer
腾讯云免费SSL证书扩展文件含义
NC24325 [USACO 2012 Mar S]Flowerpot