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

Time:02-22

I've encountered an issue and im not sure what im doing wrong. im teaching myself about neural networks and numpy. I've got the below code all implemented, but whenever I run, the console states "Cant multiply sequence by non-int of type 'float'".

Any ideas?

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)

CodePudding user response:

Your data is a list, and in python there is no list * float operation defined (the only one defined is list * int, which creates copies of the list, rather than what you want which is multiplying each element of the list independently by the value of the int/float). What you want is to convert it to numpy array, where array * flat is a well defined operation.

[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])
  • Related