实现如下,并获得类似于octiva的结果:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
filepath =('C:/Pythontry/MachineLearning/dataset/couresra/ex2data1.txt')
data =pd.read_csv(filepath,sep=',',header=None)
X = data.values[:,:2]
y = data.values[:,2:3]
postive_value = data.loc[data[2] == 1]
negative_value = data.loc[data[2] == 0]
ax1 = postive_value.plot(kind='scatter',x=0,y=1,s=50,color='b',marker="+",label="Admitted")
ax2 = negative_value.plot(kind='scatter',x=0,y=1,s=50,color='y',ax=ax1,label="Not Admitted")
ax1.set_xlabel("Exam 1 score")
ax2.set_ylabel("Exam 2 score")
plt.show()
[m,n] = np.shape(X)
print(m,n)
additional_coulmn = np.ones((m,1))
X = np.append(additional_coulmn,X,axis=1)
initial_theta = np.zeros((n+1), dtype=int)
print(initial_theta)
def sigmoid(z):
g = np.zeros(np.shape(z));
g = 1/(1+np.exp(-z));
return g
def costFunction(theta, X, y):
J = 0;
receive_theta = np.array(theta)[np.newaxis]
theta = np.transpose(receive_theta)
z = np.dot(X,theta)
h = sigmoid(z)
J = np.sum(np.dot((-y.T),np.log(h))-np.dot((1-y).T,np.log(1-h)))/m
grad =np.dot(X.T,(h-y))/m
return J,grad
[cost, grad] = costFunction(initial_theta, X, y)
print('Cost at initial theta (zeros):', cost)
print('Expected cost (approx): 0.693\n')
print('Gradient at initial theta (zeros): \n',grad)
print('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n')
In6
test_theta = [-24, 0.2, 0.2]
[cost, grad] = costFunction(test_theta, X, y)
print('\nCost at test theta: \n', cost)
print('Expected cost (approx): 0.218\n')
print('Gradient at test theta: \n',grad);
print('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n')
import scipy.optimize as opt
print ('Executing minimize function...\n')
result = opt.fmin_tnc(func=costFunction, x0=initial_theta, args=(X, y))
print('Thetas found by fmin_tnc function: ', result);
print('Cost at theta found : \n', cost);
print('Expected cost (approx): 0.203\n');
print('theta: \n',result[0]);
print('Expected theta (approx):\n');
print(' -25.161\n 0.206\n 0.201\n');
输出:
正在执行最小化函数...
fmin_tnc函数找到的theta值为:(array([-25.16131854, 0.20623159, 0.20147149]), 36, 0)
找到theta时的代价为:
0.218330193827
期望的代价(近似值)为:0.203
theta值为:
[-25.16131854 0.20623159 0.20147149]
期望的theta值(近似值)为:
-25.161
0.206
0.201
fminunc
使用某种形式的BFGS(找不到更权威的参考文献)。因此,scipy.optimize.fmin_bfgs
看起来是与fminunc
最接近的并行方法。 - alecbz