Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<categorify>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
pipeline = Pipeline(steps=[ 
("preprocessor", MissingValuesTransformer()), 
("featureEngineering", FeatureEngineeringTransformer())
 ])
 output = pipeline.fit_transform(x)
 output.head(7 )<categorify>
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )
categorical_columns = ["Pclass", "Sex", "Cabin", "Embarked", "FamilySize", "NameTitle"]
feature_transformer = ColumnTransformer(
 transformers=[ 
('age', SimpleImputer() , ["Age"]), 
('fare', OrdinalEncoder() , ["Fare"]), 
('categorical', OneHotEncoder() , categorical_columns)
 ]
)<categorify>
Null_train = train.isnull().sum()
Null_train[Null_train > 0]
pipeline = Pipeline(steps=[ 
("preprocessor", MissingValuesTransformer()), 
("featureEngineering", FeatureEngineeringTransformer()), 
("columnTransformer", feature_transformer)
 ])
 output = pipeline.fit_transform(x, y)
 print(output.todense() [0:9,:] )<categorify>
train.drop(['Alley', 'PoolQC', 'Fence', 'MiscFeature', 'Id'], axis = 1, inplace = True )
preprocessing_pipeline = Pipeline(steps=[ 
("preprocessor", MissingValuesTransformer()), 
("featureEngineering", FeatureEngineeringTransformer()), 
("columnTransformer", feature_transformer)
 ] )<categorify>
Null_train_data = train[['LotFrontage','MasVnrType', 'MasVnrArea', 'FireplaceQu',
'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1',
'BsmtFinType2', 'Electrical', 'GarageType', 'GarageYrBlt',
'GarageFinish', 'GarageQual', 'GarageCond']]
x_clean = preprocessing_pipeline.fit_transform(x)
 x_test_clean = preprocessing_pipeline.transform(x_test )<split>
def view_null_data(data):
return pd.DataFrame({"Data Type":data.dtypes, "Unique Count":data.apply(lambda x: x.nunique() ,axis=0),
"Null Count": data.isnull().sum() } )
x_train, x_valid, y_train, y_valid = train_test_split(x_clean, y, train_size=0.8, test_size=0.2, random_state=0 )<choose_model_class>
train['LotFrontage'] = train['LotFrontage'].fillna(train.LotFrontage.mean())
 train['GarageYrBlt'] = train['GarageYrBlt'].fillna(train.GarageYrBlt.mean())
 train['MasVnrArea'] = train['MasVnrArea'].fillna(train.MasVnrArea.mode() [0] )
model = RandomForestClassifier(criterion='gini',
n_estimators=1100,
max_depth=5,
min_samples_split=4,
min_samples_leaf=5,
max_features='auto',
oob_score=True,
random_state=42,
n_jobs=-1,
verbose=1 )<choose_model_class>
Null_test = test.isnull().sum()
Null_test[Null_test > 0]
modeling_pipeline = Pipeline(steps=[ 
("model", model)
 ] )<train_model>
Id = test['Id']
test.drop(['Alley', 'PoolQC', 'Fence', 'MiscFeature', 'Id'], axis = 1, inplace = True )
modeling_pipeline.fit(x_train, y_train )<predict_on_test>
Null_test_data = test[['MSZoning', 'LotFrontage', 'Utilities', 'Exterior1st',
'Exterior2nd', 'MasVnrType', 'MasVnrArea', 'BsmtQual','FireplaceQu',
'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinSF1',
'BsmtFinType2', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF',
'BsmtFullBath', 'BsmtHalfBath', 'KitchenQual', 'Functional',
'GarageType', 'GarageYrBlt', 'GarageFinish', 'GarageQual',
'GarageCars','GarageArea','GarageQual', 'GarageCond', 'SaleType']]
preds = modeling_pipeline.predict(x_valid)
 preds<compute_test_metric>
test['LotFrontage'] = test['LotFrontage'].fillna(test['LotFrontage'].mean())
 test['BsmtFinSF1'] = test['BsmtFinSF1'].fillna(test['BsmtFinSF1'].mean())
 test['BsmtUnfSF'] = test['BsmtUnfSF'].fillna(test['BsmtUnfSF'].mean())
 test['TotalBsmtSF'] = test['TotalBsmtSF'].fillna(test['TotalBsmtSF'].mean())
 test['GarageYrBlt'] = test['GarageYrBlt'].fillna(test['GarageYrBlt'].mean())
 test['GarageArea'] = test['GarageArea'].fillna(test['GarageArea'].mean())
 test['MasVnrArea'] = test['MasVnrArea'].fillna(test['MasVnrArea'].mode() [0])
 test['BsmtFullBath'] = test['BsmtFullBath'].fillna(test['BsmtFullBath'].mode() [0])
 test['BsmtFinSF2'] = test['BsmtFinSF2'].fillna(test['BsmtFinSF2'].mode() [0])
 test['BsmtHalfBath'] = test['BsmtHalfBath'].fillna(test['BsmtHalfBath'].mode() [0])
 test['LotFrontage'] = test['LotFrontage'].fillna(test['LotFrontage'].mode() [0] )
accuracy_score(y_valid, preds )<predict_on_test>
high_corr_col = filter_correlation(train, 0.7)
 high_corr_col
preds_test = modeling_pipeline.predict(x_test_clean )<prepare_output>
train = train.drop(['1stFlrSF', 'GarageArea', 'TotRmsAbvGrd'], axis = 1)
 test = test.drop(['1stFlrSF', 'GarageArea', 'TotRmsAbvGrd'], axis = 1 )
output = pd.DataFrame({"PassengerId": x_test["PassengerId"],
"Survived": preds_test})
 output.head()<save_to_csv>
train.drop(['MiscVal', 'MoSold','YrSold'], axis = 1, inplace = True)
 test.drop(['MiscVal', 'MoSold','YrSold'], axis = 1, inplace = True )
output.to_csv('submission.csv', index=False )<import_modules>
X = train.drop(['SalePrice'], axis = 1)
 col_to_use = list(X.columns)
 y = train['SalePrice']
print(X.shape)
 print(y.shape )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.impute import KNNImputer
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.model_selection import RepeatedStratifiedKFold,cross_val_score
from sklearn.feature_selection import chi2,SelectKBest,mutual_info_classif
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import RandomizedSearchCV,GridSearchCV,StratifiedKFold<load_from_csv>
num_cols = [col for col in col_to_use if train[col].dtype in ['int64', 'float64']]
cat_cols = [col for col in col_to_use if train[col].dtype == 'object']
num_cols
train=pd.read_csv("/kaggle/input/titanic/train.csv")
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 target=train['Survived']
sub=pd.DataFrame(test['PassengerId'] )<categorify>
num_processor = Pipeline(steps = [ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('scaler', MinMaxScaler())
 ] )
null_data=['Survived','Pclass','Age','SibSp','Parch','Fare']
imputer=KNNImputer()
imputer.fit(train[null_data])
 b=imputer.transform(train[null_data])
 a=pd.DataFrame(b)
 train['Age']=a.iloc[:,2]
train['Fare']=a.iloc[:,5] 
<data_type_conversions>
cat_processor = Pipeline(steps = [ 
('imputer', SimpleImputer(strategy = 'most_frequent')) , 
('ohe', OneHotEncoder(handle_unknown = 'ignore', sparse = False))
] )
print(train.isnull().sum() ,'
',test.isnull().sum())
 a=train.groupby(train['Pclass'])['Age'].median()
test['Age']=test['Age'].fillna(test['Pclass'].map(a))
test['Fare'].fillna(test['Fare'].median() ,inplace=True)
 train['Cabin'].fillna('X',inplace=True)
 test['Cabin'].fillna('X',inplace=True)
 train['Embarked'].fillna('S',inplace=True)
 print('
',train.isnull().sum() ,'
',test.isnull().sum())
<categorify>
preprocessor = ColumnTransformer([ 
('num', num_processor, num_cols), 
('cat', cat_processor, cat_cols)
 ] )
x=[]
for i in range(len(train)) :
a=train['Cabin'][i][0]
x.append(a)
 train['Cabin']=x
x=[]
for i in range(len(test)) :
x.append(test['Cabin'][i][0])
 test['Cabin']=x
train['Cabin']=train['Cabin'].map({'A':3,'B':3,'C':3,'D':2,"E":2,'F':2,'T':3,'X':0,'G':1,})
 test['Cabin']=test['Cabin'].map({'A':3,'B':3,'C':3,'D':2,"E":2,'F':2,'T':3,'X':0,'G':1,})
 a=[]
for i in range(len(train)) :
a.append(train['Name'][i].split() [1])
 train['Title']=a
a=[]
for i in range(len(test)) :
a.append(test['Name'][i].split() [1])
 test['Title']=a
train['Passenger']=train['SibSp']+train['Parch']+1
test['Passenger']=test['SibSp']+test['Parch']+1
def family(size):
a=''
if(size<=1):
a='lonely'
elif(size<=3):
a='nuclear'
elif(size<=6):
a='middle'
else:
a='large'
return a
train['Passenger']=train['Passenger'].map(family)
 test['Passenger']=test['Passenger'].map(family)
 a=[]
for i in range(len(train['Ticket'])) :
if not(train['Ticket'][i][0].isdigit()):
a.append(1)
 else:
a.append(0)
 train['Ticket']=a
a=[]
for i in range(len(test['Ticket'])) :
if not(test['Ticket'][i][0].isdigit()):
a.append(1)
 else:
a.append(0)
 test['Ticket']=a<categorify>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 42 )
train.drop(['Name','PassengerId'],axis=1,inplace=True)
 test.drop(['Name','PassengerId'],axis=1,inplace=True)
 encode=LabelEncoder()
encode.fit(train['Sex'])
 train['Sex']=encode.transform(train['Sex'])
 test['Sex']=encode.transform(test['Sex'])
 encode.fit(train['Embarked'])
 train['Embarked']=encode.transform(train['Embarked'])
 test['Embarked']=encode.transform(test['Embarked'])
 encode.fit(train['Passenger'])
 train['Passenger']=encode.transform(train['Passenger'])
 test['Passenger']=encode.transform(test['Passenger'])
<create_dataframe>
model = LinearRegression()
model1 = Lasso()
model2 = Ridge()
model3 = DecisionTreeRegressor(max_leaf_nodes = 30, random_state = 42)
 model4 = RandomForestRegressor(n_estimators = 500, random_state = 42)
 model5 = XGBRegressor(n_estimators = 1000, learning_rate = 0.05, random_state = 42)
 model6 = GradientBoostingRegressor(learning_rate = 0.1, n_estimators = 165, random_state=42)
 model7 = CatBoostRegressor(random_state=0,verbose=False, depth = 5, early_stopping_rounds=300, learning_rate= 0.1 )
a=pd.DataFrame(train['Title'].value_counts())
 a.index
train['Title']=train['Title'].replace(['Dr.', 'Rev.', 'y', 'Planke,',
'Impe,', 'Gordon,', 'Mlle.', 'Col.', 'Major.', 'Pelsmaeker,', 'Capt.',
'Shawah,', 'Messemaeker,', 'Billiard,', 'Carlo,', 'Mulder,', 'Ms.',
'the', 'Jonkheer.', 'Don.', 'Mme.', 'Melkebeke,', 'der', 'Steen,',
'Cruyssen,', 'Velde,', 'Walle,'],'Rare')
 test['Title']=test['Title'].replace(['Dr.', 'Rev.', 'y', 'Planke,',
'Impe,', 'Gordon,', 'Mlle.', 'Col.', 'Major.', 'Pelsmaeker,', 'Capt.',
'Shawah,', 'Messemaeker,', 'Billiard,', 'Carlo,', 'Mulder,', 'Ms.',
'the', 'Jonkheer.', 'Don.', 'Mme.', 'Melkebeke,', 'der', 'Steen,',
'Cruyssen,', 'Velde,', 'Walle,'],'Rare')
 train['Title']=train['Title'].map({'Mr.':1,'Miss.':2,'Mrs.':2,'Rare':0,'Master.':3})
 test['Title']=test['Title'].map({'Mr.':1,'Miss.':2,'Mrs.':2,'Rare':0,'Master.':3,'Brito,':0,'Khalil,':0,'Palmquist,':0})
 train.drop('Survived',axis=1,inplace=True )<train_model>
def build_model(model):
clf = Pipeline(steps = [ 
('preprocessor', preprocessor), 
('model', model)
 ])
 clf.fit(X_train, y_train)
 print(model)
 print("Train set score:", clf.score(X_train, y_train))
print("Test set score:", clf.score(X_test, y_test))
print("
")
 print("Train set rmse:", mean_squared_error(y_train, clf.predict(X_train),squared=False))
print("Test set rmse:", mean_squared_error(y_test, clf.predict(X_test),squared=False))
print("
")
 predictions = clf.predict(test)
 return predictions
fs=SelectKBest(score_func=chi2,k='all')
 fs.fit(train,target)
 x=fs.transform(train)
 for i in range(len(fs.scores_)) :
print('Feature %d: %f' %(i, fs.scores_[i]))<drop_column>
predictions = build_model(model7 )
train.drop(['SibSp','Ticket','Parch'],axis=1,inplace=True)
 test.drop(['SibSp','Ticket','Parch'],axis=1,inplace=True )<feature_engineering>
predictions = np.exp(predictions )
train['genderclass']=train['Sex']*train['Pclass']
test['genderclass']=test['Sex']*test['Pclass']<split>
output = pd.DataFrame({'Id': Id, 'SalePrice': predictions})
 output.to_csv('submission.csv', index = False)
 sub = pd.read_csv('./submission.csv')
 sub