今日是代码对sigmoid函数的实现和运用
#linear_model线性回归
#名字虽然叫逻辑回归,作用于分类
#分类:类别
#回归:预测
from sklearn.linear_model import LogisticRegression
实现函数
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
return 1/(1+np.exp(-x))
x = np.linspace(-5,5,100)
y = sigmoid(x)
plt.plot(x,y,color='green')
损失函数
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,y=datasets.load_breast_cancer(return_X_y=True)
X=X[:,: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 = 0
for X_i,y_i in zip(X,y):
p = sigmoid(X_i,w1,w2,b)#概率
p=np.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_,color='green')
plt.subplot(2,2,2)
plt.plot(w1_space,loss1_,color='red')
逻辑回归代码实现
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_y=True)
cood = y!=2#过滤数据:类别是2,过滤掉
X=X[cood]
y=y[cood]
y
加载数据并拆分
#将调练数据测试数据:80% 训练数据,保留20%,测试数据
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.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(axis=1)
概率手动计算
def sigmoid(x):
return 1/(1+np.exp(-z))
#方程系数和截距
w=model.coef_
b=model.intercept_
#求解线性方程
z=X_test.dot(w.reshape(-1))+b
p=sigmoid(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)