ပြင်ဆင်နည်း- ထည့်သွင်းမှုတွင် nan၊ အဆုံးမရှိ သို့မဟုတ် dtype အတွက် အလွန်ကြီးသောတန်ဖိုး ('float64') ပါရှိသည်။
Python ကိုအသုံးပြုရာတွင် သင်ကြုံတွေ့ရလေ့ရှိသော အမှားတစ်ခုမှာ-
ValueError: Input contains infinity or a value too large for dtype('float64').
scikit-learn module မှ လုပ်ဆောင်ချက်တစ်ခုကို သင်အသုံးပြုရန် ကြိုးစားသောအခါတွင် ဤအမှားသည် များသောအားဖြင့် ဖြစ်ပေါ်တတ်သော်လည်း သင်ထည့်သွင်းထားသည့် DataFrame သို့မဟုတ် matrix တွင် NaN တန်ဖိုးများ သို့မဟုတ် အဆုံးမရှိတန်ဖိုးများရှိသည်။
အောက်ဖော်ပြပါ ဥပမာသည် ဤအမှားကို လက်တွေ့ဖြေရှင်းနည်းကို ပြသထားသည်။
အမှားကို ဘယ်လိုပြန်ထုတ်မလဲ။
ကျွန်ုပ်တို့တွင် အောက်ပါ ပန်ဒါ DataFrame ရှိသည် ဆိုပါစို့။
import pandas as pd import numpy as np #createDataFrame df = pd. DataFrame ({' x1 ': [1, 2, 2, 4, 2, 1, 5, 4, 2, 4, 4], ' x2 ': [1, 3, 3, 5, 2, 2, 1, np.inf, 0, 3, 4], ' y ': [np.nan, 78, 85, 88, 72, 69, 94, 94, 88, 92, 90]}) #view DataFrame print (df) x1 x2 y 0 1 1.0 NaN 1 2 3.0 78.0 2 2 3.0 85.0 3 4 5.0 88.0 4 2 2.0 72.0 5 1 2.0 69.0 6 5 1.0 94.0 7 4 lower 94.0 8 2 0.0 88.0 9 4 3.0 92.0 10 4 4.0 90.0
ယခု ကျွန်ုပ်တို့သည် scikit-learn လုပ်ဆောင်ချက်များကို အသုံးပြု၍ များစွာသော linear regression model ကို အံဝင်ခွင်ကျဖြစ်အောင် ကြိုးစားနေသည် ဆိုပါစို့။
from sklearn. linear_model import LinearRegression
#initiate linear regression model
model = LinearRegression()
#define predictor and response variables
x, y = df[[' x1 ', ' x2 ']], df. y
#fit regression model
model. fit (x,y)
#print model intercept and coefficients
print (model. intercept_ , model. coef_ )
ValueError: Input contains infinity or a value too large for dtype('float64').
ကျွန်ုပ်တို့အသုံးပြုနေသော DataFrame တွင် အနန္တနှင့် NaN တန်ဖိုးများ နှစ်ခုလုံးပါရှိသောကြောင့် ကျွန်ုပ်တို့သည် အမှားအယွင်းတစ်ခု လက်ခံရရှိနေပါသည်။
အမှားကိုဘယ်လိုပြင်မလဲ။
ဤအမှားကိုဖြေရှင်းရန်နည်းလမ်းမှာ အဆုံးမရှိ သို့မဟုတ် NaN တန်ဖိုးများပါရှိသော DataFrame မှအတန်းအားလုံးကို ဦးစွာဖယ်ရှားရန်ဖြစ်သည်-
#remove rows with any values that are not finite
df_new = df[np. isfinite (df). all ( 1 )]
#view updated DataFrame
print (df_new)
x1 x2 y
1 2 3.0 78.0
2 2 3.0 85.0
3 4 5.0 88.0
4 2 2.0 72.0
5 1 2.0 69.0
6 5 1.0 94.0
8 2 0.0 88.0
9 4 3.0 92.0
10 4 4.0 90.0
အဆုံးမရှိ သို့မဟုတ် NaN တန်ဖိုးများပါသော လိုင်းနှစ်ခုကို ဖယ်ရှားလိုက်ပါပြီ။
ယခု ကျွန်ုပ်တို့သည် ကျွန်ုပ်တို့၏ မျဉ်းကြောင်း ဆုတ်ယုတ်မှုပုံစံကို အံဝင်ခွင်ကျ ဆက်လက်လုပ်ဆောင်နိုင်သည်-
from sklearn. linear_model import LinearRegression
#initiate linear regression model
model = LinearRegression()
#define predictor and response variables
x, y = df_new[[' x1 ', ' x2 ']], df_new. y
#fit regression model
model. fit (x,y)
#print model intercept and coefficients
print (model. intercept_ , model. coef_ )
69.85144124168515 [5.72727273 -0.93791574]
DataFrame မှ အဆုံးမရှိ သို့မဟုတ် NaN တန်ဖိုးများဖြင့် အတန်းများကို ဦးစွာဖယ်ရှားသောကြောင့် ယခုတစ်ကြိမ်တွင် မည်သည့်အမှားအယွင်းမျှ မရရှိကြောင်း သတိပြုပါ။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် Python ရှိ အခြားသော ဘုံအမှားများကို မည်သို့ပြင်ဆင်ရမည်ကို ရှင်းပြသည်-
Python တွင် ပြင်နည်း- Object ‘numpy.ndarray’ ကို ခေါ်ဆို၍မရပါ။
ပြုပြင်နည်း- TypeError- Object ‘numpy.float64’ ကို ခေါ်ဆို၍မရပါ။
ပြင်နည်း- အမှားအမျိုးအစား- မျှော်လင့်ထားသော စာကြောင်း သို့မဟုတ် ဘိုက်အရာဝတ္ထု