我想要使用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)
我正在
"不能将序列乘以非int类型' float'"
错误,即使我将数据帧转换为numpy数组。
感谢任何帮助:/
答案 0 :(得分:1)
错误消息可能有点误导。这是因为您的DataFrame包含dtype object
列,在您的情况下包含日期和时间列。转换为numpy ndarray
并不会有太大帮助,因为数据类型不会改变。您需要先将这些列转换为int或float值,然后才能使用np.dot()
。