文章目录

  • 文件的读写
    • 创建.csv类型的文件,并读取文件
    • 创建.xlsx文件
  • 使用Python做图
  • 生成数据集
  • 切片取值操作
    • 修改张量中指定位置的数据
  • 知识点
    • torch.arange(x)
    • torch.tensor(2)
    • A=torch.randn(36).reshape(6,6)
    • shape
    • numel()
    • reshape(x,y,z)
    • torch.zeros(3,3,4)
    • torch.ones(2,3,4)
    • torch.randn(4,5)
    • torch.tensor([[1,2,3],[4,5,6],[7,8,9]])
    • 数组的加减乘除
    • torch.exp(x)
    • Z1=torch.cat((X,Y),dim=0)
    • Z=X==Y
    • X.sum()
    • 矩阵的广播机制,形状不一样的矩阵变成形状相同的矩阵
    • 矩阵中元素的获取
    • before =id(Y)
    • torch.zeros_like(Y)
    • Z[:]=X+Y
    • print(type(A),type(B))
    • print(a,a.item(),int(a),float(a))
    • print(len(x))
    • B=A.T
    • B=A.clone()
    • print(A.sum(axis=[0,1])
    • print(A.cumsum(axis=0))
    • z=x*y 张量的乘法
    • y=torch.mv(A,x)
    • np.linalg.norm(A)
  • 附录

文件的读写

创建.csv类型的文件,并读取文件

.csv类型的文件是以逗号风格的字符串类型的数据

import os//导入操作计算机系统的对象os.makedirs(os.path.join('..','data'),exist_ok=True)//在当前项目所在的目录下创建文件data,如果文件存在也不报异常data_file=os.path.join('..','data','house_tiny.cvs')//在data目录下创建.csv文件with open(data_file,'w') as f://打开文件的写权限并写入数据f.write('NumRooms,Alley,Price\n')f.write('NA,Pave,127500\n')f.write('2,NA,106000\n')f.write('4,NA,178100\n')f.write('NA,NA,140000\n')import pandas//pandas主要用于数据的读取操作data=pandas.read_csv(data_file)//读取.csv类型的数据print(data) NumRooms Alley Price0 NaNPave1275001 2.0 NaN1060002 4.0 NaN1781003 NaN NaN140000
import osos.makedirs(os.path.join('D:/File/test','data'),exist_ok=True)data_file=os.path.join('D:/File/test','data','house_tiny.csv')with open(data_file,'w') as f:f.write('NumRooms,Alley,Price\n')f.write('2.0,Pave,127500\n')f.write('4.0,NA,178100\n')f.write('NA,NA,140000\n')f.write('NA,Pave,127500\n')import pandasdata =pandas.read_csv(data_file)print(data)
import torchimport osos.makedirs(os.path.join("D:/File/test","data"),exist_ok=True)data_file=os.path.join("D:/File/test","data","house_tiny.csv")with open(data_file,"w") as f:f.write('NumRooms,Alley,Price\n')f.write('2.0,Pave,127500\n')f.write('4.0,NA,178100\n')f.write('NA,NA,140000\n')f.write('NA,Pave,127500\n')import pandasdata =pandas.read_csv(data_file)print(data)inputs ,outputs =data.iloc[:,0:2],data.iloc[:,2]inputs=inputs.fillna(inputs.select_dtypes(include="number").mean())//fillna的作用填充空值,用平均值进行填充print(inputs)inputs=pandas.get_dummies(inputs,dummy_na=True,dtype=numpy.int8)print(inputs)print(outputs)X,y=torch.tensor(inputs.values),torch.tensor(outputs.values)print(X)print(y) NumRooms Alley Price0 2.0Pave1275001 4.0 NaN1781002 NaN NaN1400003 NaNPave127500 NumRooms Alley0 2.0Pave1 4.0 NaN2 3.0 NaN3 3.0Pave NumRoomsAlley_PaveAlley_nan0 2.0 101 4.0 012 3.0 013 3.0 100127500117810021400003127500Name: Price, dtype: int64tensor([[2., 1., 0.],[4., 0., 1.],[3., 0., 1.],[3., 1., 0.]], dtype=torch.float64)tensor([127500, 178100, 140000, 127500])

上述代码所用到的知识点总结
Pandas读取某列、某行数据——loc、iloc用法总结
pandas操作4(处理缺失值/位置索引)
【深入浅出学习笔记】李沐《动手学深度学习2.0》之数据预处理学习

import osos.makedirs(os.path.join("D:/File/test","testdata"),exist_ok=True)data_file=os.path.join("D:/File/test","testdata","house.csv")with open(data_file,'w') as f:f.write("size,price\n")f.write('20,10\n')f.write('30,20\n')import pandasdata=pandas.read_csv(data_file)print(data)

创建.xlsx文件

import pandas as pdimport numpy as npimport osos.makedirs(os.path.join("D:/","实验数据"),exist_ok=True)# 生成DataFramedata = pd.DataFrame(np.arange(30).reshape((6, 5)),columns=['A', 'B', 'C', 'D', 'E'])# 写入本地data.to_excel("D:\\实验数据\\data.xlsx", sheet_name="data")print(data)

使用Python做图

import numpyfrom matplotlib_inline import backend_inlinefrom d2l import torch as d2ldef f(x):return 3*x**2-4*xdef numerical_lim(f,x,h):return (f(x+h)-f(x))/hh=0.1for i in range(5):print(f"h={h:.5f},numerical limit={numerical_lim(f,1,h):.5f}")h*=0.1//这是一个求导的过程def use_svg_display():#@savebackend_inline.set_matplotlib_formats('svg')//通过在线的方式,设置画布类型为svgdef set_figsize(figsize):#@saveuse_svg_display()d2l.plt.rcParams['figure.figsize']=figsize//设置画布的尺寸为figsize#@savedef set_axes(axes,xlabel,ylabel,xlim,ylim,xscale,yscale,legend):axes.set_xlabel(xlabel)//设置X轴的标签axes.set_ylabel(ylabel)axes.set_xlim(xlim)//设置X轴上的数值axes.set_ylim(ylim)axes.set_xscale(xscale)//设置X轴上的数值的缩放比例axes.set_yscale(yscale)if legend:axes.legend(legend)//axes.legend()函数说明图例的位置等相关属性axes.grid()//grid()函数用于设置绘图区网格线。#@savedef plot(X,Y=None,xlabel=None,ylabel=None,legend=None,xlim=None,ylim=None,xscale='linear',yscale='linear', fmts=('-','m--','g-.','r:'),figsize=(3.5,2.5),axes=None):// 这是一种在Matplotib中设置图形线条样式的方式。在Matplotib中,可以使用fmts参数来设置线条样式。fmts参数是一个字符串,由一个或多个字符//组成,每个字符都代表了一个设计元素,用于设置线条的颜色、线型和标记。//在给定的fmts参数中,每个字符的含义如下://·第一个字符(-')代表线的颜色,这里的-'代表黑色。//·第二个字符('m')代表线的样式,这里的'm--'代表品红色的虚线。·第三个字符('g')代表线的颜色,这里的'g'代表绿色。//·第四个字符(r')代表线的颜色,这里的'r:'代表红色的点线。//通过在Matplotlib中设置fmts参数,可以使不同的线条呈现不同的颜色、样式和标记,从而更加直观地表达数据之间的关系。if legend is None:legend=[]set_figsize(figsize)axes=axes if axes else d2l.plt.gca()//[动手学深度学习-导数和微分-第二章plot函数理解](https://blog.csdn.net/qyk666/article/details/134756786)def has_one_axis(X):return (hasattr(X,'ndim') and X.ndim==1 or isinstance(X,list) and not hasattr(X[0],"__len__"))if has_one_axis(X):X=[X]if Y is None:X,Y=[[]]*len(X),Xelif has_one_axis(Y):Y=[Y]if len(X)!=len(Y):X=X*len(Y)axes.cla()for x,y, fmt in zip(X,Y,fmts):if len(x):axes.plot(x,y,fmt)else:axes.plot(y,fmt)set_axes(axes,xlabel,ylabel,xlim,ylim,xscale,yscale,legend)x=numpy.arange(0,3,0.1)plot(x,[f(x),2*x-3],'x','f(x)',legend=['f(x)','Tangent line (x=1)'])plt.show()""""""import numpyimport mathfrom d2l import torch as d2ldef normal(x,mu,sigma):p=1/np.sqrt(2*math.pi*sigma**2)return p*np.exp(-0.5/sigma**2*(x-mu)**2)x=numpy.arange(-10,10,0.01)params=[(0,1),(0,2),(3,1)]d2l.plot(x,[normal(x,mu,sigma) formu,sigma in params],xlabel='x',ylabel='p(x)',figsize=(4.5,2.5), legend=[f'mean{mu},std{sigma}' for mu ,sigma in params])plt.show()""""""import randomimport torchfrom d2l import torch as d2ldef synthetic_data(w,b,num_examples):#@saveX=torch.normal(0,1,(num_examples,len(w)))y=torch.matmul(X,w)+by+=torch.normal(0,0.01,y.shape)return X,y.reshape((-1,1))true_w=torch.tensor([2,-3.4])true_b=4.2features ,labels=synthetic_data(true_w,true_b,1000)print('features:',features[0],'\nlabel:',labels[0])# print(labels)d2l.set_figsize()# print(features[:,(1)].detach().numpy())# print('fasfsadfsdfsdf')# print(labels.detach().numpy)d2l.plt.scatter(features[:,(1)].detach().numpy(),labels,1)plt.show()"""

生成数据集

切片取值操作

修改张量中指定位置的数据

import torchX=torch.zeros(3,4)X[1,1]=1Y=torch.ones(3,4)Y[0,0]=0print(X==Y)print(X>Y)print((X<Y))

知识点

torch.arange(x)

作用:依据x生成一个数组

import torchx=torch.arange(12)print(x)tensor([ 0,1,2,3,4,5,6,7,8,9, 10, 11])
import numpyx=numpy.arange(12)print(x)[ 0123456789 10 11]
import torchx=torch.arange(4)print(x)print(x[2])

torch.tensor(2)

作用:生成指定的张量

import torchx=torch.tensor(2)y=torch.tensor(3)print(x+y,x-y,x*y,x/y,x**y)tensor(5) tensor(-1) tensor(6) tensor(0.6667) tensor(8)

A=torch.randn(36).reshape(6,6)

作用:生成符合正太分布的数据

A=torch.randn(36).reshape(6,6)print(A+A.T)A=torch.arange(25).reshape(5,5)print(A)print(len(A))print(A/A.sum(axis=1))print(A.sum(axis=1))print(A)

shape

作用:查看调用者的形状或者称呼为尺寸

import torchx=torch.arange(12)a=x.shapeprint(a)torch.Size([12])
import numpyx=numpy.arange(12)a=x.shapeprint(a)(12,)
import torchA=torch.arange(24).reshape(2,3,4)print(A.shape,A.shape[0],A.shape[1],A.shape[2])

numel()

作用:(名称的字面意思元素的数量)查看元素的数量

import torchx=torch.arange(12)a=x.numel()print(a)12

reshape(x,y,z)

作用:将数据的展示形状进行重新的塑形

import torchx=torch.arange(12)X=x.reshape(3,4)print(X)tensor([[ 0,1,2,3],[ 4,5,6,7],[ 8,9, 10, 11]])
import numpyx=numpy.arange(12)X=x.reshape(3,4)print(X)[[ 0123] [ 4567] [ 89 10 11]]

-1的作用:依据数据自动计算数值并填入

import torchx=torch.arange(12)X=x.reshape(-1,4)print(X)tensor([[ 0,1,2,3],[ 4,5,6,7],[ 8,9, 10, 11]])
import torchx=torch.arange(12)X=x.reshape(3,-1)print(X)tensor([[ 0,1,2,3],[ 4,5,6,7],[ 8,9, 10, 11]])
import numpyx=numpy.arange(12)X=x.reshape(-1,4)print(X)[[ 0123] [ 4567] [ 89 10 11]]
import numpyx=numpy.arange(12)X=x.reshape(3,-1)print(X)[[ 0123] [ 4567] [ 89 10 11]]

torch.zeros(3,3,4)

作用:生成三维全为零的数据

import torchX=torch.zeros(3,3,4)print(X)tensor([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]],[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]],[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]])
import numpy as npX=np.zeros((2,2,3),int)print(X)[[[0 0 0][0 0 0]] [[0 0 0][0 0 0]]]

torch.ones(2,3,4)

作用:生成三维全为1的数据

import torch as toX=to.ones(2,2,3)print(X)tensor([[[1., 1., 1.], [1., 1., 1.]],[[1., 1., 1.], [1., 1., 1.]]])

在python中想要改变函数的返回结果,往往只需要在参数列表中在添加一些参数

import numpy asnpX=np.ones((2,2,2),float)print(X)[[[1. 1.][1. 1.]] [[1. 1.][1. 1.]]]

torch.randn(4,5)

作用:生成符合正太分布的数据

import torchX=torch.randn(4,5)print(X)tensor([[-1.1866,1.1176,1.0693, -1.0216,0.1562],[ 0.1815,0.3246,1.1276,0.5653, -1.4328],[ 0.1206,0.6508, -0.4501,0.0958,0.7154],[ 1.1551,0.1163,0.7360,0.7723, -0.1527]])
import numpyX=numpy.random.randn(3,4)print(X)[[ 0.77655555 -0.89418554 -1.27220862 -0.52597834] [-0.20755957 -0.53087554 -0.833616931.29417959] [ 0.493744871.554696260.53871618 -0.01278069]]

torch.tensor([[1,2,3],[4,5,6],[7,8,9]])

自定义数据

import torchX=torch.tensor([[1,2,3],[4,5,6],[7,8,9]])print(X)tensor([[1, 2, 3],[4, 5, 6],[7, 8, 9]])

数组的加减乘除

import torchx=torch.tensor([1,2,4,8])y=torch.tensor([2,2,2,2])print(str(x+y)+"\n"+str(x-y)+"\n"+str(x*y)+"\n"+str(x/y)+"\n"+str(x**y)+"\n")tensor([ 3,4,6, 10])tensor([-1,0,2,6])tensor([ 2,4,8, 16])tensor([0.5000, 1.0000, 2.0000, 4.0000])tensor([ 1,4, 16, 64])

torch.exp(x)

作用生成数学公式中e函数

import torchx=torch.tensor([1,2,3,4])x=torch.exp(x)print(x)tensor([ 2.7183,7.3891, 20.0855, 54.5981])
import numpyx=numpy.array([1,2,3,4],float)print(x)x=numpy.exp(x)print(x)[1. 2. 3. 4.][ 2.718281837.389056120.08553692 54.59815003]

Z1=torch.cat((X,Y),dim=0)

作用:合并数据存储类型,dim=0按照列合并,dim=1按照行合并

import torchX=torch.arange(12,dtype=torch.int).reshape(3,4)print(X)Y=torch.tensor([[1,2,3,4],[2,2,2,2],[6,6,6,6]])Z1=torch.cat((X,Y),dim=0)Z2=torch.cat((X, Y),dim=1)print(Y)print(Z1)print(Z2)tensor([[ 0,1,2,3],[ 4,5,6,7],[ 8,9, 10, 11]], dtype=torch.int32)tensor([[1, 2, 3, 4],[2, 2, 2, 2],[6, 6, 6, 6]])tensor([[ 0,1,2,3],[ 4,5,6,7],[ 8,9, 10, 11],[ 1,2,3,4],[ 2,2,2,2],[ 6,6,6,6]])tensor([[ 0,1,2,3,1,2,3,4],[ 4,5,6,7,2,2,2,2],[ 8,9, 10, 11,6,6,6,6]])

Z=X==Y

作用:比较两个矩阵是否相等,比较的是两个数据存储结构中的每一位元素是否相等

import torchX=torch.arange(12).reshape(3,4)print(X)Y=torch.tensor([[1,2,3,4],[2,2,2,2],[6,6,6,6]])print(Y)Z=X==Yprint(Z)

X.sum()

作用 :将矩阵中所有的元素相加

import torchX=torch.arange(12).reshape(3,4)print(X.sum())
import torchx=torch.arange(4)print(x)print(x.sum())

矩阵的广播机制,形状不一样的矩阵变成形状相同的矩阵

作用:广播机制可以使形状不一样的矩阵变成一样的矩阵

"import torcha=torch.arange(3).reshape(3,1)print(a)b=torch.arange(2).reshape(1,2)print(b)C=a+bprint(C)tensor([[0],[1],[2]])tensor([[0, 1]])tensor([[0, 1],[1, 2],[2, 3]])
import torchA=torch.tensor([[[1,2,3], [4,5,6]],[[1,2,3],[1,2,3]]])print(A)a=2A=a+Aprint(A)A=A*aprint(A)

矩阵中元素的获取

作用:获取矩阵中特定位置的元素

import torchX=torch.arange(12).reshape(3,4)print(X)print(X[-1])print(X[1,2])X[1,2]=111print(X)print(X[0:2])X[0:2]=111111print(X)X[0:2,:]=222222print(X)tensor([[ 0,1,2,3],[ 4,5,6,7],[ 8,9, 10, 11]])tensor([ 8,9, 10, 11])tensor(6)tensor([[0, 1, 2, 3],[4, 5, 111, 7],[8, 9,10,11]])tensor([[0, 1, 2, 3],[4, 5, 111, 7]])tensor([[111111, 111111, 111111, 111111],[111111, 111111, 111111, 111111],[ 8,9, 10, 11]])tensor([[222222, 222222, 222222, 222222],[222222, 222222, 222222, 222222],[ 8,9, 10, 11]])

before =id(Y)

作用:查看对象在内存中的存储位置

import torchY=torch.ones(3,4)X=torch.ones(3,4)before =id(Y)print(before)Y=X+Yafter=id(Y)print(after)23922638294402391076947984

torch.zeros_like(Y)

作用:形成一个像Y一样的全为零的张量

import torchY = torch.zeros(3,4)X = torch.ones(3,4)Z = torch.zeros_like(Y)print(id(Z))Z[:]=X+Yprint(id(Z))22553585532002255358553200

Z[:]=X+Y

作用:可以使Z在内存中的存储位置不变(使用+=这种类型的符合也不会改变对象在内存中的存储位置)

import torchY = torch.zeros(3,4)X = torch.ones(3,4)Z = torch.zeros_like(Y)print(id(Z))Z[:]=X+Yprint(id(Z))22553585532002255358553200
import torchX=torch.zeros(3,2)Y=torch.ones(3,2)before=id(X)X+=Yprint(before==id(X))True
import torchX=torch.zeros(2,4,1)Y=torch.ones(2,4,4)Y[:]=X+Yprint(Y)tensor([[[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]],[[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]])

print(type(A),type(B))

作用:查看数据的数据类型

import torchX=torch.ones(3,4)A=X.numpy()B=torch.tensor(A)print(type(A),type(B))print(A)<class 'numpy.ndarray'> <class 'torch.Tensor'>[[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]]

print(a,a.item(),int(a),float(a))

作用:数据类型的强制转换

import torcha=torch.tensor([3.3])print(a,a.item(),int(a),float(a))tensor([3.3000]) 3.299999952316284 3 3.299999952316284

print(len(x))

作用:查看张量的长度

import torchx=torch.arange(4)print(x)print(len(x))print(x.shape)

B=A.T

作用:将张量进行转置

import torchA=torch.arange(20).reshape(4,5)print(A)print(A[1,1])B=A.Tprint(B)print(B.shape)
A=torch.arange(20).reshape(5,4)print(A)print(A.T)print(A.T.T)B=torch.arange(30).reshape(5,4)print(A.T+B.T==(A+B).T)

B=A.clone()

作用:对张量A进行克隆

import torchA=torch.arange(24).reshape(2,3,4)B=A.clone()print(A)C=A*Bprint(C)

print(A.sum(axis=[0,1])

作用:axis=0代表列,axis=1代表行

import torchA=torch.arange(20,dtype=torch.float32).reshape(5,4)print(A.shape)print(A.sum())print(A.sum(axis=0))print(A)print(A.shape)print(A.sum(axis=1))print(A.sum(axis=[0,1]))print(A.numel())print(A.mean(),A.sum()/A.numel())print(A.shape[0])print(A.mean(axis=0),A.sum(axis=0)/A.shape[0])
import torchA=torch.arange(24).reshape(2,3,4)print(A.sum(axis=0))print(A.sum(axis=1))print(A.sum(axis=2))print(A)

print(A.cumsum(axis=0))

作用:列中的值,等于列中这个位置的值加上此列中这个位置上面的所有的值

import torchA=torch.arange(20,dtype=torch.float32).reshape(5,4)sum=A.sum(axis=1,keepdims=True)print(sum)print(A/sum)print(A.cumsum(axis=0))print(A)tensor([[ 6.],[22.],[38.],[54.],[70.]])tensor([[0.0000, 0.1667, 0.3333, 0.5000],[0.1818, 0.2273, 0.2727, 0.3182],[0.2105, 0.2368, 0.2632, 0.2895],[0.2222, 0.2407, 0.2593, 0.2778],[0.2286, 0.2429, 0.2571, 0.2714]])tensor([[ 0.,1.,2.,3.],[ 4.,6.,8., 10.],[12., 15., 18., 21.],[24., 28., 32., 36.],[40., 45., 50., 55.]])tensor([[ 0.,1.,2.,3.],[ 4.,5.,6.,7.],[ 8.,9., 10., 11.],[12., 13., 14., 15.],[16., 17., 18., 19.]])

z=x*y 张量的乘法

作用:张量中对应位置的元素相乘

import torchx=torch.arange(20).reshape(4,5)y=torch.arange(20).reshape(4,5)z=x*yprint(z)tensor([[0, 1, 4, 9,16],[ 25,36,49,64,81],[100, 121, 144, 169, 196],[225, 256, 289, 324, 361]])

y=torch.mv(A,x)

作用:矩阵和向量相乘,矩阵的行乘以向量的列并相加

import torchx=torch.arange(4,dtype=torch.float32)A=torch.arange(20,dtype=torch.float32).reshape(5,4)print(x.shape,A.shape)y=torch.mv(A,x)print(y)torch.Size([4]) torch.Size([5, 4])tensor([ 14.,38.,62.,86., 110.])

np.linalg.norm(A)

作用:用于求张量的范数,默认求的是二范数

import torchimport numpy as npA=torch.arange(24).reshape(2,3,4)print(np.linalg.norm(A))

附录

关于文中提到的张量
在文中我提到的张量可以是众所周知的一维数组,也可以使二维数组(矩阵),但是三维、四维数组很难用特有的名词来展示,这里对一维、二维、三维、、、等统称为张量。当读到张量时我们需要根据上下文的代码自行推断。
关于文中引用到的链接
在文中引用到的链接,均是上下文中代码知识的拓展,