04-《机器学习及实战》系数截距的绘图体验

# -*- coding: UTF-8 -*-
import matplotlib.pyplot as plt
import pandas as pd
df_train = pd.read_csv('D:\\Python2713\\Datasets\\Breast-Cancer\\breast-cancer-train.csv')
df_test = pd.read_csv('D:\\Python2713\\Datasets\\Breast-Cancer\\breast-cancer-test.csv')
df_test_negative=df_test.loc[df_test['Type'] == 0][['Clump Thickness','Cell Size']]
df_test_positive=df_test.loc[df_test['Type'] == 1][['Clump Thickness','Cell Size']]
plt.scatter(df_test_negative['Clump Thickness'],df_test_negative['Cell Size'],marker='o',s=200,c='red')
plt.scatter(df_test_positive['Clump Thickness'],df_test_positive['Cell Size'],marker='x',s=150,c='black')
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
plt.show()
import numpy as np
intercept=np.random.random([1])
print "np.random.random([1]) %s" %np.random.random()
print "intercept :%s" %intercept
coef=np.random.random([2])
print "coef :%s" %coef
lx = np.arange(0,12)
print "lx :%s" %lx

ly = (-intercept - lx * coef[0]) / coef[1]
print "ly :%s" %ly
plt.plot(lx,ly,c='yellow')
plt.show()
plt.scatter(df_test_negative['Clump Thickness'],df_test_negative['Cell Size'],marker='o',s=200,c='red')
plt.scatter(df_test_positive['Clump Thickness'],df_test_positive['Cell Size'],marker='x',s=150,c='black')
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
plt.show()


from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
lr.fit(df_train[['Clump Thickness','Cell Size']][:10],df_train['Type'][:10])
print lr.score(df_test[['Clump Thickness','Cell Size']],df_test['Type'])
print '-----------------------------------'
print 'lr.intercept_:%s' %lr.intercept_
intercept = lr.intercept_
print 'lr.coef_[0,:] %s'%lr.coef_[0,:]
coef = lr.coef_[0,:]
ly=(-intercept-lx * coef[0])/coef[1]
print ly

plt.plot(lx,ly,c='green')
plt.scatter(df_test_negative['Clump Thickness'],df_test_negative['Cell Size'],marker='o',s=200,c='red')
plt.scatter(df_test_positive['Clump Thickness'],df_test_positive['Cell Size'],marker='x',s=150,c='black')
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
plt.show()


print '--=====----____+++++--'
lr = LogisticRegression()
#使用所有训练样本学习直线的截距和系数
lr.fit(df_train[['Clump Thickness','Cell Size']],df_train['Type'])
print lr.score(df_test[['Clump Thickness','Cell Size']],df_test['Type'])
print '-----------------------------------'
print 'lr.intercept_:%s' %lr.intercept_

intercept = lr.intercept_
print 'lr.coef_[0,:] %s'%lr.coef_[0,:]
coef = lr.coef_[0,:]
ly=(-intercept-lx * coef[0])/coef[1]
print ly

plt.plot(lx,ly,c='blue')
plt.scatter(df_test_negative['Clump Thickness'],df_test_negative['Cell Size'],marker='o',s=200,c='red')
plt.scatter(df_test_positive['Clump Thickness'],df_test_positive['Cell Size'],marker='x',s=150,c='black')
plt.xlabel('Clump Thickness')
plt.ylabel('Cell Size')
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千码君2016

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值