ကိန်းဂဏန်းဆိုင်ရာမော်ဒယ်များတွင် linear regression မှ p တန်ဖိုးများကို ထုတ်ယူနည်း
Python ရှိ statsmodels module ကိုအသုံးပြု၍ linear regression model တွင် ကိန်းဂဏန်းများအတွက် p-တန်ဖိုးများကို ထုတ်ယူရန် အောက်ပါနည်းလမ်းများကို သင်အသုံးပြုနိုင်သည်-
#extract p-values for all predictor variables for x in range(0, 3): print ( model.pvalues [x]) #extract p-value for specific predictor variable name model. pvalues . loc [' predictor1 '] #extract p-value for specific predictor variable position model. pvalues [0]
အောက်ဖော်ပြပါ ဥပမာများသည် နည်းလမ်းတစ်ခုစီကို လက်တွေ့အသုံးချနည်းကို ပြသထားသည်။
ဥပမာ- ကိန်းဂဏန်းပုံစံများတွင် မျဉ်းကြောင်းပြန်ဆုတ်ခြင်းမှ P-တန်ဖိုးများကို ထုတ်ယူပါ။
ကျွန်ုပ်တို့တွင် သင်ကြားခဲ့သည့် နာရီများ၊ ကြိုတင်ပြင်ဆင်မှုစာမေးပွဲများနှင့် အတန်းတစ်ခုရှိ ကျောင်းသားမှရရှိသော နောက်ဆုံးအဆင့်များအကြောင်း အချက်အလက်များပါရှိသော အောက်ပါပန်ဒါ DataFrame ရှိသည်ဆိုပါစို့။
import pandas as pd #createDataFrame df = pd. DataFrame ({' hours ': [1, 2, 2, 4, 2, 1, 5, 4, 2, 4, 4, 3, 6], ' exams ': [1, 3, 3, 5, 2, 2, 1, 1, 0, 3, 4, 3, 2], ' score ': [76, 78, 85, 88, 72, 69, 94, 94, 88, 92, 90, 75, 96]}) #view head of DataFrame df. head () hours exam score 0 1 1 76 1 2 3 78 2 2 3 85 3 4 5 88 4 2 2 72
“ နာရီ” နှင့် “ စာမေးပွဲများ” ကို ခန့်မှန်းသူကိန်းရှင်များနှင့် “ ရမှတ်” တုံ့ပြန်မှုကိန်းရှင် အဖြစ် “ နာရီများ” နှင့် “ စာမေးပွဲများ” ကို အသုံးပြု၍ များပြားသော linear regression မော်ဒယ် နှင့်ကိုက်ညီရန် statsmodels module ၏ OLS() လုပ်ဆောင်ချက်ကို အသုံးပြုနိုင်ပါသည်။
import statsmodels. api as sm #define predictor and response variables y = df['score'] x = df[['hours', 'exams']] #add constant to predictor variables x = sm. add_constant (x) #fit linear regression model model = sm. OLS (y,x). fit () #view model summary print ( model.summary ()) OLS Regression Results ==================================================== ============================ Dept. Variable: R-squared score: 0.718 Model: OLS Adj. R-squared: 0.661 Method: Least Squares F-statistic: 12.70 Date: Fri, 05 Aug 2022 Prob (F-statistic): 0.00180 Time: 09:24:38 Log-Likelihood: -38.618 No. Observations: 13 AIC: 83.24 Df Residuals: 10 BIC: 84.93 Df Model: 2 Covariance Type: non-robust ==================================================== ============================ coef std err t P>|t| [0.025 0.975] -------------------------------------------------- ---------------------------- const 71.4048 4.001 17.847 0.000 62.490 80.319 hours 5.1275 1.018 5.038 0.001 2.860 7.395 exams -1.2121 1.147 -1.057 0.315 -3.768 1.344 ==================================================== ============================ Omnibus: 1,103 Durbin-Watson: 1,248 Prob(Omnibus): 0.576 Jarque-Bera (JB): 0.803 Skew: -0.289 Prob(JB): 0.669 Kurtosis: 1.928 Cond. No. 11.7 ==================================================== ============================
ပုံသေအားဖြင့်၊ summary() function သည် ဒဿမနေရာသုံးခုအထိ ခန့်မှန်းပေးသူ variable တစ်ခုစီ၏ p-တန်ဖိုးများကို ပြသသည်-
- ကြားဖြတ်အတွက် P-တန်ဖိုး- 0.000
- နာရီများအတွက် P-တန်ဖိုး- 0.001
- စာမေးပွဲများအတွက် P-တန်ဖိုး- 0.315
သို့သော်၊ ကျွန်ုပ်တို့သည် အောက်ပါ syntax ကို အသုံးပြု၍ မော်ဒယ်မှ ခန့်မှန်းသူ variable တစ်ခုစီအတွက် p-values အပြည့်အစုံကို ထုတ်ယူနိုင်ပါသည်။
#extract p-values for all predictor variables for x in range(0, 3): print ( model.pvalues [x]) 6.514115622692573e-09 0.0005077783375870773 0.3154807854805659
၎င်းသည် ကျွန်ုပ်တို့အား ဒဿမနေရာများနှင့်အတူ p-တန်ဖိုးများကို မြင်နိုင်စေသည်-
- ကြားဖြတ်အတွက် P-တန်ဖိုး- 0.00000000651411562269257
- နာရီများအတွက် P တန်ဖိုး- 0.0005077783375870773
- စာမေးပွဲများအတွက် P-တန်ဖိုး- 0.3154807854805659
မှတ်ချက် – ကျွန်ုပ်တို့၏ regression model တွင် စုစုပေါင်း coefficient သုံးခုရှိသောကြောင့် ကျွန်ုပ်တို့၏ range() function တွင် 3 ကို အသုံးပြုထားပါသည်။
“ နာရီများ” variable အတွက် p-value ကို အတိအကျ ထုတ်ယူရန် အောက်ပါ syntax ကို အသုံးပြုနိုင်ပါသည်။
#extract p-value for 'hours' only model. pvalues . loc [' hours '] 0.0005077783375870773
သို့မဟုတ် regression model ၏ သီးခြားအနေအထားတစ်ခုရှိ variable တစ်ခု၏ p-value ကို ထုတ်ယူရန် အောက်ပါ syntax ကို အသုံးပြုနိုင်သည်။
#extract p-value for coefficient in index position 0 model. pvalues [0] 6.514115622692573e-09
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ပါ သင်ခန်းစာများသည် Python တွင် အခြားသော အသုံးများသော အလုပ်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
Python တွင် Logistic Regression ကို မည်သို့လုပ်ဆောင်မည်နည်း။
Python ရှိ regression မော်ဒယ်များ၏ AIC တွက်ချက်နည်း
Python တွင် ချိန်ညှိထားသော R-squared တွက်ချက်နည်း