Machine Learning – 5 – Pandas

Pandas என்பது நிகழ்காலத் தரவுகளை அணுகி, அலசி நமக்கேற்றவாறு வடிவமைப்பதற்கு python வழங்குகின்ற ஒரு library ஆகும். இதன் மூலம் csv, txt, json போன்ற பல்வேறு வடிவங்களில் இருக்கும் மூலத் தரவுகளை எடுத்து ஒரு dataframe-ஆக மாற்றி நமக்கேற்றவாறு தரவுகளை தகவமைத்துக் கொள்ள முடியும்.

இங்கு நாம் பார்க்கப் போகும் உதாரணத்தில் ஒரு வீட்டின் விற்பனை விலையை நிர்ணயிப்பதற்கு உதவும் பல்வேறு காரணிகளும், அதன்படி நிர்ணயிக்கப்பட்ட விலைகளும் csv கோப்பாக கொடுக்கப்பட்டுள்ளன. இதுவே training data எனப்படும். இதை வைத்துத்தான் நாம் ஒரு model-ஐ உருவாக்கப்போகிறோம்.

முதலில் model-ஐ உருவாக்குவதற்கு முன்னர் இந்த training data-ஐ நாம் புரிந்து கொள்ள வேண்டும். இதில் எத்தனை தரவுகள் உள்ளன, எத்தனை null மதிப்புகள் உள்ளன, எவையெல்லாம் விற்பனை விலையை பாதிக்கக்கூடிய முக்கியக் காரணிகள், தேவையில்லாத இன்ன பிற காரணிகளை எவ்வாறு நீக்குவது, Null மதிப்புகளை எவ்வாறு நமக்கு வேண்டிய மதிப்புகளால் மாற்றி அமைப்பது போன்றவற்றையெல்லாம் Pandas மூலம் நாம் செய்து பார்க்கப்போகிறோம். இதுவே preprocessing / feature selection எனப்படும். இதற்கான நிரல் பின்வருமாறு.


import pandas as pd
# data can be downloaded from the url: www.kaggle.com/vikrishnan/boston-house-prices
df = pd.read_csv('data.csv')
target='SalePrice'
# Understanding data
print (df.shape)
print (df.columns)
print(df.head(5))
print(df.info())
print(df.describe())
print(df.groupby('LotShape').size())
# Dropping null value columns which cross the threshold
a = df.isnull().sum()
print (a)
b = a[a>(0.05*len(a))]
print (b)
df = df.drop(b.index, axis=1)
print (df.shape)
# Replacing null value columns (text) with most used value
a1 = df.select_dtypes(include=['object']).isnull().sum()
print (a1)
print (a1.index)
for i in a1.index:
b1 = df[i].value_counts().index.tolist()
print (b1)
df[i] = df[i].fillna(b1[0])
# Replacing null value columns (int, float) with most used value
a2 = df.select_dtypes(include=['integer','float']).isnull().sum()
print (a2)
b2 = a2[a2!=0].index
print (b2)
df = df.fillna(df[b2].mode().to_dict(orient='records')[0])
# Creating new columns from existing columns
print (df.shape)
a3 = df['YrSold'] – df['YearBuilt']
b3 = df['YrSold'] – df['YearRemodAdd']
df['Years Before Sale'] = a3
df['Years Since Remod'] = b3
print (df.shape)
# Dropping unwanted columns
df = df.drop(["Id", "MoSold", "SaleCondition", "SaleType", "YearBuilt", "YearRemodAdd"], axis=1)
print (df.shape)
# Dropping columns which has correlation with target less than threshold
x = df.select_dtypes(include=['integer','float']).corr()[target].abs()
print (x)
df=df.drop(x[x<0.4].index, axis=1)
print (df.shape)
# Checking for the necessary features after dropping some columns
l1 = ["PID","MS SubClass","MS Zoning","Street","Alley","Land Contour","Lot Config","Neighborhood","Condition 1","Condition 2","Bldg Type","House Style","Roof Style","Roof Matl","Exterior 1st","Exterior 2nd","Mas Vnr Type","Foundation","Heating","Central Air","Garage Type","Misc Feature","Sale Type","Sale Condition"]
l2 = []
for i in l1:
if i in df.columns:
l2.append(i)
# Getting rid of nominal columns with too many unique values
for i in l2:
len(df[i].unique())>10
df=df.drop(i, axis=1)
print (df.columns)
df.to_csv('training_data.csv',index=False)

நிரலுக்கான விளக்கம் மற்றும் வெளியீடு :

csv-ல் உள்ள தரவுகள் df எனும் dataframe-க்குள் pandas மூலம் ஏற்றப்பட்டுள்ளது. இதில் எத்தனை rows மற்றும் columns உள்ளது என்பதை பின்வருமாறு அறியலாம்.

[code language=”bash”]
print (df.shape)
(1460, 81)
[/code]

பின்வரும் கட்டளை என்னென்ன columns உள்ளது என்பதை வெளிப்படுத்தும்.

[code language=”bash”]
print (df.columns)
Index([‘Id’, ‘MSSubClass’, ‘MSZoning’, ‘LotFrontage’, ‘LotArea’, ‘Street’,
‘Alley’, ‘LotShape’, ‘LandContour’, ‘Utilities’, ‘LotConfig’,
‘LandSlope’, ‘Neighborhood’, ‘Condition1’, ‘Condition2’, ‘BldgType’,
‘HouseStyle’, ‘OverallQual’, ‘OverallCond’, ‘YearBuilt’, ‘YearRemodAdd’,
‘RoofStyle’, ‘RoofMatl’, ‘Exterior1st’, ‘Exterior2nd’, ‘MasVnrType’,
‘MasVnrArea’, ‘ExterQual’, ‘ExterCond’, ‘Foundation’, ‘BsmtQual’,
‘BsmtCond’, ‘BsmtExposure’, ‘BsmtFinType1’, ‘BsmtFinSF1’,
‘BsmtFinType2’, ‘BsmtFinSF2’, ‘BsmtUnfSF’, ‘TotalBsmtSF’, ‘Heating’,
‘HeatingQC’, ‘CentralAir’, ‘Electrical’, ‘1stFlrSF’, ‘2ndFlrSF’,
‘LowQualFinSF’, ‘GrLivArea’, ‘BsmtFullBath’, ‘BsmtHalfBath’, ‘FullBath’,
‘HalfBath’, ‘BedroomAbvGr’, ‘KitchenAbvGr’, ‘KitchenQual’,
‘TotRmsAbvGrd’, ‘Functional’, ‘Fireplaces’, ‘FireplaceQu’, ‘GarageType’,
‘GarageYrBlt’, ‘GarageFinish’, ‘GarageCars’, ‘GarageArea’, ‘GarageQual’,
‘GarageCond’, ‘PavedDrive’, ‘WoodDeckSF’, ‘OpenPorchSF’,
‘EnclosedPorch’, ‘3SsnPorch’, ‘ScreenPorch’, ‘PoolArea’, ‘PoolQC’,
‘Fence’, ‘MiscFeature’, ‘MiscVal’, ‘MoSold’, ‘YrSold’, ‘SaleType’,
‘SaleCondition’, ‘SalePrice’],
dtype=’object’)
[/code]

head(5) முதல் 5 தரவுகளை வெளிப்படுத்தும்.

[code language=”bash”]
print(df.head(5))
Id MSSubClass MSZoning … SaleType SaleCondition SalePrice
0 1 60 RL … WD Normal 208500
1 2 20 RL … WD Normal 181500
2 3 60 RL … WD Normal 223500
3 4 70 RL … WD Abnorml 140000
4 5 60 RL … WD Normal 250000
[5 rows x 81 columns]
[/code]

info() நமது dataframe-ன் அமைப்பு பற்றிய விவரங்களை வெளிப்படுத்தும்.

[code language=”bash”]
print(df.info())
&lt;class ‘pandas.core.frame.DataFrame’&gt;
RangeIndex: 1460 entries, 0 to 1459
Data columns (total 81 columns):
Id 1460 non-null int64
MSSubClass 1460 non-null int64
……………
SaleCondition 1460 non-null object
SalePrice 1460 non-null int64
dtypes: float64(3), int64(35), object(43)
memory usage: 924.0+ KB
None
[/code]

describe() ஒருசில முக்கியப் புள்ளியியல் விவரங்களைக் கணக்கெடுத்து வெளிப்படுத்தும்.

[code language=”bash”]
print(df.describe())
Id MSSubClass … YrSold SalePrice
count 1460.000000 1460.000000 … 1460.000000 1460.000000
mean 730.500000 56.897260 … 2007.815753 180921.195890
std 421.610009 42.300571 … 1.328095 79442.502883
min 1.000000 20.000000 … 2006.000000 34900.000000
25% 365.750000 20.000000 … 2007.000000 29975.000000
50% 730.500000 50.000000 … 2008.000000 163000.000000
75% 1095.250000 70.000000 … 2009.000000 214000.000000
max 1460.000000 190.000000 … 2010.000000 755000.000000
[8 rows x 38 columns]
[/code]

groupby() ஒரு column-ல் உள்ள மதிப்புகளை வகைப்படுத்தி வெளிப்படுத்தும்.

[code language=”bash”]
print(df.groupby(‘LotShape’).size())
LotShape
IR1 484
IR2 41
IR3 10
Reg 925
dtype: int64
[/code]

ஒவ்வொரு column-லும் உள்ள null மதிப்புகளின் எண்ணிக்கையை வெளிப்படுத்தும்.

[code language=”bash”]
print (a)
Id 0
MSSubClass 0
MSZoning 0
LotFrontage 259
LotArea 0
Street 0
Alley 1369
LotShape 0
LandContour 0
Utilities 0
……………
PoolQC 1453
Fence 1179
MiscFeature 1406
MiscVal 0
MoSold 0
YrSold 0
SaleType 0
SaleCondition 0
SalePrice 0
Length: 81, dtype: int64
[/code]

0.05 என்பது Null-க்கான threshold ஆகும். அதாவது 100 க்கு 5 null மதிப்புகள் இருக்கலாம் என வரையறுக்கப்பட்டுள்ளது. எனவே அதை விட அதிக அளவு null மதிப்புகள் கொண்ட columns கண்டறியப்பட்டு வெளிப்படுத்தப்படுகிறது. பின்னர் இவை dataframe-லிருந்து நீக்கப்படுகின்றன.

[code language=”bash”]
print (b)
LotFrontage 259
Alley 1369
MasVnrType 8
MasVnrArea 8
BsmtQual 37
BsmtCond 37
BsmtExposure 38
BsmtFinType1 37
BsmtFinType2 38
FireplaceQu 690
GarageType 81
GarageYrBlt 81
GarageFinish 81
GarageQual 81
GarageCond 81
PoolQC 1453
Fence 1179
MiscFeature 406
dtype: int64
[/code]

மேற்கண்ட 18 columns-ஐயும் நீக்கிய பின்னர் 81 என்பது 63-ஆகக் குறைந்துள்ளத்தைக் காணலாம்.

[code language=”bash”]
print (df.shape)
(1460, 63)
[/code]

அடுத்ததாக Threshold-ஐ விடக் குறைவான null மதிப்புகளைப் பெற்றுள்ள text column-ஆனது வெளிப்படுத்தப்படுகிறது. include=[‘object’] என்பது text column-ஐக் குறிக்கும்.

[code language=”bash”]
print (a1)
MSZoning 0
Street 0
LotShape 0
……………
Electrical 1
KitchenQual 0
Functional 0
PavedDrive 0
SaleType 0
SaleCondition 0
dtype: int64

print (a1.index)
Index([‘MSZoning’, ‘Street’, ‘LotShape’, ‘LandContour’, ‘Utilities’,
‘LotConfig’, ‘LandSlope’, ‘Neighborhood’, ‘Condition1’, ‘Condition2’,
‘BldgType’, ‘HouseStyle’, ‘RoofStyle’, ‘RoofMatl’, ‘Exterior1st’,
‘Exterior2nd’, ‘ExterQual’, ‘ExterCond’, ‘Foundation’, ‘Heating’,
‘HeatingQC’, ‘CentralAir’, ‘Electrical’, ‘KitchenQual’, ‘Functional’,
‘PavedDrive’, ‘SaleType’, ‘SaleCondition’],
dtype=’object’)
[/code]

அந்த columns-ல் உள்ள ஒவ்வொரு மதிப்பும் எத்தனை முறை இடம்பெற்றுள்ளது என்பது கண்டறியப்பட்டு அவை ஒரு list-ஆக மாற்றப்படுகின்றன. list-ன் முதலாவது மதிப்பு அதிக அளவு இடம்பெற்றுள்ள வார்த்தை ஆகும். இவ்வார்த்தையினால் தான் null மதிப்புகள் நிரப்பப்படுகின்றன.

[code language=”bash”]
print (b1)
[‘RL’, ‘RM’, ‘FV’, ‘RH’, ‘C (all)’]
[‘Pave’, ‘Grvl’]
[‘Reg’, ‘IR1’, ‘IR2’, ‘IR3’]
……………
[‘Y’, ‘N’, ‘P’]
[‘WD’, ‘New’, ‘COD’, ‘ConLD’, ‘ConLI’, ‘ConLw’, ‘CWD’, ‘Oth’, ‘Con’]
[‘Normal’, ‘Partial’, ‘Abnorml’, ‘Family’, ‘Alloca’, ‘AdjLand’]
[/code]

அடுத்ததாக Threshold-ஐ விடக் குறைவான null மதிப்புகளைப் பெற்றுள்ள numerical column-ஆனது அதிக அளவு இடம்பெற்றுள்ள மதிப்பினால் நிரப்பப்படுகிறது. include=[‘integer’,’float’] என்பது numerical columns-ஐக் குறிக்கும்.

[code language=”bash”]
print (a2)
Id 0
MSSubClass 0
LotArea 0
……………
MoSold 0
YrSold 0
SalePrice 0
dtype: int64

print (b2)
Index([], dtype=’object’)
[/code]

அடுத்ததாக இரண்டு column-ல் உள்ள மதிப்புகளை ஒப்பிட்டு, அவைகளின் வித்தியாசம் கண்டறியப்பட்டு ஒரு புது column-ஆக dataframe-ல் இணைக்கப்படுகிறது. 63 columns-ஆக உள்ளது புது columns இணைந்த பின் 65 என மாறியிருப்பதைக் காணலாம்.

[code language=”bash”]
print (df.shape)
(1460, 63)

print (df.shape)
(1460, 65)
[/code]

தேவையில்லாத ஒருசில column-ன் பெயர்கள் நேரடியாகக் கொடுக்கப்பட்டு அவை dataframe-ல் இருந்து நீக்கப்படுகின்றன. பின் 59 என மாறியிருப்பதைக் காணலாம்.

[code language=”bash”]
print (df.shape)
(1460, 59)
[/code]

numerical columns-க்கும், target columns-க்குமான correlation கண்டறியப்பட்டு வெளிப்படுத்தப்படுகிறது. இதன் மதிப்பு 0.4 எனும் threashold-ஐ விட குறைவாக இருப்பின் அவை dataframe-லிருந்து நீக்கப்படுகின்றன.

[code language=”bash”]
print (x)
MSSubClass 0.084284
LotFrontage 0.351799
LotArea 0.263843
……………
SalePrice 1.000000
Years Before Sale 0.523350
Years Since Remod 0.509079
Name: SalePrice, dtype: float64
[/code]

மேற்கூறிய மாற்றங்கள் அனைத்தும் நிகழ்ந்த பின், நமக்குத் தேவையான ஒருசில முக்கிய விஷயங்கள் dataframe-ல் இன்னும் உள்ளதா என்பது சோதிக்கப்படுகிறது.அளவுக்கு அதிகமான தனிப்பட்ட மதிப்புகளைக் கொண்ட columns நீக்கப்படுகின்றன. இவையும் நீக்கப்பட்டபின் columns எண்ணிக்கை 38 என மாறியிருப்பதைக் காணலாம்.

[code language=”bash”]
print (df.shape)
(1460, 38)
[/code]

பின்னர் அவை எந்தெந்த columns என வெளிப்படுத்தப்படுகின்றன.

[code language=”bash”]
print (df.columns)
Index([‘MSZoning’, ‘LotShape’, ‘LandContour’, ‘Utilities’, ‘LotConfig’,
‘LandSlope’, ‘Condition1’, ‘Condition2’, ‘BldgType’, ‘HouseStyle’,
‘OverallQual’, ‘RoofStyle’, ‘RoofMatl’, ‘Exterior1st’, ‘Exterior2nd’,
‘ExterQual’, ‘ExterCond’, ‘TotalBsmtSF’, ‘HeatingQC’, ‘CentralAir’,
‘Electrical’, ‘1stFlrSF’, ‘GrLivArea’, ‘FullBath’, ‘KitchenQual’,
‘TotRmsAbvGrd’, ‘Functional’, ‘Fireplaces’, ‘GarageCars’, ‘GarageArea’,
‘PavedDrive’, ‘SalePrice’, ‘Years Before Sale’, ‘Years Since Remod’],
dtype=’object’)
[/code]

கடைசியாக இந்த dataframe-ல் இருக்கும் மதிப்புகளானது training_data எனும் பெயரில் .csv கோப்பாக சேமிக்கப்படுகின்றன. இதுவே model-ன் உருவாக்கத்திற்கு உள்ளீடாக அமையும். இதை வைத்து model-ஐ உருவாக்குவது எப்படி என்று அடுத்த பகுதியில் காணலாம்.

 

%d bloggers like this: