.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 127500
NumRooms Alley
0 2.0 Pave
1 4.0 NaN
2 3.0 NaN
3 3.0 Pave
NumRooms 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)
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)
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))
作用:依据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.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)
作用:查看调用者的形状或者称呼为尺寸
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
x=torch.arange(12)
a=x.numel()
print(a)
12
作用:将数据的展示形状进行重新的塑形
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]]
作用:生成三维全为零的数据
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]]]
作用:生成三维全为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.]]]
作用:生成符合正太分布的数据
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]]
自定义数据
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])
作用生成数学公式中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]
作用:合并数据存储类型,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]])
作用:比较两个矩阵是否相等,比较的是两个数据存储结构中的每一位元素是否相等
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)
作用 :将矩阵中所有的元素相加
import torch
X=torch.arange(12).reshape(3,4)
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
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]])
作用:查看对象在内存中的存储位置
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
作用:形成一个像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在内存中的存储位置不变(使用+=这种类型的符合也不会改变对象在内存中的存储位置)
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.]]])
作用:查看数据的数据类型
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.]]
作用:数据类型的强制转换
import torch
a=torch.tensor([3.3])
print(a,a.item(),int(a),float(a))
tensor([3.3000]) 3.299999952316284 3 3.299999952316284