python 线性回归编码

一,使用 scikit-learn 编码练习

 

# coding=utf-8
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats

# import scikit-learn  as sl
'''
数据分布
    均匀分布
    正态分布 高斯分布 直方图  散点图
线性回归: R平方 评估相关度  ,是否适合线性回归模型
         

'''
# plt.ion()

'''
distribution_arr = np.random.uniform(0.0, 5.0, 250)
plt.hist(distribution_arr, 5)  # 数据分布,个数分布范围
plt.show()

normal_arr = np.random.normal(5.0, 1.0, 1000)
plt.hist(normal_arr, 100)
plt.show()

'''

'''
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
plt.scatter(x, y)
plt.show()

'''
# x = np.random.normal(5.0, 1.0, 1000)
# y = np.random.normal(10.0, 2.0, 1000)

x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
slope, intercept, r, p, std_err = stats.linregress(x, y) # 调用了线性回归函数


def myfunc(x):
    return slope * x + intercept


mymodel = list(map(myfunc, x))
plt.scatter(x, y)
plt.plot(x, mymodel)
plt.show()

 

posted @ 2022-06-09 19:48  gaussen126  阅读(30)  评论(0编辑  收藏  举报