比较样本方差对线性拟合结果的影响

代码和结果如下:

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
import seaborn as sns

def self_func(i):
    
    np.random.seed(i)
    n = 101
    x = np.linspace(0,10,n)
    noise = np.random.randn(n)
    
    y1 = 2.5 * x + 0.8 + 20.0 * noise
    y2 = 2.5 * x + 0.8 + 2.0 * noise
    
    lr1 = LinearRegression()
    lr1.fit(x.reshape(-1,1),y1)
    
    lr2 = LinearRegression()
    lr2.fit(x.reshape(-1,1),y2)

    return pd.DataFrame([[0,float(lr1.coef_)],[1,float(lr2.coef_)]],columns=['cate','coef_'])

result = self_func(0)
for i in range(100):
    df = self_func(i)
    result = result.append(df,ignore_index=True)

sns.stripplot(x="cate", y="coef_", data=result,jitter=True)
    
df1 = result[result['cate'].isin([0])]
df2 = result[result['cate'].isin([1])]

print (df1.describe())
print (df2.describe())

结果:

         cate       coef_
count    101.0  101.000000
mean     0.0      2.658496
std      0.0      0.641301
min      0.0      1.183393
25%      0.0      2.146773
50%      0.0      2.584440
75%      0.0      3.147349
max      0.0      4.042992
         cate       coef_
count    101.0  101.000000
mean     1.0      2.515850
std      0.0      0.064130
min      1.0      2.368339
25%      1.0      2.464677
50%      1.0      2.508444
75%      1.0      2.564735
max      1.0      2.654299

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值