gpt4 book ai didi

python - 神经网络 - 类型错误 : can't multiply sequence by non-int of type 'float'

转载 作者:行者123 更新时间:2023-11-30 09:52:08 28 4
gpt4 key购买 nike

我在使用 numpy 和 pandas 作为依赖项创建神经网络时遇到问题。网络应该以日期、时间、纬度和经度为特征来预测地震的强度。以下是数据集的片段:

Date    Time    Latitude    Longitude   Magnitude
0 01/02/1965 13:44:18 19.246 145.616 6.0
1 01/04/1965 11:29:49 1.863 127.352 5.8
2 01/05/1965 18:05:58 -20.579 -173.972 6.2
3 01/08/1965 18:49:43 -59.076 -23.557 5.8
4 01/09/1965 13:32:50 11.938 126.427 5.8

这是代码:

import pandas as pd
import numpy as np

data = pd.read_csv("C:/Users/Kamalov/AppData/Local/Programs/Python/Python35/my_code/datasets/database.csv")
train, test = data[:15000], data[15000:]
trainX, trainY = train[["Date","Time","Latitude","Longitude"]], train['Magnitude']
testX, testY = test[["Date","Time","Latitude","Longitude"]], test['Magnitude']

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

def sigmoid_output_to_derivative(output):
return output*(1-output)

synapse_0 = 2*np.random.random((4,1)) - 1
synapse_1 = 2*np.random.random((1,4)) - 1

X = trainX.values
y = trainY.values


for iter in range(50000):
# forward propagation
layer_0 = X
layer_1 = sigmoid(np.dot(layer_0,synapse_0))

layer_2 = sigmoid(np.dot(layer_1,synapse_1))

# how much did we miss?
layer_2_error = layer_2 - y

# multiply how much we missed by the
# slope of the sigmoid at the values in l1
layer_2_delta = layer_2_error * sigmoid_output_to_derivative(layer_2)
synapse_0_derivative = np.dot(layer_0.T,layer_2_delta)

# update weights
synapse_0 -= synapse_0_derivative

print ("Output After Training:")
print (layer_2)

我明白了

"can't multiply sequence by non-int of type 'float'"

错误,即使我将数据帧转换为 numpy 数组。

感谢任何帮助:/

最佳答案

错误消息可能有点误导。这是因为您的 DataFrame 包含 dtype object 的列,在您的情况下是日期和时间列。转换为 numpy ndarray 并没有多大帮助,因为数据类型不会改变。您需要先将这些列转换为 int 或 float 值,然后才能使用 np.dot()

关于python - 神经网络 - 类型错误 : can't multiply sequence by non-int of type 'float' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43426544/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com