Andrew Ng的machine learning课程week2编程题python实现
来源:互联网 发布:centos vim插件 编辑:程序博客网 时间:2024/05/18 18:02
# -*- coding:utf-8 -*-import numpy as npimport matplotlib.pylab as pltfrom multiprocessing import Processdef plotData(X, y): plt.scatter(X, y, color='r', s=10, marker='x') plt.xlabel('Population of City in 10,000s') plt.ylabel('Profit in $10,000s') plt.show()def computerCost(X, y, theta): m = len(y) h = np.dot(X, theta) #X和theta是array类型,用点乘 square = np.square(h - y) J = 1/(2*m)*sum(square) return Jdef gradientDescent(X, y, theta, alpha, num_iters): m = len(y) J_history = np.zeros((num_iters,1)) for i in range(num_iters): theta = theta - alpha/m*(np.dot(X.T,(np.dot(X,theta))-y)) J_history = computerCost(X, y, theta) return theta, J_historyif __name__ == '__main__': #X为txt里第一列 X = np.loadtxt(fname='E:\python\ex1_multi\data\ex1data1.txt',dtype=float,delimiter=",",usecols=(0,)) #y为txt里第二列 y = np.loadtxt(fname='E:\python\ex1_multi\data\ex1data1.txt',dtype=float,delimiter=",",usecols=(1,)) m = len(X) #读取的数据为1*m,转换为m*1 X = X.reshape(m, 1) y = y.reshape(m, 1) #绘图 plotData(X, y) X = np.column_stack((np.ones((m,1),dtype=int), X)) #Add a column of ones to x theta = np.zeros((2,1)) iterations = 1500 alpha = 0.01 J = computerCost(X, y, theta) #Expected cost value (approx) 32.07 print(J) J = computerCost(X, y, [[-1],[2]]) #Expected cost value (approx) 54.24 print(J) theta = gradientDescent(X, y, theta, alpha, iterations)[0] print(theta)
阅读全文
0 0
- Andrew Ng的machine learning课程week2编程题python实现
- Andrew Ng的machine learning课程week3编程题python实现
- Machine Learning课程 by Andrew Ng
- andrew ng的machine learning第一次作业
- Machine Learning- overfitting (Andrew Ng 课程总结-过拟合)
- coursera andrew Ng老师的machine learning的课程总结(一)
- Andrew Ng机器学习课程11之使用machine learning的建议
- Andrew Ng-Machine learning (1)
- coursera Machine Learning, Andrew Ng
- Machine Learning-Andrew Ng-week3
- Machine Learning--Andrew Ng--week4
- Coursera—machine learning(Andrew Ng)第二周编程作业
- Coursera—machine learning(Andrew Ng)第三周编程作业
- Coursera—machine learning(Andrew Ng)第四周编程作业
- Coursera—machine learning(Andrew Ng)第五周编程作业
- Coursera—machine learning(Andrew Ng)第六周编程作业
- Coursera—machine learning(Andrew Ng)第七周编程作业
- Coursera—machine learning(Andrew Ng)第八周编程作业
- Java小白之路
- Linux内核学习经验总结
- 小波变换教程(十三)
- 设计模式-策略模式
- redisson-2.10.4源代码分析
- Andrew Ng的machine learning课程week2编程题python实现
- 工作中linux常用命令总结
- struts、hibernate、spring、jre version
- 小波变换教程(二十四 完)
- Spring框架学习——HelloWorld
- Apache
- 请使用一种算法,对字符串进行升序排列
- scheduleAtFixedRate与scheduleWithFixedDelay的区别
- python学习_4