fork download
  1. # # __author__ = 'Sony'
  2. import sys
  3. import scipy.optimize, scipy.special
  4. from numpy import *
  5. from matplotlib import pyplot, cm
  6. from mpl_toolkits.mplot3d import Axes3D
  7.  
  8. # EX_DIRECTORY_PATH = 'C:\Users\Sony\OneDrive\#my lab\NeuralNets'
  9.  
  10. def plot_samples(data):
  11. X, y = data[:, 0], data[:, 1]
  12. pyplot.xlabel("X - axis")
  13. pyplot.ylabel("Y - axis")
  14. pyplot.xlim([0, 51])
  15. pyplot.ylim([0, 51])
  16.  
  17. pyplot.scatter( X, y, c='b', marker='o', s=40, linewidths=1, label="input samples" )
  18. pyplot.legend()
  19. pyplot.show()
  20. pyplot.close()
  21.  
  22. def cost( theta, data ):
  23. X, y = data[:, 0], data[:, 1]
  24. m = shape(X)[0]
  25. y = y.reshape(m, 1)
  26. X = c_[ones((m, 1)), X]
  27.  
  28. J = X.dot(theta) - y
  29. J = J.T.dot(J) / (m)
  30. # print(J[0, 0])
  31. return J
  32.  
  33. def gradDesc(theta, data):
  34. X = data[:, 0]
  35. y = data[:, 1]
  36. m = shape(X)[0]
  37. X = c_[ones((m, 1)), X]
  38. y = y.reshape(m, 1)
  39. hypo = X.dot(theta)
  40. grad = X.T.dot(hypo - y)/m
  41. # print(grad)
  42. return grad
  43.  
  44. def run(theta, data ):
  45. result = scipy.optimize.fmin_cg( f = cost, fprime=gradDesc, x0=theta, \
  46. args = (data), maxiter=50, disp=False, full_output=True )
  47. theta = result[0]
  48. minCost = result[1]
  49. return theta, minCost
  50.  
  51. def main():
  52. data = genfromtxt('in.txt', delimiter=',')
  53. theta = zeros((2, 1))
  54. # plot_samples(data)
  55. run(theta, data)
  56. # gradDesc(theta, data)
  57. if __name__ == '__main__':
  58. main()
  59.  
  60.  
Runtime error #stdin #stdout #stderr 0.48s 45440KB
stdin
5,5
10,15
44,44
24,20
2,2
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 5, in <module>
  File "/usr/lib/python3/dist-packages/matplotlib/__init__.py", line 1102, in <module>
    rcParams = rc_params()
  File "/usr/lib/python3/dist-packages/matplotlib/__init__.py", line 949, in rc_params
    fname = matplotlib_fname()
  File "/usr/lib/python3/dist-packages/matplotlib/__init__.py", line 770, in matplotlib_fname
    configdir = _get_configdir()
  File "/usr/lib/python3/dist-packages/matplotlib/__init__.py", line 635, in _get_configdir
    return _get_config_or_cache_dir(_get_xdg_config_dir())
  File "/usr/lib/python3/dist-packages/matplotlib/__init__.py", line 612, in _get_config_or_cache_dir
    return _create_tmp_config_dir()
  File "/usr/lib/python3/dist-packages/matplotlib/__init__.py", line 544, in _create_tmp_config_dir
    tempdir = os.path.join(tempdir, 'matplotlib-%s' % getpass.getuser())
  File "/usr/lib/python3.4/getpass.py", line 170, in getuser
    return pwd.getpwuid(os.getuid())[0]
KeyError: 'getpwuid(): uid not found: 20085'