Kaggle competition: House Prices - Advanced Regression Techniques
knn = KNeighborsClassifier(n_neighbors=7)
 knn.fit(X_train,y_train)
 knn.score(X_test,y_test )<predict_on_test>
warnings.filterwarnings('ignore' )
prediction = knn.predict(test_data.drop("RefId",axis=1)) 
<create_dataframe>
def prepare_df(df, cols_raw, cols_drop):
result = df[cols_raw].copy()
if len(cols_drop)> 0:
result.drop(cols_drop, axis=1, inplace=True)
 return result
submission = pd.DataFrame(data=prediction,columns=["IsBadBuy"])
 submission["RefId"] = test_data["RefId"]
submission.set_index("RefId",inplace=True )<save_to_csv>
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv',na_filter=False)
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv',na_filter=False )
submission.to_csv("submission.csv" )<load_from_csv>
object_c = [
'MSZoning', 'Street', 'Alley',
'LotShape', 'LandContour', 'Utilities', 'LotConfig',
'LandSlope', 'Neighborhood', 'Condition1', 'Condition2',
'BldgType', 'HouseStyle', 'RoofStyle', 'RoofMatl',
'Exterior1st', 'Exterior2nd', 'MasVnrType',
'ExterQual', 'ExterCond', 'Foundation',
'BsmtQual', 'BsmtCond', 'BsmtExposure',
'BsmtFinType1', 'BsmtFinType2', 'Heating',
'HeatingQC', 'CentralAir', 'Electrical',
'KitchenQual', 'Functional', 'FireplaceQu',
'GarageType', 'GarageFinish', 'GarageQual',
'PavedDrive', 'PoolQC', 'Fence', 'MiscFeature',
'SaleType', 'SaleCondition'
]
numeric_c = [
'LotArea', 'YearBuilt', 'YearRemodAdd',
'BsmtUnfSF', 'TotalBsmtSF', 'LowQualFinSF', 'GrLivArea',
'BsmtFullBath', 'BsmtHalfBath', 'FullBath', 'HalfBath', 'BedroomAbvGr', 'KitchenAbvGr', 'TotRmsAbvGrd', 'Fireplaces',
'GarageArea', 'LotFrontage', 'MasVnrArea', 'BsmtFinSF1', 'BsmtFinSF2', '1stFlrSF', '2ndFlrSF', 'GarageYrBlt', 'GarageCars',
'WoodDeckSF', 'OpenPorchSF', 'EnclosedPorch', '3SsnPorch', 'ScreenPorch', 'PoolArea', 'MiscVal',
'OverallCond', 'OverallQual', 'MSSubClass', 'MoSold', 'YrSold'
]
convert_f = ['LotFrontage', 'MasVnrArea', 'BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'GarageYrBlt', 'BsmtFullBath', 'BsmtHalfBath', 'GarageCars', 'GarageArea']
train_data=pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()<load_from_csv>
y_col = 'SalePrice'
cols_raw = object_c.copy()
cols_raw.extend(numeric_c)
 cols_raw_y = cols_raw.copy()
cols_raw_y.extend([y_col])
 categorical_cols_l, _ = split_cat_cols(train, test, object_c)
 categorical_cols_oh = object_c
numerical_cols = numeric_c
count_enc_cols = []
drop = []
test_data=pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()<count_missing_values>
type_converter = FunctionTransformer(func=convert_df_types, kw_args={'target': [(convert_f, 'numeric')]})
 numerical_transformer = SimpleImputer(strategy='mean')
 steps = []
if count_enc_cols:
count_encoder = ce.CountEncoder(cols=count_enc_cols)
 steps.append(( 'count_enc', count_encoder))
steps.append(( 'imputer', SimpleImputer(strategy='most_frequent')))
 steps.append(( 'label', ce.BinaryEncoder()))
categorical_transformer_l = Pipeline(steps=steps)
 steps = []
if count_enc_cols:
count_encoder = ce.CountEncoder(cols=count_enc_cols)
 steps.append(( 'count_enc', count_encoder))
steps.append(( 'imputer', SimpleImputer(strategy='most_frequent')))
 steps.append(( 'onehot', OneHotEncoder(handle_unknown='ignore', sparse=False)))
 categorical_transformer_oh = Pipeline(steps=steps)
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', numerical_transformer, numerical_cols), 
('cat_l', categorical_transformer_l, categorical_cols_l), 
('cat_oh', categorical_transformer_oh, categorical_cols_oh)
 ])
 drop_cols = []
feature_eng = FunctionTransformer(func=prepare_df, kw_args={'cols_raw': cols_raw, 'cols_drop': drop_cols} )
train_data.isnull().sum()<count_missing_values>
final_layer = StackingRegressor(
 estimators=[('rf', RandomForestRegressor(random_state=42)) , 
('gbrt', GradientBoostingRegressor(random_state=42)) ],
final_estimator=RidgeCV() 
)
 model = StackingRegressor(
 estimators=[ 
('x', xgb.XGBRegressor(n_estimators=5000, learning_rate=0.01, max_depth=4, n_jobs=4, random_state=0)) , 
('lasso', LassoCV(random_state=42)) , 
('svr', SVR(kernel='rbf')) ],
final_estimator=final_layer 
)
 my_pipeline = Pipeline(
 steps=[ 
('type_conv', type_converter), 
('preprocessor', preprocessor), 
('scaler', PowerTransformer()), 
('model', model)
 ] 
)
 _ = my_pipeline.fit(train[cols_raw], train[y_col] )
test_data.isnull().sum()<count_values>
output = pd.DataFrame({'Id': test.Id, 'SalePrice': my_pipeline.predict(test)})
 output.to_csv('result28.csv', index=False)
 print("Your submission was successfully saved!" )