栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

02.pandas

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

02.pandas

文章目录
  • 数据预处理
  • 处理缺失值
  • 转换为张量

数据预处理
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,Pricen')
    f.write('NA,Pave,127500n')
    f.write('2,NA,106000n')
    f.write('4,NA,178100n')
    f.write('NA,NA,140000n')
import pandas as pd

data = pd.read_csv(data_file)
data
NumRoomsAlleyPrice
0NaNPave127500
12.0NaN106000
24.0NaN178100
3NaNNaN140000
处理缺失值
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


C:UsersluciferAppDataLocalTemp/ipykernel_17604/2223495382.py:2: FutureWarning: Dropping of nuisance columns in Dataframe reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError.  Select only valid columns before calling the reduction.
  inputs=inputs.fillna(inputs.mean())
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]))
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/303151.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号