#下面是练习
import os
import pandas as pd
import torch
os.makedirs(os.path.join(‘…’,‘data’),exist_ok=True)
data_file = os.path.join(‘…’,‘data’,‘test.csv’)
with open(data_file, ‘w’) as f:
f.write(‘Animals,Numbers,Heights,Weights,Ages,Features\n’) # 动物们,总数量,平均高度,平均体重,平均年龄,大小特征
f.write(‘Tigers,4,85.5,68.2,6.4,Big\n’) # 每行表示一个数据样本
f.write(‘Cats,2,NA,7.3,6.5,Small\n’)
f.write(‘Lions,NA,106,74.8,NA,Big\n’)
f.write(‘Dogs,12,62.7,25.8,5.4,Small\n’)
f.write(‘Panda,1,NA,46,9.9,NA\n’)
f.write(‘Cattles,8,143.2,NA,12.5,Big\n’)
data = pd.read_csv(data_file)
print(data)
data = pd.get_dummies(data,columns=[‘Features’],dummy_na=True)
sum_na = data.isna().sum()
column_drop = sum_na.index[sum_na.argmax()]
data = data.drop(columns = column_drop)
print(data)
outpus = data.iloc[:,1:]
outpus = torch.tensor(outpus.to_numpy(dtype=float))
outpus = torch.tensor(outpus.values,dtype=torch.float32)
print(outpus)