机器学习算法(二) 基于逻辑回归的分类预测(代码实现)

以下代码来自阿里云平台,本人只是在学习时为了自己看起来方便做了一些删减,还有少部分名称,注释,以及测试数据的改动。想学习完整部分的可以见https://tianchi.aliyun.com/specials/promotion/aicampml?invite_channel=1

## base func
import numpy as np 

## ply
import matplotlib.pyplot as plt
import seaborn as sns

## logistic
from sklearn.linear_model import LogisticRegression

# data
x_feature = np.array([[1,2], [2,2], [3,1], [-1,-1], [-3,-4], [-2,-2]])
y_label = np.array([0,0,0,1,1,1])

# model
lr_clf = LogisticRegression()

# put data to mdoel
lr_clf = lr_clf.fit(x_feature, y_label) #linear combination

## print weight w,w0
print('the weight w', lr_clf.coef_)
print('the weight w0', lr_clf.intercept_)

## model and boundary visualization
plt.figure()
plt.scatter(x_feature[:, 0], x_feature[:, 1], c=y_label, s=50, cmap='viridis')
plt.title('Dataset')

nx, ny = 200, 100
x_min, x_max = plt.xlim()
y_min, y_max = plt.ylim()
x_grid, y_grid = np.meshgrid(np.linspace(x_min, x_max, nx), np.linspace(y_min, y_max, ny))
z_proba = lr_clf.predict_proba(np.c_[x_grid.ravel(), y_grid.ravel()])
z_proba = z_proba[:, 1].reshape(x_grid.shape)
plt.contour(x_grid, y_grid, z_proba, [0.5], linewidths=2., colors='blue')

plt.show()

# new data visualization
plt.figure()

x_feature1 = np.array([[2, -2]])
plt.scatter(x_feature1[:, 0], x_feature1[:, 1], s=50, cmap='viridis')
plt.annotate(s='new point 1', xy=(2, -2), xytext=(1, -2), color = 'blue', 
	arrowprops=dict(arrowstyle='-|>', connectionstyle='arc3', color ='red'))

x_feature2 = np.array([[1, 1]])
plt.scatter(x_feature2[:, 0], x_feature2[:, 1], s=50, cmap='viridis')
plt.annotate(s='New point 2', xy=(1, 1), xytext=(1, 2), color='red',
	arrowprops=dict(arrowstyle='-|>',connectionstyle='arc3',color='red'))

## train data
plt.scatter(x_feature[:,0],x_feature[:,1], c=y_label, s=50, cmap='viridis')
plt.title('Dataset')

# boundary
plt.contour(x_grid, y_grid, z_proba, [0.5], linewidths=2., colors='blue')

plt.show()

# model predict
y_label1 = lr_clf.predict(x_feature1)
y_label2 = lr_clf.predict(x_feature2)

print('new point 1 predict class:\n', y_label1)
print('new point 2 predict class:\n', y_label2)

# because log regresion math is probability
y_label1_proba = lr_clf.predict_proba(x_feature1)
y_label2_proba = lr_clf.predict_proba(x_feature2)

print(y_label1_proba, y_label2_proba)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值