【问题标题】:Neural Network - TypeError: can't multiply sequence by non-int of type 'float'神经网络 - 类型错误:不能将序列乘以“浮点”类型的非整数
【发布时间】:2017-09-11 14:23:02
【问题描述】:

我想使用 numpy 和 pandas 作为我的依赖项创建的神经网络有问题。网络应该根据日期、时间、纬度和经度作为特征来预测地震的震级。这是来自数据集的 sn-p:

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)

我来了

“不能将序列乘以'float'类型的非整数”

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

任何帮助表示赞赏:/

【问题讨论】:

  • 该错误可能会告诉您它在代码中的确切位置。为什么对我们隐瞒这个?此外,这是一个常见的 python 错误,谷歌搜索会告诉你在哪些情况下会发生这种错误。结合这两个提示应该可以帮助您解决这个问题。

标签: python pandas numpy machine-learning neural-network


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-05-13
    • 2011-04-06
    • 2012-09-17
    • 2018-12-19
    • 1970-01-01
    • 2013-09-11
    • 2015-06-27
    相关资源
    最近更新 更多