ပြင်နည်း- ပန်ဒါဒေတာကို numpy အရာဝတ္ထုအမျိုးအစားသို့ ပြောင်းသည်။ ထည့်သွင်းဒေတာကို np.asarray(data) ဖြင့် စစ်ဆေးပါ။
Python ကိုအသုံးပြုရာတွင် သင်ကြုံတွေ့ရနိုင်သည့် အမှားတစ်ခုမှာ-
ValueError : Pandas data cast to numpy dtype of object. Check input data with
np.asarray(data).
သင်သည် Python တွင် ဆုတ်ယုတ်မှုပုံစံတစ်ခုကို အံဝင်ခွင်ကျဖြစ်အောင် ကြိုးပမ်းပြီး မော်ဒယ်နှင့် အံကိုက်မဖြစ်မီ အမျိုးအစားခွဲကိန်းများကို dummy variableများ အဖြစ်သို့ မပြောင်းလဲနိုင်သောအခါတွင် ဤအမှားအယွင်း ဖြစ်ပေါ်ပါသည်။
အောက်ဖော်ပြပါ ဥပမာသည် ဤအမှားကို လက်တွေ့တွင် မည်သို့ပြုပြင်ရမည်ကို ပြသထားသည်။
အမှားကို ဘယ်လိုပြန်ထုတ်မလဲ။
ကျွန်ုပ်တို့တွင် အောက်ပါ ပန်ဒါ DataFrame ရှိသည် ဆိုပါစို့။
import pandas as pd
#createDataFrame
df = pd. DataFrame ({' team ': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
' assists ': [5, 7, 7, 9, 12, 9, 9, 4],
' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12],
' points ': [14, 19, 8, 12, 17, 19, 22, 25]})
#view DataFrame
df
team assists rebounds points
0 A 5 11 14
1 To 7 8 19
2 A 7 10 8
3 to 9 6 12
4 B 12 6 17
5 B 9 5 19
6 B 9 9 22
7 B 4 12 25
ယခု ကျွန်ုပ်တို့သည် တုံ့ပြန်မှုကိန်းရှင်အဖြစ် ခန့်မှန်းကိန်းရှင်များနှင့် အချက်များကို တုံ့ပြန်မှုကိန်းရှင် အဖြစ် ခန့်မှန်းပေးသည့်ကိန်းရှင်များနှင့် အချက်များကို အသုံးပြု၍ များစွာသောမျဉ်းကြောင်းဆုတ်ယုတ်မှုပုံစံကို အံဝင်ခွင်ကျဖြစ်အောင် ကြိုးစားသည်ဆိုပါစို့။
import statsmodels. api as sm
#define response variable
y = df['points']
#define predictor variables
x = df[['team', 'assists', 'rebounds']]
#add constant to predictor variables
x = sm. add_constant (x)
#attempt to fit regression model
model = sm. OLS (y,x). fit ()
ValueError : Pandas data cast to numpy dtype of object. Check input data with
np.asarray(data).
“ team” variable သည် အမျိုးအစားအလိုက်ဖြစ်ပြီး၊ regression model နှင့်မကိုက်ညီမီ ၎င်းကို dummy variable အဖြစ်သို့ မပြောင်းထားသောကြောင့် အမှားအယွင်းတစ်ခုရရှိခဲ့ပါသည်။
အမှားကိုဘယ်လိုပြင်မလဲ။
ဤအမှားကို ပြင်ဆင်ရန် အလွယ်ကူဆုံးနည်းလမ်းမှာ pandas.get_dummies() လုပ်ဆောင်ချက်ကို အသုံးပြု၍ “ team” variable ကို dummy variable အဖြစ်သို့ ပြောင်းရန်ဖြစ်သည်။
မှတ်ချက် – regression မော်ဒယ်များတွင် dummy variables များအကြောင်း အမြန်ပြန်လည်ပြင်ဆင်ရန်အတွက် ဤသင်ခန်းစာကို ကြည့်ပါ။
အောက်ပါကုဒ်သည် “ team” ကို dummy variable သို့ မည်သို့ပြောင်းရမည်ကို ပြသသည်-
import pandas as pd
#createDataFrame
df = pd. DataFrame ({' team ': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
' assists ': [5, 7, 7, 9, 12, 9, 9, 4],
' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12],
' points ': [14, 19, 8, 12, 17, 19, 22, 25]})
#convert "team" to dummy variable
df = pd. get_dummies (df, columns=[' team '], drop_first= True )
#view updated DataFrame
df
assists rebounds points team_B
0 5 11 14 0
1 7 8 19 0
2 7 10 8 0
3 9 6 12 0
4 12 6 17 1
5 9 5 19 1
6 9 9 22 1
7 4 12 25 1
“အဖွဲ့” ကော်လံရှိ တန်ဖိုးများကို “A” နှင့် “B” မှ 0 နှင့် 1 သို့ ပြောင်းထားသည်။
ယခု ကျွန်ုပ်တို့သည် ပြောင်းလဲနိုင်သော “team_B” ကို အသုံးပြု၍ များစွာသော linear regression model ကို အံကိုက်လုပ်နိုင်သည်-
import statsmodels. api as sm
#define response variable
y = df['points']
#define predictor variables
x = df[['team_B', 'assists', 'rebounds']]
#add constant to predictor variables
x = sm. add_constant (x)
#fit regression model
model = sm. OLS (y,x). fit ()
#view summary of model fit
print ( model.summary ())
OLS Regression Results
==================================================== ============================
Dept. Variable: R-squared points: 0.701
Model: OLS Adj. R-squared: 0.476
Method: Least Squares F-statistic: 3.119
Date: Thu, 11 Nov 2021 Prob (F-statistic): 0.150
Time: 14:49:53 Log-Likelihood: -19.637
No. Observations: 8 AIC: 47.27
Df Residuals: 4 BIC: 47.59
Df Model: 3
Covariance Type: non-robust
==================================================== ============================
coef std err t P>|t| [0.025 0.975]
-------------------------------------------------- ----------------------------
const 27.1891 17.058 1.594 0.186 -20.171 74.549
team_B 9.1288 3.032 3.010 0.040 0.709 17.548
assists -1.3445 1.148 -1.171 0.307 -4.532 1.843
rebounds -0.5174 1.099 -0.471 0.662 -3.569 2.534
==================================================== ============================
Omnibus: 0.691 Durbin-Watson: 3.075
Prob(Omnibus): 0.708 Jarque-Bera (JB): 0.145
Skew: 0.294 Prob(JB): 0.930
Kurtosis: 2.698 Cond. No. 140.
==================================================== ============================
ဤတစ်ကြိမ်တွင် ကျွန်ုပ်တို့သည် အမှားအယွင်းမရှိဘဲ ဆုတ်ယုတ်မှုပုံစံကို အံဝင်ခွင်ကျဖြစ်စေနိုင်ကြောင်း သတိပြုပါ။
မှတ်ချက် – statsmodels စာကြည့်တိုက်တွင် ols() လုပ်ဆောင်ချက်အတွက် စာရွက်စာတမ်းအပြည့်အစုံကို ဤနေရာတွင် ရှာတွေ့နိုင်ပါသည်။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် Python ရှိ အခြားသော ဘုံအမှားများကို မည်သို့ပြင်ဆင်ရမည်ကို ရှင်းပြသည်-
Pandas တွင် KeyError ကိုဘယ်လိုပြင်မလဲ။
ပြင်ဆင်နည်း- ValueError- float NaN ကို int အဖြစ်သို့ ပြောင်းလဲ၍မရပါ။
ပြုပြင်နည်း- တန်ဖိုးအမှား- Operands များကို ပုံသဏ္ဍာန်များဖြင့် ထုတ်လွှင့်၍မရပါ။