文章目录
- 文件的读写
- 创建.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 Price
0 NaN Pave 127500
1 2.0 NaN 106000
2 4.0 NaN 178100
3 NaN NaN 140000
import os
os.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 pandas
data =pandas.read_csv(data_file)
print(data)
import torch
import os
os.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 pandas
data =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 Price
0 2.0 Pave 127500
1 4.0 NaN 178100
2 NaN NaN 140000
3 NaN Pave 127500NumRooms Alley
0 2.0 Pave
1 4.0 NaN
2 3.0 NaN
3 3.0 PaveNumRooms Alley_Pave Alley_nan
0 2.0 1 0
1 4.0 0 1
2 3.0 0 1
3 3.0 1 0
0 127500
1 178100
2 140000
3 127500
Name: Price, dtype: int64
tensor([[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 os
os.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 pandas
data=pandas.read_csv(data_file)
print(data)
创建.xlsx文件
import pandas as pd
import numpy as np
import os
os.makedirs(os.path.join("D:/","实验数据"),exist_ok=True)
# 生成DataFrame
data = 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 numpy
from matplotlib_inline import backend_inline
from d2l import torch as d2l
def f(x):return 3*x**2-4*xdef numerical_lim(f,x,h):return (f(x+h)-f(x))/hh=0.1
for i in range(5):print(f"h={h:.5f},numerical limit={numerical_lim(f,1,h):.5f}")h*=0.1def use_svg_display():#@savebackend_inline.set_matplotlib_formats('svg')def set_figsize(figsize):#@saveuse_svg_display()d2l.plt.rcParams['figure.figsize']=figsize#@save
def set_axes(axes,xlabel,ylabel,xlim,ylim,xscale,yscale,legend):axes.set_xlabel(xlabel)axes.set_ylabel(ylabel)axes.set_xlim(xlim)axes.set_ylim(ylim)axes.set_xscale(xscale)axes.set_yscale(yscale)if legend:axes.legend(legend)axes.grid()#@save
def 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):if legend is None:legend=[]set_figsize(figsize)axes=axes if axes else d2l.plt.gca()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 numpy
import math
from d2l import torch as d2l
def 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) for mu,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 random
import torch
from d2l import torch as d2l
def 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.2
features ,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 torch
X=torch.zeros(3,4)
X[1,1]=1
Y=torch.ones(3,4)
Y[0,0]=0
print(X==Y)
print(X>Y)
print((X<Y))
知识点
torch.arange(x)
作用:依据x生成一个数组
import torch
x=torch.arange(12)
print(x)
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
import numpy
x=numpy.arange(12)
print(x)
[ 0 1 2 3 4 5 6 7 8 9 10 11]
import torch
x=torch.arange(4)
print(x)
print(x[2])
torch.tensor(2)
作用:生成指定的张量
import torch
x=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 torch
x=torch.arange(12)
a=x.shape
print(a)
torch.Size([12])
import numpy
x=numpy.arange(12)
a=x.shape
print(a)
(12,)
import torch
A=torch.arange(24).reshape(2,3,4)
print(A.shape,A.shape[0],A.shape[1],A.shape[2])
numel()
作用:(名称的字面意思元素的数量)查看元素的数量
import torch
x=torch.arange(12)
a=x.numel()
print(a)
12
reshape(x,y,z)
作用:将数据的展示形状进行重新的塑形
import torch
x=torch.arange(12)
X=x.reshape(3,4)
print(X)
tensor([[ 0, 1, 2, 3],[ 4, 5, 6, 7],[ 8, 9, 10, 11]])
import numpy
x=numpy.arange(12)
X=x.reshape(3,4)
print(X)
[[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]]
-1的作用:依据数据自动计算数值并填入
import torch
x=torch.arange(12)
X=x.reshape(-1,4)
print(X)
tensor([[ 0, 1, 2, 3],[ 4, 5, 6, 7],[ 8, 9, 10, 11]])
import torch
x=torch.arange(12)
X=x.reshape(3,-1)
print(X)
tensor([[ 0, 1, 2, 3],[ 4, 5, 6, 7],[ 8, 9, 10, 11]])
import numpy
x=numpy.arange(12)
X=x.reshape(-1,4)
print(X)
[[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]]
import numpy
x=numpy.arange(12)
X=x.reshape(3,-1)
print(X)
[[ 0 1 2 3][ 4 5 6 7][ 8 9 10 11]]
torch.zeros(3,3,4)
作用:生成三维全为零的数据
import torch
X=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 np
X=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 to
X=to.ones(2,2,3)
print(X)
tensor([[[1., 1., 1.],[1., 1., 1.]],[[1., 1., 1.],[1., 1., 1.]]])
在python中想要改变函数的返回结果,往往只需要在参数列表中在添加一些参数
import numpy as np
X=np.ones((2,2,2),float)
print(X)
[[[1. 1.][1. 1.]][[1. 1.][1. 1.]]]
torch.randn(4,5)
作用:生成符合正太分布的数据
import torch
X=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 numpy
X=numpy.random.randn(3,4)
print(X)
[[ 0.77655555 -0.89418554 -1.27220862 -0.52597834][-0.20755957 -0.53087554 -0.83361693 1.29417959][ 0.49374487 1.55469626 0.53871618 -0.01278069]]
torch.tensor([[1,2,3],[4,5,6],[7,8,9]])
自定义数据
import torch
X=torch.tensor([[1,2,3],[4,5,6],[7,8,9]])
print(X)
tensor([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
数组的加减乘除
import torch
x=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 torch
x=torch.tensor([1,2,3,4])
x=torch.exp(x)
print(x)
tensor([ 2.7183, 7.3891, 20.0855, 54.5981])
import numpy
x=numpy.array([1,2,3,4],float)
print(x)
x=numpy.exp(x)
print(x)
[1. 2. 3. 4.]
[ 2.71828183 7.3890561 20.08553692 54.59815003]
Z1=torch.cat((X,Y),dim=0)
作用:合并数据存储类型,dim=0按照列合并,dim=1按照行合并
import torch
X=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 torch
X=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==Y
print(Z)
X.sum()
作用 :将矩阵中所有的元素相加
import torch
X=torch.arange(12).reshape(3,4)
print(X.sum())
import torch
x=torch.arange(4)
print(x)
print(x.sum())
矩阵的广播机制,形状不一样的矩阵变成形状相同的矩阵
作用:广播机制可以使形状不一样的矩阵变成一样的矩阵
"import torch
a=torch.arange(3).reshape(3,1)
print(a)
b=torch.arange(2).reshape(1,2)
print(b)
C=a+b
print(C)
tensor([[0],[1],[2]])
tensor([[0, 1]])
tensor([[0, 1],[1, 2],[2, 3]])
import torch
A=torch.tensor([[[1,2,3],[4,5,6]],[[1,2,3],[1,2,3]]])
print(A)
a=2
A=a+A
print(A)
A=A*a
print(A)
矩阵中元素的获取
作用:获取矩阵中特定位置的元素
import torch
X=torch.arange(12).reshape(3,4)
print(X)
print(X[-1])
print(X[1,2])
X[1,2]=111
print(X)
print(X[0:2])
X[0:2]=111111
print(X)
X[0:2,:]=222222
print(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 torch
Y=torch.ones(3,4)
X=torch.ones(3,4)
before =id(Y)
print(before)
Y=X+Y
after=id(Y)
print(after)
2392263829440
2391076947984
torch.zeros_like(Y)
作用:形成一个像Y一样的全为零的张量
import torch
Y = torch.zeros(3,4)
X = torch.ones(3,4)
Z = torch.zeros_like(Y)
print(id(Z))
Z[:]=X+Y
print(id(Z))
2255358553200
2255358553200
Z[:]=X+Y
作用:可以使Z在内存中的存储位置不变(使用+=这种类型的符合也不会改变对象在内存中的存储位置)
import torch
Y = torch.zeros(3,4)
X = torch.ones(3,4)
Z = torch.zeros_like(Y)
print(id(Z))
Z[:]=X+Y
print(id(Z))
2255358553200
2255358553200
import torch
X=torch.zeros(3,2)
Y=torch.ones(3,2)
before=id(X)
X+=Y
print(before==id(X))
True
import torch
X=torch.zeros(2,4,1)
Y=torch.ones(2,4,4)
Y[:]=X+Y
print(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 torch
X=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 torch
a=torch.tensor([3.3])
print(a,a.item(),int(a),float(a))
tensor([3.3000]) 3.299999952316284 3 3.299999952316284
print(len(x))
作用:查看张量的长度
import torch
x=torch.arange(4)
print(x)
print(len(x))
print(x.shape)
B=A.T
作用:将张量进行转置
import torch
A=torch.arange(20).reshape(4,5)
print(A)
print(A[1,1])
B=A.T
print(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 torch
A=torch.arange(24).reshape(2,3,4)
B=A.clone()
print(A)
C=A*B
print(C)
print(A.sum(axis=[0,1])
作用:axis=0代表列,axis=1代表行
import torch
A=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 torch
A=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 torch
A=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 torch
x=torch.arange(20).reshape(4,5)
y=torch.arange(20).reshape(4,5)
z=x*y
print(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 torch
x=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 torch
import numpy as np
A=torch.arange(24).reshape(2,3,4)
print(np.linalg.norm(A))
附录
关于文中提到的张量
在文中我提到的张量可以是众所周知的一维数组,也可以使二维数组(矩阵),但是三维、四维数组很难用特有的名词来展示,这里对一维、二维、三维、、、等统称为张量。当读到张量时我们需要根据上下文的代码自行推断。
关于文中引用到的链接
在文中引用到的链接,均是上下文中代码知识的拓展,