残差散点图的线性回归循环

时间:2020-12-22 20:27:28

标签: python loops scikit-learn regression scatter-plot

我正在运行线性回归模拟,每个模型都根据“标签”变量的不同值。我可以为每个模型打印指标,但我无法为每个模型运行不同的散点图。所有图形都在单个散点图中重现。我想为每个模型运行一个指标和一个不同的散点图

import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
from scipy.stats import binom
from scipy.stats import norm
import numpy as np

from scipy.stats import norm
# generate random numbers from N(0,1)
x = norm.rvs(size=10000,loc=0,scale=1)
y = norm.rvs(size=10000,loc=0,scale=1)
z = binom.rvs(n=10,p=0.8,size=10000)
df = pd.DataFrame(data={'v1':x.flatten(),'target':y.flatten(),'label':z.flatten()})

classes=df.label.unique().tolist()
results = []


for name in classes:
    df_subset=df.loc[df['label']==name]
    
    reg = LinearRegression()
    reg.fit(df_subset['v1'].values.reshape(-1, 1), df_subset["target"].values.reshape(-1, 1))
    predictions = reg.predict(df_subset['v1'].values.reshape(-1, 1))
    
    res=np.mean((predictions - df_subset["target"].values.reshape(-1, 1)) ** 2)
    results.append(res)
    
    msg = "Metric model %s: %f " % (name, res)
    print(msg)
    
    df_subset['pred']=predictions
    sns.scatterplot(data=df_subset, x='pred', y="target")

2 个答案:

答案 0 :(得分:1)

只需在 sns 绘图之前创建一个新图形即可。 plt.figure() <--- 在 sns 绘图之后执行 plt.show() 以便您可以在每个绘图之前显示打印语句(模型度量)。

import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
from scipy.stats import binom
from scipy.stats import norm
import numpy as np
import seaborn as sns

from scipy.stats import norm
# generate random numbers from N(0,1)
x = norm.rvs(size=10000,loc=0,scale=1)
y = norm.rvs(size=10000,loc=0,scale=1)
z = binom.rvs(n=10,p=0.8,size=10000)
df = pd.DataFrame(data={'v1':x.flatten(),'target':y.flatten(),'label':z.flatten()})

classes=df.label.unique().tolist()
results = []


for name in classes:
    df_subset=df.loc[df['label']==name]
    
    reg = LinearRegression()
    reg.fit(df_subset['v1'].values.reshape(-1, 1), df_subset["target"].values.reshape(-1, 1))
    predictions = reg.predict(df_subset['v1'].values.reshape(-1, 1))
    
    res=np.mean((predictions - df_subset["target"].values.reshape(-1, 1)) ** 2)
    results.append(res)
    
    msg = "Metric model %s: %f " % (name, res)
    print(msg)
    plt.figure() #<-----------here
    df_subset['pred']=predictions
    sns.scatterplot(data=df_subset, x='pred', y="target")
    plt.show() #<------------ here

答案 1 :(得分:1)

我建议先安装 matplotlib 库,然后

import matplotlib.pyplot as plt
y = 0
.
.
.
#inside your for loop
plot = sns.scatterplot(data=df_subset, x='pred', y="target")
plt.savefig('plot_' + str(y))
plt.clf()
相关问题