无法将序列乘以 'float' 类型的非整数 - numpy

Cant multiply sequence by non-int of type 'float' - numpy

我遇到了一个问题,我不确定自己做错了什么。我正在自学神经网络和 numpy。我已经实现了以下代码,但是每当我 运行,控制台状态 “不能将序列乘以 'float' 类型的非整数”。

有什么想法吗?

import pandas as pd
import numpy as np


df = pd.read_csv ('example.csv')
df.to_csv("example.csv", header=None, index=False)

trainingdata = df.sample(frac = 0.7)
testingdata = df.drop(trainingdata.index)

training_output = np.array([[0, 1, 1, 0]]).T


def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):
    return x * (1 - x)

np.random.seed(1)

neural_weights = 2 * np.random.random((31, 1)) - 1

for iteration in range(100000):

    input_layer = trainingdata

    outputs = sigmoid(np.dot(input_layer, neural_weights))

    error = training_output - outputs

    adjustments = error * sigmoid_derivative(outputs)

    neural_weights += np.dot(input_layer.T, adjustments)

print(outputs)
print(neural_weights)

你的数据是一个list,在python中没有定义list * float操作(唯一定义的是list * int,创建列表的副本,而不是你想要的,它是将列表的每个元素独立地乘以int/float的值)。你想要的是将它转换为 numpy 数组,其中 array * flat 是一个定义明确的操作。

[1, 2, 3] * 1.2            # error
[1, 2, 3] * 2              # [1, 2, 3, 1, 2, 3]
np.array([1, 2, 3]) * 1.2  # np.array([1.2, 2.4, 3.6])
np.array([1, 2, 3]) * 2    # np.array([2, 4, 6])