Pytorch 2.3.1 预备知识 pandas
预备知识 pandas
创建一个数据
#pandas预备知识 
import os 
os.makedirs(os.path.join('data'),exist_ok=True) #exist_ok = True 可以在文件已有的时候不创建
data_file = os.path.join('data','hourse.csv')
with open(data_file,'w') as f: 
    f.write("NumRooms,Alley,Prices\n")
    f.write("NA,Pave,213231232\n") 
    f.write("2,NA,213231231\n")
    f.write("4,NA,213214214\n")
    f.write("NA,NA,245531444")
使用pandas查看创建好的csv文件
import pandas as pd 
data = pd.read_csv(data_file) 
data
输出
     NumRooms	Alley	Prices
0	NaN	Pave	213231232
1	2.0	NaN	213231231
2	4.0	NaN	213214214
3	NaN	NaN	245531444
将数据划分,用 data.iloc[:]进行划分数据
inputs,outputs = data.iloc[:,0:2],data.iloc[:,2]
inputs = inputs.fillna(inputs.mean()) #在NaN处填充均值 
inputs
输出
| NumRooms | Alley | 
|---|---|
| 3.0 | Pave | 
| 2.0 | NaN | 
| 4.0 | NaN | 
| 3.0 | NaN | 
将如上数据转换成张量格式
import torch 
x,y = torch.tensor(inputs.values),torch.tensor(outputs.values)
x,y
输出
(tensor([[3., 1., 0.],
         [2., 0., 1.],
         [4., 0., 1.],
         [3., 0., 1.]], dtype=torch.float64),
 tensor([213231232, 213231231, 213214214, 245531444]))
posted on 2021-12-11 16:42 YangShusen' 阅读(242) 评论(0) 收藏 举报
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号