当前位置: 首页 > news >正文

机器学习 - 准备数据

“Data” in machine learning can be almost anything you can imagine. A table of big Excel spreadsheet, images, videos, audio files, text and more.

机器学习其实可以分为两部分

  1. 将不管是什么data,都转成numbers.
  2. 挑选或者建立一个模型来学习这些numbers as best as possible.

下面是代码展示,创建一个straight line data

import torch 
from torch import nn  # nn: neural networks. This package contains the building blocks for creating neural networks 
import matplotlib.pyplot as plt # Create linear regression parameters
weight = 0.7
bias = 0.3 # Create data 
start = 0
end = 1
step = 0.02 
X = torch.arange(start, end, step).unsqueeze(dim=1)  # X is features
y = weight * X + bias   # y is labels
print(X[:10])
print(y[:10])# 结果如下
tensor([[0.0000],[0.0200],[0.0400],[0.0600],[0.0800],[0.1000],[0.1200],[0.1400],[0.1600],[0.1800]])
tensor([[0.3000],[0.3140],[0.3280],[0.3420],[0.3560],[0.3700],[0.3840],[0.3980],[0.4120],[0.4260]])

将上面获取到的数据进行拆分,每部分数据带有不同的意思。

SplitPurposeAmount of total dataHow often is it used?
Training setThe model learns from this data (like the course materials you study during the semester)~60-80%Always
Validation setThe model gets tuned on this data (like the practice exam you take before the final exam).~10-20%Often but not always
Testing setThe model gets evaluated on this data to test what it has leanred (like the final exam you take at the end of the semester).~10-20%Always

When dealing with real-world data, this step is typically done right at the start of a project (the test set should always be kept separate from all other data). Let the model learn on training data and then evaluate the model on test data to get an indication of how well it generalizes to unseen examples.

下面是代码。

# Create train/test split 
train_split = int(0.8 * len(X))
X_train, y_train = X[:train_split], y[:train_split]
X_test, y_test = X[train_split:], y[train_split:]# Learn the relationship between X_train and y_train
print(f"X_train length: {len(X_train)}")
print(f"y_train length: {len(y_train)}")
# Learn the relationship between X_test and y_test
print(f"X_test length: {len(X_test)}")
print(f"y_test length: {len(y_test)}")# 输出如下
X_train length: 40
y_train length: 40
X_test length: 10
y_test length: 10

通过将各个数字显示出来,更直观

plt.figure(figsize=(10, 7))# s 代表是散点的大小
plt.scatter(X_train, y_train, c="b", s=4, label="Training data")
plt.scatter(X_test, y_test, c="r", s=4, label="Testing data")plt.legend(prop={"size": 14})
plt.show()

结果如图
都看到这了,给个赞呗~

相关文章:

  • 总结: HQL语句
  • 软考 系统架构设计师系列知识点之系统性能(1)
  • Hive自定义GenericUDF函数
  • 【AI生成文章】flutter ChangeNotifierProvider 实用场景举例
  • RIP,EIGRP,OSPF的区别
  • 医学预测变量筛选的几种方法(R语言版)
  • 2024年阿里云2核4G服务器价格30元、165元和199元1年
  • AI智能分析网关V4在养老院视频智能监控场景中的应用
  • File的学习2
  • ChatGPT无法登录,提示我们检测到可疑的登录行为?如何解决?
  • 南京大学AI考研,宣布改考408!
  • Visual Studio - 添加快捷键图标
  • 无服务器推理在大语言模型中的未来
  • 一次完整的 HTTP 请求所经历的步骤
  • 使用GPT2预训练模型的方法
  • 网络传输文件的问题
  • Android开源项目规范总结
  • Angular4 模板式表单用法以及验证
  • Docker: 容器互访的三种方式
  • JS数组方法汇总
  • python docx文档转html页面
  • React 快速上手 - 07 前端路由 react-router
  • Redis 懒删除(lazy free)简史
  • SpiderData 2019年2月23日 DApp数据排行榜
  • Spring框架之我见(三)——IOC、AOP
  • Twitter赢在开放,三年创造奇迹
  • Vue2.0 实现互斥
  • windows下mongoDB的环境配置
  • 从零开始的无人驾驶 1
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 计算机常识 - 收藏集 - 掘金
  • 前端设计模式
  • 数据库写操作弃用“SELECT ... FOR UPDATE”解决方案
  • 小程序测试方案初探
  • ###项目技术发展史
  • #微信小程序(布局、渲染层基础知识)
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (16)Reactor的测试——响应式Spring的道法术器
  • (LeetCode 49)Anagrams
  • (M)unity2D敌人的创建、人物属性设置,遇敌掉血
  • (react踩过的坑)antd 如何同时获取一个select 的value和 label值
  • (附源码)springboot 基于HTML5的个人网页的网站设计与实现 毕业设计 031623
  • (四)模仿学习-完成后台管理页面查询
  • (幽默漫画)有个程序员老公,是怎样的体验?
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • (转载)利用webkit抓取动态网页和链接
  • (轉貼) 2008 Altera 亞洲創新大賽 台灣學生成果傲視全球 [照片花絮] (SOC) (News)
  • .net MySql
  • .net 中viewstate的原理和使用
  • .NET/C# 的字符串暂存池
  • /etc/shadow字段详解
  • @entity 不限字节长度的类型_一文读懂Redis常见对象类型的底层数据结构
  • @取消转义
  • [Asp.net mvc]国际化
  • [ERROR]-Error: failure: repodata/filelists.xml.gz from addons: [Errno 256] No more mirrors to try.