import os
path='路径'
os.makedirs(os.path.join('..', 'data'), exist_ok=True)
data_file = os.path.join('..', 'data', 'house_tiny.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 as pd
data = pd.read_csv('./house_tiny.csv' )
print(data)
print('--------------------')
'''
CSV
--------------------
NumRooms Alley Price
0 NaN Pave 127500
1 2.0 NaN 106000
2 4.0 NaN 178100
3 NaN NaN 140000
'''
inputs, outputs = data.iloc[:, 0], data.iloc[:, 2]
#inputs -- 0,1列的所有行 outputs -- 2列的所有行
inputs = inputs.fillna(value=inputs.mean()) #mean,median,min,max
#number NA -> mean
print(inputs)
'''
0 3.0
1 2.0
2 4.0
3 3.0
'''
inputs = data.iloc[:, 0:2]
inputs = pd.get_dummies(inputs, dummy_na=True)#分为pave nan两种类型
print(inputs)
'''
Name: NumRooms, dtype: float64
NumRooms Alley_Pave Alley_nan
0 NaN True False
1 2.0 False True
2 4.0 False True
3 NaN False True
'''
import torch
x = torch.tensor(inputs.to_numpy(dtype=float))
y = torch.tensor(outputs.to_numpy(dtype=float))
print(x,y)
'''
tensor([[nan, 1., 0.],
[2., 0., 1.],
[4., 0., 1.],
[nan, 0., 1.]],
dtype=torch.float64)
tensor([127500., 106000., 178100., 140000.],
dtype=torch.float64)
'''
(DL笔记)Dive into Deep Learning -- CSV
最新推荐文章于 2024-07-25 14:12:59 发布