Kaggle competition: House Prices - Advanced Regression Techniques
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )<load_from_csv>
warnings.filterwarnings('ignore')
 %matplotlib inline
train_df = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test_df = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 sub_df = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv" )<count_values>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )
train_df.count()<count_values>
df_train.sort_values(by = 'GrLivArea', ascending = False)[:2]
df_train = df_train.drop(df_train[df_train['Id'] == 1299].index)
 df_train = df_train.drop(df_train[df_train['Id'] == 524].index )
test_df.count()<import_modules>
all_data = pd.concat(( df_train.loc[:,'MSSubClass':'SaleCondition'],
df_test.loc[:,'MSSubClass':'SaleCondition']))
from nltk.corpus import stopwords
import nltk
import re
import string
from sklearn import feature_extraction, linear_model, model_selection, preprocessing<categorify>
df_train["SalePrice"] = np.log1p(df_train["SalePrice"])
 numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
skewed_feats = df_train[numeric_feats].apply(lambda x: skew(x.dropna()))
skewed_feats = skewed_feats[skewed_feats > 0.75]
skewed_feats = skewed_feats.index
all_data[skewed_feats] = np.log1p(all_data[skewed_feats] )
def change_text(text):
text = re.sub(r"n't", " not", text)
 text = re.sub(r"'re", " are", text)
 text = re.sub(r"'s", " is", text)
 text = re.sub(r"'d", " would", text)
 text = re.sub(r"'ll", " will", text)
 text = re.sub(r"'t", " not", text)
 text = re.sub(r"'ve", " have", text)
 text = re.sub(r"'m", " am", text)
 text = re.sub(r"won't", "will not", text)
 text = re.sub(r"can't", "can not", text)
 return text
train_df['text'] = train_df['text'].apply(lambda x : change_text(x))
test_df['text'] = test_df['text'].apply(lambda x : change_text(x))
train_df['text'].head()<feature_engineering>
all_data = pd.get_dummies(all_data )
def clean_preprocessor(text):
text = text.lower()
text = re.sub('\[.*?\]', '', text)
 text = re.sub("\\W"," ",text)
 text = re.sub('https?://\S+|www\.\S+', '', text)
 text = re.sub('<.*?>+', '', text)
 text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
 text = re.sub('
', '', text)
 text = re.sub('\w*\d\w*', '', text)
 return text
train_df['text'] = train_df['text'].apply(lambda x : clean_preprocessor(x))
test_df['text'] = test_df['text'].apply(lambda x : clean_preprocessor(x))
train_df['text'].head()<drop_column>
all_data = all_data.fillna(all_data.mean() )
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 train_df['text'] = train_df['text'].apply(lambda x : remove_emoji(x))
test_df['text'] = test_df['text'].apply(lambda x : remove_emoji(x))<import_modules>
X_train = all_data[:df_train.shape[0]]
X_test = all_data[df_train.shape[0]:]
y = df_train.SalePrice
from sklearn.feature_extraction.text import CountVectorizer,TfidfVectorizer
from sklearn.linear_model import LogisticRegression<feature_engineering>
def rmse_cv(model):
rmse= np.sqrt(-cross_val_score(model, X_train, y, scoring="neg_mean_squared_error", cv = 5))
return(rmse )
stopwords = stopwords.words('english')
 count_vectorizer = CountVectorizer(token_pattern=r'\w{1,}', ngram_range=(1, 2), stop_words = stopwords)
 train_vector = count_vectorizer.fit_transform(train_df['text'])
 test_vector = count_vectorizer.transform(test_df['text'] )<compute_train_metric>
model_ridge = Ridge()
clf = LogisticRegression(C=0.9,max_iter=1000,penalty='l2')
 scores = model_selection.cross_val_score(clf, train_vector, train_df["target"], cv=7, scoring="f1")
 print(scores )<train_model>
alphas = [0.05, 0.1, 0.3, 1, 3, 5, 10, 15, 30, 50, 75]
cv_ridge = [rmse_cv(Ridge(alpha = alpha)).mean()
for alpha in alphas]
clf.fit(train_vector, train_df["target"] )<save_to_csv>
model_lasso = LassoCV(alphas = [1, 0.1, 0.001, 0.0005] ).fit(X_train, y )
sub_df["target"] = clf.predict(test_vector)
 sub_df.to_csv("sample_submission.csv", index=False )<set_options>
rmse_cv(model_lasso ).mean()
pd.options.display.max_columns = None
pd.options.mode.chained_assignment = None
pd.options.display.float_format
%matplotlib inline
mpl.style.use('ggplot')
 sns.set_style('white')
 pylab.rcParams[ 'figure.figsize' ] = 8 , 6

 def load_training_data(file_name):
return pd.read_csv(file_name)

def load_properties_data(file_name):
def convert_true_to_float(df, col):
df.loc[df[col] == 'true', col] = '1'
df.loc[df[col] == 'Y', col] = '1'
df[col] = df[col].astype(float)
 prop = pd.read_csv(file_name, dtype={
'propertycountylandusecode': str,
'hashottuborspa': str,
'propertyzoningdesc': str,
'fireplaceflag': str,
'taxdelinquencyflag': str
})
 for col in ['hashottuborspa', 'fireplaceflag', 'taxdelinquencyflag']:
convert_true_to_float(prop, col)
 return prop

 def rename_columns(prop):
prop.rename(columns={
'parcelid': 'parcelid',
'airconditioningtypeid': 'cooling_id',
'architecturalstyletypeid': 'architecture_style_id',
'basementsqft': 'basement_sqft',
'bathroomcnt': 'bathroom_cnt',
'bedroomcnt': 'bedroom_cnt',
'buildingclasstypeid': 'framing_id',
'buildingqualitytypeid': 'quality_id',
'calculatedbathnbr': 'bathroom_cnt_calc',
'decktypeid': 'deck_id',
'finishedfloor1squarefeet': 'floor1_sqft',
'calculatedfinishedsquarefeet': 'finished_area_sqft_calc',
'finishedsquarefeet12': 'finished_area_sqft',
'finishedsquarefeet13': 'perimeter_area',
'finishedsquarefeet15': 'total_area',
'finishedsquarefeet50': 'floor1_sqft_unk',
'finishedsquarefeet6': 'base_total_area',
'fips': 'fips',
'fireplacecnt': 'fireplace_cnt',
'fullbathcnt': 'bathroom_full_cnt',
'garagecarcnt': 'garage_cnt',
'garagetotalsqft': 'garage_sqft',
'hashottuborspa': 'spa_flag',
'heatingorsystemtypeid': 'heating_id',
'latitude': 'latitude',
'longitude': 'longitude',
'lotsizesquarefeet': 'lot_sqft',
'poolcnt': 'pool_cnt',
'poolsizesum': 'pool_total_size',
'pooltypeid10': 'pool_unk_1',
'pooltypeid2': 'pool_unk_2',
'pooltypeid7': 'pool_unk_3',
'propertycountylandusecode': 'county_landuse_code',
'propertylandusetypeid': 'landuse_type_id' ,
'propertyzoningdesc': 'zoning_description',
'rawcensustractandblock': 'census_1',
'regionidcity': 'city_id',
'regionidcounty': 'county_id',
'regionidneighborhood': 'neighborhood_id',
'regionidzip': 'region_zip',
'roomcnt': 'room_cnt',
'storytypeid': 'story_id',
'threequarterbathnbr': 'bathroom_small_cnt',
'typeconstructiontypeid': 'construction_id',
'unitcnt': 'unit_cnt',
'yardbuildingsqft17': 'patio_sqft',
'yardbuildingsqft26': 'storage_sqft',
'yearbuilt': 'year_built',
'numberofstories': 'story_cnt',
'fireplaceflag': 'fireplace_flag',
'structuretaxvaluedollarcnt': 'tax_structure',
'taxvaluedollarcnt': 'tax_parcel',
'assessmentyear': 'tax_year',
'landtaxvaluedollarcnt': 'tax_land',
'taxamount': 'tax_property',
'taxdelinquencyflag': 'tax_overdue_flag',
'taxdelinquencyyear': 'tax_overdue_year',
'censustractandblock': 'census_2'
}, inplace=True)

def retype_columns(prop):
def float_to_categorical(df, col):
df[col] = df[col] - df[col].min()
df.loc[df[col].isnull() , col] = -1
df[col] = df[col].astype(int ).astype('category')
 list_float2categorical = ['cooling_id', 'architecture_style_id', 'framing_id',
'heating_id', 'county_id', 'construction_id', 'fips', 'landuse_type_id',
'county_landuse_code_id', 'zoning_description_id']
for col in prop.columns:
if col in list_float2categorical:
float_to_categorical(prop, col)
 elif prop[col].dtype.name == 'float64':
prop[col] = prop[col].astype(np.float32)
 gc.collect()

 def compute_datetime_aggregate_features(train):
dt = pd.to_datetime(train.transactiondate ).dt
train['year'] = dt.year
train['month'] = dt.month
train['quarter'] = dt.quarter
logerror_year = train.groupby('year' ).logerror.median().to_frame() \ 
.rename(index=str, columns={"logerror": "logerror_year"})
 logerror_month = train.groupby('month' ).logerror.median().to_frame() \ 
.rename(index=str, columns={"logerror": "logerror_month"})
 logerror_quarter = train.groupby('quarter' ).logerror.median().to_frame() \ 
.rename(index=str, columns={"logerror": "logerror_quarter"})
 logerror_year.index = logerror_year.index.map(int)
 logerror_month.index = logerror_month.index.map(int)
 logerror_quarter.index = logerror_quarter.index.map(int)
 train.drop(['year', 'month', 'quarter'], axis=1, errors='ignore', inplace=True)
 return logerror_year, logerror_month, logerror_quarter

 def add_datetime_aggregate_features(df, logerror_year, logerror_month, logerror_quarter):
dt = pd.to_datetime(df.transactiondate ).dt
df['year'] = dt.year
df['month'] = dt.month
df['quarter'] = dt.quarter
df = df.merge(how='left', right=logerror_year, on='year')
 df = df.merge(how='left', right=logerror_month, on='month')
 df = df.merge(how='left', right=logerror_quarter, on='quarter')
 df = df.drop(['year', 'month', 'quarter', 'transactiondate'], axis=1, errors='ignore')
 return df

 def add_simple_datetime_features(df):
dt = pd.to_datetime(df.transactiondate ).dt
df['year'] =(dt.year - 2016 ).astype(int)
 df['month'] =(dt.month ).astype(int)
 df['quarter'] =(dt.quarter ).astype(int)
 df.drop(['transactiondate'], axis=1, inplace=True)

def print_complete_percentage(df):
complete_percent = []
total_cnt = len(df)
 for col in df.columns:
complete_cnt = total_cnt -(df[col].isnull() ).sum()
complete_cnt -=(df[col] == -1 ).sum()
complete_percent.append(( col, complete_cnt * 1.00 / total_cnt))
complete_percent.sort(key=lambda x: x[1], reverse=True)
 for col, percent in complete_percent:
print("{}: {}".format(col, percent))<load_from_csv>
dtrain = xgb.DMatrix(X_train, label = y)
 dtest = xgb.DMatrix(X_test)
 params = {"max_depth":2, "eta":0.1}
model = xgb.cv(params, dtrain, num_boost_round=500, early_stopping_rounds=5 )
%%time
prop_2016 = load_properties_data("/kaggle/input/zillow-prize-1/properties_2016.csv")
 prop_2017 = load_properties_data("/kaggle/input/zillow-prize-1/properties_2017.csv")
 assert len(prop_2016)== len(prop_2017)
 print("Number of properties: {}".format(len(prop_2016)))
 print("Number of property features: {}".format(len(prop_2016.columns)-1))<create_dataframe>
model_xgb = xgb.XGBRegressor(n_estimators=1000, max_depth=2, learning_rate=0.1)
 model_xgb.fit(X_train, y )
def get_landuse_code_df(prop_2016, prop_2017):
temp = prop_2016.groupby('county_landuse_code')['county_landuse_code'].count()
landuse_codes = list(temp[temp >= 300].index)
 temp = prop_2017.groupby('county_landuse_code')['county_landuse_code'].count()
landuse_codes += list(temp[temp >= 300].index)
 landuse_codes = list(set(landuse_codes))
df_landuse_codes = pd.DataFrame({'county_landuse_code': landuse_codes,
'county_landuse_code_id': range(len(landuse_codes)) })
 return df_landuse_codes
def get_zoning_desc_code_df(prop_2016, prop_2017):
temp = prop_2016.groupby('zoning_description')['zoning_description'].count()
zoning_codes = list(temp[temp >= 5000].index)
 temp = prop_2017.groupby('zoning_description')['zoning_description'].count()
zoning_codes += list(temp[temp >= 5000].index)
 zoning_codes = list(set(zoning_codes))
df_zoning_codes = pd.DataFrame({'zoning_description': zoning_codes,
'zoning_description_id': range(len(zoning_codes)) })
 return df_zoning_codes
def process_columns(df, df_landuse_codes, df_zoning_codes):
df = df.merge(how='left', right=df_landuse_codes, on='county_landuse_code')
 df = df.drop(['county_landuse_code'], axis=1)
 df = df.merge(how='left', right=df_zoning_codes, on='zoning_description')
 df = df.drop(['zoning_description'], axis=1)
 df.loc[df.county_id == 3101, 'county_id'] = 0
df.loc[df.county_id == 1286, 'county_id'] = 1
df.loc[df.county_id == 2061, 'county_id'] = 2
df.loc[df.landuse_type_id == 279, 'landuse_type_id'] = 261
return df
rename_columns(prop_2016)
 rename_columns(prop_2017)
 df_landuse_codes = get_landuse_code_df(prop_2016, prop_2017)
 df_zoning_codes = get_zoning_desc_code_df(prop_2016, prop_2017)
 prop_2016 = process_columns(prop_2016, df_landuse_codes, df_zoning_codes)
 prop_2017 = process_columns(prop_2017, df_landuse_codes, df_zoning_codes)
 retype_columns(prop_2016)
 retype_columns(prop_2017)
 prop_2017.head() 
<load_from_csv>
xgb_preds = np.expm1(model_xgb.predict(X_test))
lasso_preds = np.expm1(model_lasso.predict(X_test))
train_2016 = load_training_data("/kaggle/input/zillow-prize-1/train_2016_v2.csv")
 train_2017 = load_training_data("/kaggle/input/zillow-prize-1/train_2017.csv")
 print("Number of 2016 transaction records: {}".format(len(train_2016)))
 print("Number of 2017 transaction records: {}".format(len(train_2017)))
 print("
", train_2016.head())
 print("
", train_2017.head() )<feature_engineering>
predictions = pd.DataFrame({"xgb":xgb_preds, "lasso":lasso_preds})
 predictions.plot(x = "xgb", y = "lasso", kind = "scatter" )
for prop in [prop_2016, prop_2017]:
prop['avg_garage_size'] = prop['garage_sqft'] / prop['garage_cnt']
prop['property_tax_per_sqft'] = prop['tax_property'] / prop['finished_area_sqft_calc']
prop['location_1'] = prop['latitude'] + prop['longitude']
prop['location_2'] = prop['latitude'] - prop['longitude']
prop['location_3'] = prop['latitude'] + 0.5 * prop['longitude']
prop['location_4'] = prop['latitude'] - 0.5 * prop['longitude']
prop['missing_finished_area'] = prop['finished_area_sqft'].isnull().astype(np.float32)
 prop['missing_total_area'] = prop['total_area'].isnull().astype(np.float32)
 prop.drop(['finished_area_sqft', 'total_area'], axis=1, inplace=True)
 prop['missing_bathroom_cnt_calc'] = prop['bathroom_cnt_calc'].isnull().astype(np.float32)
 prop.drop(['bathroom_cnt_calc'], axis=1, inplace=True)
 prop['derived_room_cnt'] = prop['bedroom_cnt'] + prop['bathroom_cnt']
mask =(prop.room_cnt >= 1)
 prop.loc[mask, 'avg_area_per_room'] = prop.loc[mask, 'finished_area_sqft_calc'] / prop.loc[mask, 'room_cnt']
mask =(prop.derived_room_cnt >= 1)
 prop.loc[mask,'derived_avg_area_per_room'] = prop.loc[mask,'finished_area_sqft_calc'] / prop.loc[mask,'derived_room_cnt']
prop_2017.head()<merge>
preds = 0.6*lasso_preds + 0.4*xgb_preds
<merge><EOS> 
solution = pd.DataFrame({"id":df_test.Id, "SalePrice":preds})
 solution.to_csv("ridge_sol.csv", index = False )