Demo entry 6717239

1

   

Submitted by anonymous on Feb 23, 2018 at 22:08
Language: ABAP. Code size: 2.1 kB.

# -*- coding:utf-8 -*-
import numpy as np
from numpy import genfromtxt
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D


def get_data(data_set):
    m, n = np.shape(data_set)
    print(m,n)
    train_data = np.ones((m, n))
    train_data[:, 1:] = data_set[:, 1:]
    train_label = data_set[:, 0]
    return train_data, train_label

'''
def batchGradientDescent(x, y, theta, alpha, m):
    xTrains = x.transpose()
    for i in range(0, 50000):
        hypothesis = np.dot(x, theta)
        loss = hypothesis - y
        gradient = np.dot(xTrains, loss)/m
        theta = theta - alpha * gradient

        loss_trans = loss.transpose()
        cost_f = np.dot(loss_trans, loss)
        cost_f /= 2
        print(i)
        print(theta, cost_f)
    return theta
'''



def cost_f(x, y, theta):
        h = np.dot(x, theta)
        loss = h - y
        loss_trans = loss.transpose()
        cost_f = np.dot(loss_trans, loss)
        cost_f /= 2
        return cost_f


data_path = r"C:\Users\TIANLI\Desktop\Dataset-HW01-Part-01.csv"
data_set = genfromtxt(data_path, delimiter=',')

train_data, train_label = get_data(data_set)
'''
print(train_data[0])
print(train_label[0])
m, n = np.shape(train_data)
alpha = 0.002
theta = np.zeros(n)
print(theta)
theta = batchGradientDescent(train_data, train_label,theta, alpha, m)
print(theta)
'''
'''
train_label = data_set[:, 0]
train_data = data_set[:, 1]

x = np.arange(10, 50, 0.1)
y = x*theta[1] + theta[0]
plt.scatter(train_data, train_label, marker='.')
plt.plot(x, y, color='black')
plt.show()
'''





theta0 = np.arange(0, 500)
theta1 = np.arange(0, 500)
X, Y = np.meshgrid(theta0, theta1)


'''
print(theta[0])
theta_trans = theta.transpose()
print(theta_trans[0])
'''


c = np.zeros((500,500))
for i in range(0,500):
    for j in range(0,500):
         c[i, j]=cost_f(train_data, train_label, [i, j])
         print(c[i, j], i)

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, c, rstride=1, cstride=1, cmap='rainbow')
plt.show()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).