去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
// An highlighted block
#使用python的线性回归算法
import numpy as np
import matplotlib.pyplot as plt
#解决绘图过程中的中文乱码问题
plt.rcParams['font.sans-serif']=['SimHei']
#可以让数轴有负号
plt.rcParams['axes.unicode_minus']=False
train_data=np.loadtxt(r'LoR_linear.txt',delimiter=',')
test_data=np.loadtxt(r'LoR_nonlinear.txt',delimiter=',')
train_X,train_y=train_data[:,:-1],train_data[:,-1]
test_X,test_y=test_data[:,:-1],test_data[:,-1]
#生成一个随机的种子,将原来的文档中的内容打乱顺序,是的训练集数据训练的更加精确
np.random.seed(1)
order=np.random.permutation(len(train_data))
train_data=train_data[order]
test_data=test_data[order]
def preProess(X,y):
#对数据的特征缩放
X -=np.mean(X,axis=0)
X /=np.std(X,axis=0,ddof=1)
X=np.c_[np.ones(len(X)),X]#给X拼接一个一矩阵
y=np.c_[y]#将y转化为列矩阵
return X,y
train_X,train_y=preProess(train_X,train_y)
test_X,test_y=preProess(test_X,test_y)
def g(x):
return 1/(1+np.exp(-x))
#
x=np.linspace(-10,10,500)
y=g(x)
plt.plot(x,y)
plt.show()
#定义模型函数
def model(X,theta):
z=np.dot(X,theta)
h=g(z)
return h
#定义代价函数
def costFunc(h,y):
m=len(y)
J=-(1.0/m)*np.sum(y*np.log(h)+(1-y)*np.log(1-h))
return J
#梯度下降函数
def gradDesc(X,y,max_iter=15000,alpha=0.1):
m,n=X.shape
theta=np.zeros((n,1))
J_history=np.zeros(max_iter)
for i in range(max_iter):
h=model(X,theta)
J_history[i]=costFunc(h,y)#将所得的值存
deltaTheta = (1.0/m)*np.dot(X.T,h-y)
theta -= deltaTheta*alpha
return J_history,theta
def score(h,y):
m=len(y)
count=0
for i in range(m):
h[i]=np.where(h[i]>=0.5,1,0)
if h[i]==y[i]:
count+=1
return count/m
#预测结果函数,结果不是0就是1
def predict(h):
y_pre=[1 if i>=0.5 else 0 for i in h]
return y_pre
print(train_X.shape,train_y.shape)
J_history,theta=gradDesc(train_X,train_y)
print(theta)
plt.title("代价函数")
plt.plot(J_history)
plt.show()
train_h=model(train_X,theta)
test_h=model(test_X,theta)
print(train_h,test_h)
def showDivide(X,theta,y,title):
plt.title(title)
plt.scatter(X[y[:,0]==0,1],X[y[:,0]==0,2],label="负样本")
plt.scatter(X[y[:,0]==1,1],X[y[:,0]==1,2],label="正样本")
min_x1,max_x1=np.min(X),np.max(X)
min_x2,max_x2=-(theta[0]+theta[1]*min_x1)/theta[2],-(theta[0]+theta[1]*max_x1)/theta[2]
plt.plot([min_x1,max_x1],[min_x2,max_x2])
plt.legend()
plt.show()
showDivide(train_X,theta,train_y,'训练集')
showDivide(test_X,theta,test_y,'测试集集')
train_y1=predict(train_h)
print('预测的结果是:',train_y1)