ပြီးခဲ့သည့် ဥပမာ-၁ (example-1)တွင် Linear Regression Single Variable အကြောင်းကို ဖော်ပြပြီးဖြစ်သည်။ ယခု ဥပမာတွင် variable များစွာပါဝင်သည်။
အိမ်တစ် တိုက်ခန်းတစ်၏ တန်ဖိုးသည် ကြမ်းခင်းဧရိယာအကျယ်တစ်ခု အပေါ်တွင်သာ မူတည်သည် မဟုတ်ပေ။ အိပ်ခန်း အရေအတွက်၊ ဆောက်ပြီး အသုံးပြုနေသည့် သက်တမ်း စသည်တို့ ပေါ်တွင်လည်း မူတည်သည်။ ယခု ဥပမာတွင် ထိုအချက််များကို ထည့်သွင်းတွက်ချက်၍ အိမ်တန်ဖိုးကို ခန်းမှန်းမည် ဖြစ်သည်။ variable များစွာပါဝင်သည့် regression နည်းဖြင့် အိမ်တန်ဖိုးကို ခန့်မှန်းမည် ဖြစ်သည်။
အိမ်စျေးနှုန်းတို့၏ သဘောမှာ ကြမ်းခင်း ဧရိယာချင်းတူလျှင် သက်တမ်းပိုငယ်သည် အိပ်သည် စျေးကောင်းကောင်းရနိုင်သည်။ ကြမ်းခင်း ဧရိယာနှင့် အိပ်ခန်းအရေအတွက်များလျှင် စျေးပိုကြီးသည်။ ထိုအချက်များကြောင့် Linear Regression နည်းကို အသုံးပြုနိုင်သည်။
ကြမ်းခင်း ဧရိယာ၊ အိပ်ခန်း အရေအတွက်၊ သက်တမ်းစသည့်အချက် (၃)ချက်သည် သချင်္ာ ဝေါဟာရဖြင့် independent variable များ ဖြစ်ကြသည်။ Machine learning ဝေါဟာရဖြင့် feature ဖြစ်သည်။ feature အဖြစ်ရွေးချယ်မည်ဆိုလျှင် independent variable များ ဖြစ်သင့်သည်။
Data visualization လုပ်ရန် matplotlib library နှင့် Python packages များဖြစ်သည့် scipy , NumPy, pandas တို့ ကို အသုံးပြုရန် import လုပ်သည်။
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import pearsonr
import warnings
warnings.filterwarnings('ignore')
kc_house_data.csv ဖိုင်မှ ဒေတာများကို pd.read_csv ဖြင့် ဖတ်ယူသည်။
dataset = pd.read_csv('kc_house_data.csv')
ဖတ်ယူထားသည့် ဒေတာများကို ပြန်ကြည့်သည်။
dataset.shape
dataset.head()
dataset.describe() ဖြင့် statistical value များကို ကြည့်သည်။
dataset.describe()
dataset.columns
dataset.plot ဖြင့် ပုံဆွဲသည်။ အခန်းအရေအတွက်(Bedrooms)နှင့် ဈေးနှုန်း(Price) ကို တပြိုင်နက် ယှဥ်တွဲဖော်ပြသည့် ပုံဆွဲသည်။
dataset.plot(x='bedrooms', y='price', style='o')
plt.title('Bedrooms vs Price')
plt.xlabel('Bedrooms Available')
plt.ylabel('Total Price')
plt.show()
SquareFeet-Living နှင့် ဈေးနှုန်း ကို တပြိုင်နက် ယှဥ◌်တွဲဖော်ပြသည့် ပုံဆွဲသည်။
dataset.plot(x='sqft_living', y='price', style='o')
plt.title('SquareFeet-Living vs Price')
plt.xlabel('SquareFeet-Living')
plt.ylabel('Total Price')
plt.show()
sqft_living ကို x အဖြစ် price ကို y အဖြစ် သတ်မှတ်ပြီး sns.jointplot ဖြင့် နည်းကို သုံး၍ regression နည်းဖြင့် Coefficient တန်ဖိုးနှင့် p-value ကို တွက်သည်။
sns.jointplot(x='sqft_living', y='price', data=dataset, kind='reg')
x = dataset['sqft_living']
y = dataset['price']
r,p = pearsonr(x,y)
print('Coefficient: ', r)
print('p-value: ',p)
sqft_lot ကို x အဖြစ် price ကို y အဖြစ် သတ်မှတ်ပြီး sns.jointplot ဖြင့် နည်းကို သုံး၍ regression နည်းဖြင့် Coefficient တန်ဖိုးနှင့် p-value ကို တွက်သည်။
sns.jointplot(x='sqft_lot', y='price', data=dataset, kind='reg')
x = dataset['sqft_lot']
y = dataset['price']
r,p = pearsonr(x,y)
print('Coefficient: ', r)
print('p-value: ',p)
floors နှင့် ဈေးနှုန်း(Price) ကို တပြိုင်နက် ယှဥ်တွဲဖော်ပြသည့် ပုံဆွဲသည်။
dataset.plot(x='floors', y='price', style='o')
plt.title('Floors vs Price')
plt.xlabel('Floors')
plt.ylabel('Total Price')
plt.show()
waterfront နှင့် ဈေးနှုန်း(Price) ကို တပြိုင်နက် ယှဥ်တွဲဖော်ပြသည့် ပုံဆွဲသည်။
dataset.plot(x='waterfront', y='price', style='o')
plt.title('Waterfront vs Price')
plt.xlabel('Waterfront')
plt.ylabel('Total Price')
plt.show()
dataset.columns ဖြင့် dataset မှာ ကော်လံများ၏ နာမည်များကို ဖတ်ယူသည်။
dataset.columns
ကြမ်းခင်း ဧရိယာ၊ အိပ်ခန်း အရေအတွက်၊ သက်တမ်းစသည့်အချက် (၃)ချက်သည် သင်္ချာ ဝေါဟာရဖြင့် independent variable များ ဖြစ်ကြသည်။ Machine learning ဝေါဟာရဖြင့် feature ဖြစ်သည်။ feature အဖြစ်ရွေးချယ်မည်ဆိုလျှင် independent variable များ ဖြစ်သင့်သည်။ independent variable များကို X အဖြစ်သတ်မှတ်သည်။ price သည် Y ဖြစ်သည်။
x = dataset[['bedrooms', 'bathrooms', 'sqft_living',
'sqft_lot', 'floors', 'waterfront', 'view', 'condition', 'grade',
'sqft_above', 'sqft_basement','sqft_living15', 'sqft_lot15']]
y = dataset['price']
sklearn.model_selection မှ train_test_split ကို import လုပ်သည်။
test_size=0.3 သည် ဒေတာများ၏ ၃၀% ကို test လုပ်ရန် ခွဲခြားချန်ထားသည်၊ ၇၀ ကိုသာ train လုပ်ရန် အသုံးပြုသည် ဆိုလိုသည်။
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)
df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
df.head(10)
from sklearn import metrics
print('Mean Absolute Error: ', metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error: ', metrics.mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error: ', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))