学校网站建设方面汇报,备案时网站名称怎么写,常用的软件开发平台,小程序制作平台开发今日是代码对sigmoid函数的实现和运用
#linear_model线性回归
#名字虽然叫逻辑回归#xff0c;作用于分类
#分类#xff1a;类别
#回归#xff1a;预测
from sklearn.linear_model import LogisticRegression 实现函数
import numpy as np
import matplotlib.pyplot as pl…今日是代码对sigmoid函数的实现和运用
#linear_model线性回归
#名字虽然叫逻辑回归作用于分类
#分类类别
#回归预测
from sklearn.linear_model import LogisticRegression 实现函数
import numpy as np
import matplotlib.pyplot as pltdef sigmoid(x):return 1/(1np.exp(-x))x np.linspace(-5,5,100)y sigmoid(x)plt.plot(x,y,colorgreen) 损失函数
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#Z-score归一化
from sklearn.preprocessing import scale,StandardScaler
加载数据
X,ydatasets.load_breast_cancer(return_X_yTrue)XX[:,:2]#切片两个特征
display(X.shape)
display(y.shape) 建模
model LogisticRegression()#训练和之前线性回归类似
#后面其他方法算法类似
model.fit(X,y) 逻辑回归线性方程拿出来系数
w1 model.coef_[0,0]
w2 model.coef_[0,1]
b model.intercept_
print(方程系数,w1,w2)
print(截距,b) sigmoid函数
def sigmoid(X,w1,w2,b):z w1*X[0] w2*X[1] b#方程表示return 1/(1 np.exp(-z))
损失函数 def loss_function(X,y,w1,w2,b):loss 0for X_i,y_i in zip(X,y):p sigmoid(X_i,w1,w2,b)#概率pnp.clip(p,0.0001,0.999)#裁剪loss -y_i * np.log(p) (1-y_i)* np.log(1-p)return loss
定义参数w1,w2取值空间
w1_space np.linspace (w1 - 2,w1 2,100)w2_space np.linspace(w2 - 2,w2 2,100)
损失计算
loss1_ np.array([loss_function(X,y,i,w2,b) for i in w1_space])
loss1_ loss2_ np.array([loss_function(X,y,w1,i,b) for i in w2_space])
loss2_ 可视化¶
fig1 plt.figure(figsize(12,9))plt.subplot(2,2,1)
plt.plot(w1_space,loss1_,colorgreen)plt.subplot(2,2,2)
plt.plot(w1_space,loss1_,colorred) 逻辑回归代码实现
import numpy as np
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
#将数据拆分
from sklearn.model_selection import train_test_split
X,y datasets.load_iris(return_X_yTrue)
cood y!2#过滤数据类别是2过滤掉
XX[cood]
yy[cood]
y 加载数据并拆分
#将调练数据测试数据80% 训练数据保留20%测试数据
X_train,X_test,y_train,y_test train_test_split(X,y,test_size0.2)display(X_train.shape,X_test.shape)
display(y_train.shape,y_test.shape) 训练
model LogisticRegression()model.fit(X_train,y_train)y_pred model.predict(X_test)
print(预测结果是:,y_pred)proba_ model.predict_proba(X_test)
print(预测概率是:\n,proba_) y_pred proba_.argmax(axis1) 概率手动计算
def sigmoid(x):return 1/(1np.exp(-z))#方程系数和截距
wmodel.coef_
bmodel.intercept_#求解线性方程
zX_test.dot(w.reshape(-1))bpsigmoid(z)#列合并
#np.column_stack([1-p,p])
np.concatenate([(1-p).reshape(-1,1),p.reshape(-1,1)],axis 1)[:5] model.predict_proba(X_test)