从csv文件准备数据以与神经网络python一起使用

时间:2017-02-22 19:43:13

标签: python csv pandas numpy neural-network

我正在努力让这个基本的神经网络工作,因为我对此全新。我可以使用硬编码输入/输出,但似乎无法从csv文件中获取动态数据。我以前用几种不同的方式问过这个问题并且每次都接近一点但是不能通过这个错误..

我得到的错误 -

adjustment = dot(training_set_inputs.T, error * self.__sigmoid_derivative(output)) ValueError: shapes (2736,3) and (2736,1) not aligned: 1 (dim 2) != 2736 (dim 1)

如何重塑它以使输入和输出为(2736,3)?我很确定问题出在' train'因为它正在重塑

 output = self.predict(training_set_inputs)

这是我的数据准备 -

Model | Trim | Cost | Success
6     | 102  | 1200 | 1
8     | 105  | 1500 | 1
15    | 110  | 3000 | 0


df = pd.read_csv('fulldata.csv')
y = np.array(df[['Success']])
y.reshape(y.size, 1)

df.drop(['Success'],1 , inplace=True)

t_in = df.values.tolist()

这是网络

class NeuralNetwork():
    def __init__(self):
        # Seed the random number generator
        random.seed(1)
        self.synaptic_weights = 2 * random.random((3,1)) - 1

    def __sigmoid(self, x):
        return 1 /(1 + exp(-x))

    def __sigmoid_derivative(self, x):
        return x * (1-x)

    def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):
        for iteration in xrange(number_of_training_iterations):
            output = self.predict(training_set_inputs)

            error = training_set_outputs - output

            adjustment = dot(training_set_inputs, error * self.__sigmoid_derivative(output))

            self.synaptic_weights += adjustment


    def predict(self, inputs):
        return self.__sigmoid(dot(inputs, self.synaptic_weights))


if __name__=="__main__":

    #initialize a single neuron neural network
    neural_network = NeuralNetwork()

    # training data inputs / outputs
    training_set_inputs = t_in
    training_set_outputs = y 
    # number of iterations 
    neural_network.train(training_set_inputs, training_set_outputs, 10000)

    # new input to predict
    my_input = array([6,102,3000])

    print 'New synaptic weights after training'
    print neural_network.synaptic_weights

    print 'Predicting'

    my_prediction =  neural_network.predict(my_input)
    print (my_prediction)

从输入(t_in)和输出(y) -

输出什么
t_in = [[6.0, 102.0, 0.0], [61.0, 138.0, 12414.0], [224.0, 291.0, 30309.0]]...

y = [[ 1.]
 [ 1.]
 [ 1.]
 ..., 
 [ 0.]
 [ 0.]
 [ 0.]]

0 个答案:

没有答案