第二章

2.2.2

# 处理缺失值
inputs, outputs = data.iloc[:, 0:2], data.iloc[:, 2]  # input处理第一二列数据  ,Output处理3列
inputs = inputs.fillna(inputs.mean())
print(inputs)

1)遇到报错:TypeError: can only concatenate str (not “int”) to str

解决方法:mean()函数中添加 numeric_only=true

2.2.3

# 转换为张量格式
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])
)

2)遇到报错: can't convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.

解决方法:添加 .astype(float) 进行转化

X, y = torch.Tensor(inputs.values.astype(float)), torch.tensor(outputs.values.astype(float))

2.2.5 作业

删除缺失值最多的列。

# data 是通过 pandas.read_csv() 读入的数据
data = data.drop(columns=data.isna().sum(axis=0).idxmax())  # axis=0表示缺失值最多的“列”

2.3.6

3)使用mean()函数报错:mean(): could not infer output dtype. Input dtype must be either a floating point or complex dtype. Got: Long

解决方法:使用.type(torch.float64)

原因分析:input----输入张量。它的数据类型必须是浮点型或复数型。

Z = A.type(torch.float64)
print(Z.mean())

4)使用点积报错:RuntimeError: dot : expected both vectors to have same dtype, but found Long and Float

解决方法:类型转化-----z=z.float()  

原因分析:输出y和z的类型不一样,一个为float型、一个不是

y = torch.ones(4, dtype=torch.float32)
z=z.float()
print(torch.dot(z, y))

2.3

问题:无法下载d2l安装包

解决方法:在Anaconda Prompt中输入命令:pip install d2l  【注意:不要加上版本号!】

2.4

问题:矢量图显示函数use_svg_display()内部报错

解决方法:将  display.set_matplotlib_formats('svg')删除,更改为

display.display_svg()

注:%matplotlib inline 是内置函数,不需要写入 pyCharm,写入会报错

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐