b站:https://space.bilibili.com/1567748478/channel/seriesdetail?sid=358497
教材:https://zh-v2.d2l.ai/chapter_preface/index.html#sec-code
我没有按照他的来 我使用的是anaconda
1)anaconda prompt
2) cd 到下载好的jupyter记事本位置
3)jupyter notebook
即可
(沐神没告诉我在jupyter记事本的位置…尬住
数据操作:notebooks/pytorch/chapter_preliminaries/ndarray.ipynb
数据预处理:notebooks/pytorch/chapter_preliminaries/pandas.ipynb
下载torch很慢
→ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple torch
我保存在了./data1/house_tiny.csv
import os
os.makedirs(os.path.join('.', 'data1'), exist_ok=True)
data_file = os.path.join('.', 'data1', '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')
os.makedirs 创建目录
os.path.join 路径拼接函数
注意,“NaN”项代表缺失值。 [为了处理缺失的数据,典型的方法包括插值法和删除法,] 其中插值法用一个替代值弥补缺失值,而删除法则直接忽略缺失值。 在(这里,我们将考虑插值法)。
通过位置索引iloc,我们将data分成inputs和outputs, 其中前者为data的前两列,而后者为data的最后一列。 对于inputs中缺少的数值,我们用同一列的均值替换“NaN”项。
inputs, outputs = data.iloc[:, 0:2], data.iloc[:, 2] inputs = inputs.fillna(inputs.mean()) print(inputs)
fillna()会填充nan数据,返回填充后的结果。
mean()函数功能:求取均值
这里运行的时候报了个警告
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中的类别值或离散值,我们将“NaN”视为一个类别。] 由于“巷子类型”(“Alley”)列只接受两种类型的类别值“Pave”和“NaN”, pandas可以自动将此列转换为两列“Alley_Pave”和“Alley_nan”。 巷子类型为“Pave”的行会将“Alley_Pave”的值设置为1,“Alley_nan”的值设置为0。 缺少巷子类型的行会将“Alley_Pave”和“Alley_nan”分别设置为0和1。
inputs = pd.get_dummies(inputs, dummy_na=True) print(inputs)特征提取之pd.get_dummies()
cr:https://www.jianshu.com/p/5f8782bf15b1
pandas提供对one-hot编码的函数是:pd.get_dummies()
one-hot的基本思想:将离散型特征的每一种取值都看成一种状态,若你的这一特征中有N个不相同的取值,那么我们就可以将该特征抽象成N种不同的状态,one-hot编码保证了每一个取值只会使得一种状态处于“激活态”,也就是说这N种状态中只有一个状态位值为1,其他状态位都是0。
作者:轰鸣龙
链接:https://www.jianshu.com/p/5f8782bf15b1
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
[现在inputs和outputs中的所有条目都是数值类型,它们可以转换为张量格式。] 当数据采用张量格式后,可以通过在 :numref:sec_ndarray中引入的那些张量函数来进一步操作。
import torch X, y = torch.tensor(inputs.values), torch.tensor(outputs.values) X, y作业
data = pd.read_csv(data_file) nan_numer = data.isnull().sum(axis=0)



