Я пытаюсь создать нейронную сеть, которая прогнозирует цену дома на основе количества спален и ванных комнат в доме. Ниже приведен файл "realestate.csv".
beds baths price
0 2 1 59222
1 3 1 68212
2 2 1 68880
3 2 1 69307
4 2 1 81900
.. ... ... ...
980 4 3 232425
981 3 2 234000
982 3 2 235000
983 4 2 235301
984 3 2 235738
[985 rows x 3 columns]
Ниже мой код в python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
train_bed_bath = np.array(pd.read_csv('realestate.csv',usecols=['beds', 'baths']), dtype='int')[0:984]
train_price = np.array(pd.read_csv('realestate.csv',usecols=['price']),dtype='int')[0:984]
test_bed_bath = np.array(pd.read_csv('realestate.csv',usecols=['beds', 'baths']), dtype='int')[984:985]
predicted_outputs = []
class Neural_Network(object):
def __init__(self):
#parameters
self.inputSize = 2
self.hiddenSize = 3
self.outputSize = 1
#weights
self.W1 = np.random.randn(2, 3) # (3x2) weight matrix from input to hidden layer
self.W2 = np.random.randn(self.hiddenSize, self.outputSize) # (3x1) weight matrix from hidden to output layer
def forward(self, X):
#forward propagation through our network
self.z = np.dot(X, self.W1) # dot product of X (input) and first set of 3x2 weights
self.z2 = self.sigmoid(self.z) # activation function
self.z3 = np.dot(self.z2, self.W2) # dot product of hidden layer (z2) and second set of 3x1 weights
self.o = self.sigmoid(self.z3) # final activation function
return self.o
def sigmoid(self, s):
# activation function
return .5 * (1 + np.tanh(.5 * s))
def sigmoidPrime(self, s):
#derivative of sigmoid
return s * (1 - s)
def backward(self, X, y, o):
# backward propagate through the network
self.o_error = y - o # error in output
self.o_delta = self.o_error*self.sigmoidPrime(o) # applying derivative of sigmoid to error
self.z2_error = self.o_delta.dot(self.W2.T) # z2 error: how much our hidden layer weights contributed to error
self.z2_delta = self.z2_error*self.sigmoidPrime(self.z2) # applying derivative of sigmoid to z2 error
self.W1 += X.T.dot(self.z2_delta) # adjusting first set (input --> hidden) weights
self.W2 += self.z2.T.dot(self.o_delta) # adjusting second set (hidden --> output) weights
def train(self, X, y):
self.o = self.forward(X)
self.backward(X, y, self.o)
NN = Neural_Network()
for i in range(1000):
NN.train(train_bed_bath, train_price)
for i in range(5):
for j in range (5):
print(NN.forward([i,j]))
Он выводит следующий вывод
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
[1.]
Я не уверен, почему он это делает , Любая помощь будет принята с благодарностью.