赞
踩
import os
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(data_file)
print(data)
"""
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:2], data.iloc[:, 2] # 插值-平均值 inputs = inputs.fillna(inputs.mean()) print(inputs) """ NumRooms Alley 0 3.0 Pave 1 2.0 NaN 2 4.0 NaN 3 3.0 NaN """ # 对于inputs中的类别值或离散值,我们将“NaN”视为一个类别 inputs = pd.get_dummies(inputs, dummy_na=True) print(inputs) """ NumRooms Alley_Pave Alley_nan 0 3.0 1 0 1 2.0 0 1 2 4.0 0 1 3 3.0 0 1 """
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([127500, 106000, 178100, 140000]))
"""
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。