|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码部分如下
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#导入数据
data=pd.read_csv('C:\\Users\\hasee\\Desktop\\machine learning\\machine-learning-ex2\\ex2\\ex2data1.txt',
names=['Exam1','Exam2','Admitted'])
#X与y的矩阵化,初始化thetas
data.insert(0,'ones',1)
X=np.array(data.iloc[:,:3])
y=np.array(data.Admitted).reshape(100,1)
thetas=np.zeros((3,1))
#定义函数
def sigmoid(x):
return 1/(1+np.exp(-x))
def costfunction(X,y,thetas):
a=np.log(sigmoid(np.dot(X,thetas)))
first=np.multiply(y,a)
second=np.multiply((1-y),np.log(1-sigmoid(X.dot(thetas))))
return np.sum(first+second)/(-100)
def gradient(X,y,thetas):
length = len(y)
s=np.empty((1,3))
for i in range(length):
s+=(sigmoid(X.dot(thetas)) - y)[i] * X[i]
s=s.reshape(3,)
return s/100
处理后的data长这样
ones Exam1 Exam2 Admitted
0 1 34.623660 78.024693 0
1 1 30.286711 43.894998 0
2 1 35.847409 72.902198 0
3 1 60.182599 86.308552 1
4 1 79.032736 75.344376 1
.. ... ... ... ...
95 1 83.489163 48.380286 1
96 1 42.261701 87.103851 1
97 1 99.315009 68.775409 1
98 1 55.340018 64.931938 1
99 1 74.775893 89.529813 1
在用scipy.optimize.fmin_tnc最小化costfunction这个函数的时候
输入代码为result = opt.fmin_tnc(func=costfunction, x0=thetas, fprime=gradient, args=(X, y))
报错说costfunction里有问题:
File "D:/Python/machine learning/ml 2.py", line 33, in costfunction
a=np.log(sigmoid(np.dot(X,thetas)))
File "<__array_function__ internals>", line 5, in dot
ValueError: shapes (3,) and (100,1) not aligned: 3 (dim 0) != 100 (dim 0)
可是X.shape为(100,3)thetas.shape为(3,1)
单独执行np.log(sigmoid(np.dot(X,thetas)))也是可以正常执行的
|
|