Python+sklearn使用线性回归算法预测儿童身高

Python+sklearn使用线性回归算法预测儿童身高

In [1]:

import copy
import numpy as np
from sklearn import linear_model

def linearRegressionPredict(x, y):
    lr = linear_model.LinearRegression()
    # 拟合
    lr.fit(x, y)
    return lr

# 儿童年龄,性别(0女1男),父亲身高,母亲身高,祖父身高,祖母身高,外祖父身高,外祖母身高
x = np.array([[1, 0, 180, 165, 175, 165, 170, 165],\
              [3, 0, 180, 165, 175, 165, 173, 165],\
              [4, 0, 180, 165, 175, 165, 170, 165],\
              [6, 0, 180, 165, 175, 165, 170, 165],\
              [8, 1, 180, 165, 175, 167, 170, 165],\
              [10, 0, 180, 166, 175, 165, 170, 165],\
              [11, 0, 180, 165, 175, 165, 170, 165],\
              [12, 0, 180, 165, 175, 165, 170, 165],\
              [13, 1, 180, 165, 175, 165, 170, 165],\
              [14, 0, 180, 165, 175, 165, 170, 165],\
              [17, 0, 170, 165, 175, 165, 170, 165]])
# 儿童身高,单位:cm
y = np.array([60, 90, 100, 110,\
              130, 140, 150, 164,\
              160, 163, 168])
# 根据已知数据拟合最佳直线的系数和截距
lr = linearRegressionPredict(x, y)
# 查看最佳拟合系数
print('k:', lr.coef_)
k: [ 8.03076923e+00 -4.65384615e+00  2.87769231e+00 -5.61538462e-01
  1.33226763e-15  5.07692308e+00  1.88461538e+00  0.00000000e+00]

In [2]:

# 截距
print('b:', lr.intercept_)
b: -1523.1538461538462

In [3]:

import matplotlib.pyplot as plt

In [23]:

x.take(0,axis =1)

Out[23]:

array([ 1,  3,  4,  6,  8, 10, 11, 12, 13, 14, 17])

In [24]:

plt.plot(x.take(0,axis =1),y)

Out[24]:

[<matplotlib.lines.Line2D at 0x22628ff1208>]

181429_gtkA_2862061.png

In [23]:

# 预测
xs = np.array([[10, 0, 180, 165, 175, 165, 170, 165],\
              [17, 1, 173, 153, 175, 161, 170, 161],\
              [34, 0, 170, 165, 170, 165, 170, 165]])
for item in xs:
    # 深复制,假设超过18岁以后就不再长高了
    item1 = copy.deepcopy(item)
    if item1[0] > 18:
        item1[0] = 18
    print(item, ':', lr.predict(item1.reshape(1,-1)))
[ 10   0 180 165 175 165 170 165] : [140.56153846]
[ 17   1 173 153 175 161 170 161] : [158.41]
[ 34   0 170 165 170 165 170 165] : [176.03076923]

In [35]:

x[:][0]

Out[35]:

array([  1,   0, 180, 165, 175, 165, 170, 165])

转载于:https://my.oschina.net/dwqdwd/blog/1820101

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值