예제 #1
0
import math
import random
import _thread
from GradientDescent import gradientDescent, getTrainingDataSet, readFile

def f(x, theta):
	return theta[0] * x * x * x * x + theta[1] * x * x * x + theta[2] * x * x + theta[3] * x + theta[4]

arr = readFile()
for i in arr:
	plt.scatter(i[0], i[1])

features, target = getTrainingDataSet(arr)

theta = [random.uniform(-1.0, 1.0), random.uniform(-1.0, 1.0), random.uniform(-1.0, 1.0), random.uniform(-1.0, 1.0), random.uniform(-1.0, 1.0)]

newtheta = gradientDescent(theta, features, target)

print('theta:\n')
for i in newtheta:
	print(i, '\n')

x = np.arange(-5.0, 5.0, 0.01)
y = f(x, theta)
z = f(x, [0.0, 3.0, -4.0, 4.0, 2.0])
plt.plot(x, z, 'r')
plt.plot(x, y, 'b')
plt.xlabel('x')
plt.ylabel('y')

plt.show()
예제 #2
0
# Defining parameters for gradient descent
initalWeights = np.random.random([1,X.shape[1]])*0
maxIter = 1000
learningRate = 0.01


# Training a Least Squares Regression
weights = initalWeights
cost = []

for i in range(maxIter):

	yp = np.matmul(X_train,weights.T).ravel()
	J = computeL2Cost(Y_train, yp)
	G = computeGradient(X_train, Y_train, yp)
	weights = gradientDescent(weights, G, learningRate)
        
	if i%10 ==0:
		print("Cost of the model is {}".format(J))
	cost.append(J)

print("Weights after the training are : {}".format(weights))


# Plotting the training loss curve
plt.plot(range(0,len(cost)),cost)
plt.title('Cost per iterations')
plt.show()


# Computing the Residuals of the Training data
# Defining parameters for gradient descent
initalWeights = np.random.random([1, X.shape[1]])
maxIter = 1000
learningRate = 0.3

# Training a Logistic Regression
weights = initalWeights
cost = []

for i in range(maxIter):

    yp = logistic(weights, X_train)
    J = computeEntropyCost(Y_train, yp)
    G = computeGradient(X_train, Y_train, yp)
    weights = gradientDescent(weights, G, learningRate)

    if i % 10 == 0:
        print("Cost of the model is {}".format(J))
    cost.append(J)

print("Weights {} after the training are".format(weights))

# Plotting the training loss curve
plt.plot(range(0, len(cost)), cost)
plt.title('Cost per iterations')
plt.show()

# Prediction using the model
yp = logistic(weights, X_test)
yp = ((yp >= 0.5) * 1.0).ravel()
예제 #4
0
    for row in r:
        [x, y, z] = int(row[0]), int(row[1]), int(row[2])
        a.append([x, y])
        b.append(z)
'''Initializing values'''
X = matrix(a)  # creating the x-data matrix
y = transpose(matrix(b))  # creating the y-data matrix
theta = zeros((3, 1))  # initializing theta value
alpha = 0.1  # initializing learning rate
num_iter = 400  # initializing no. of iterations
print('Area         No. of rooms            Cost')
for i, j in zip(X, y):
    print('%d           %d          %d' % (i[0, 0], i[0, 1], j[0, 0]))
[X, mu, sigma] = featureNormalize(X)
X = insert(X, 0, 1, axis=1)
[theta, J_history] = gradientDescent(X, y, theta, alpha, num_iter)
'''Trying to predict the cost of house with sample area 1650 sq.m and 3 rooms'''
to_find = [1650, 3]
normalized = (to_find - mu) / sigma
normalized = insert(normalized, 0, 1, axis=1)
pcost = normalized * theta
print('#########Sample Prediction#########')
print('Price of house with area %d sq.m and %d rooms is ' %
      (to_find[0], to_find[1])),
print('$%0.2f' % pcost[0, 0])
print('###################################')
'''Predicting house prices using custom inputs'''
to_find = []
x = int(input('Enter area of house: '))
y = int(input('Enter number of rooms: '))
to_find = [x, y]