Kaggle competition: House Prices - Advanced Regression Techniques
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Lambda, Flatten, BatchNormalization
from tensorflow.keras.layers import Conv2D, MaxPool2D, AvgPool2D
from tensorflow.keras.optimizers import Adadelta
from keras.utils.np_utils import to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.callbacks import LearningRateScheduler<categorify>
import matplotlib.pyplot as plt
import seaborn as sns
nclasses = mnist_train_label.max() - mnist_train_label.min() + 1
mnist_train_label = to_categorical(mnist_train_label, num_classes = nclasses)
 print("Shape of ytrain after encoding: ", mnist_train_label.shape )<choose_model_class>
train_data=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test_data=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv' )
def build_model(input_shape=(28, 28, 1)) :
model = Sequential()
model.add(Conv2D(32, kernel_size = 3, activation='relu', input_shape = input_shape))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size = 4, activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
return model
def compile_model(model, optimizer='adam', loss='categorical_crossentropy'):
model.compile(optimizer=optimizer, loss=loss, metrics=["accuracy"])
 def train_model(model, train, test, epochs, split):
history = model.fit(train, test, shuffle=True, epochs=epochs, validation_split=split)
 return history<train_model>
nan=[]
for i in x:
n=train_data[i].isnull().sum()
if n>0:
nan.append(i)
 nan
cnn_model = build_model(( 28, 28, 1))
compile_model(cnn_model, 'adam', 'categorical_crossentropy')
 model_history = train_model(cnn_model, mnist_train_data, mnist_train_label, 80, 0.2 )<predict_on_test>
less=[]
high=[]
for i in x:
l=train_data[i].nunique()
if l<50:
less.append(i)
 else :
high.append(i)
predictions = cnn_model.predict(mnist_test_arr )<define_variables>
numeric_dtypes = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
numeric = []
for i in train_data.columns:
if train_data[i].dtype in numeric_dtypes:
numeric.append(i)
 _=numeric.pop(0)
 _=numeric.pop(-1)
 numeric
predictions_test = []
for i in predictions:
predictions_test.append(np.argmax(i))<save_to_csv>
category = []
for i in x:
if train_data[i].dtypes == 'object':
category.append(i)
 category
submission = pd.DataFrame({
"ImageId": mnist_test.index+1,
"Label": predictions_test
})
 submission.to_csv('my_submission.csv', index=False )<set_options>
corrected_price= np.log1p(train_data["SalePrice"])
%matplotlib inline 
<load_from_csv>
bldg = train_data[['BldgType','HouseStyle','OverallQual','OverallCond','SalePrice','MiscVal']]
mnist_train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 mnist_test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )<count_missing_values>
bldg=bldg.groupby(['BldgType'] ).sum()
mnist_train.isna().any().any()<categorify>
garage=train_data[['GarageType','GarageFinish','GarageCars','GarageQual','GarageCond','SalePrice']]
garage.head()
mnist_train_data = mnist_train.loc[:, "pixel0":]
mnist_train_label = mnist_train.loc[:, "label"]
mnist_train_data = mnist_train_data/255.0
mnist_test = mnist_test/255.0<normalization>
g=g=garage.groupby(['GarageType'] ).mean()
g['GarageCars']=g['GarageCars'].astype(int)
 g['SalePrice']=g['SalePrice'].astype(int )
standardized_scalar = StandardScaler()
standardized_data = standardized_scalar.fit_transform(mnist_train_data)
 standardized_data.shape<train_model>
misc=train_data[['MiscVal','SaleType','SalePrice']]
misc.groupby(['SaleType'] ).mean()
cov_matrix = np.matmul(standardized_data.T, standardized_data)
 cov_matrix.shape<compute_train_metric>
date=train_data[['MoSold','YrSold','SalePrice','SaleCondition']]
lambdas, vectors = eigh(cov_matrix, eigvals=(782, 783))
vectors.shape<concatenate>
train = train_data.copy()
test=test_data.copy()
label=train_data['SalePrice'].reset_index(drop=True)
 train=train.drop(['SalePrice'],axis=1 )
new_coordinates = np.matmul(vectors, standardized_data.T)
 print(new_coordinates.shape)
 new_coordinates = np.vstack(( new_coordinates, mnist_train_label)).T<prepare_output>
data=pd.concat([train,test] ).reset_index(drop=True )
df_new = pd.DataFrame(new_coordinates, columns=["f1", "f2", "labels"])
 df_new.head()<normalization>
train_id=train['Id']
test_id=test['Id']
data=data.drop(['Id'],axis=1)
 data.reset_index(drop=True )
pca = decomposition.PCA()
pca.n_components = 2
pca_data = pca.fit_transform(standardized_data)
 pca_data.shape<concatenate>
num_nan=[]
for i in numeric:
for j in nan:
if i==j:
num_nan.append(j)
 print(j)
pca_data = np.vstack(( pca_data.T, mnist_train_label)).T<prepare_output>
imputer=SimpleImputer()
df_PCA = pd.DataFrame(new_coordinates, columns=["f1", "f2", "labels"])
 df_PCA.head()<create_dataframe>
imp_data=pd.DataFrame(imputer.fit_transform(data[num_nan]))
imp_data.columns=num_nan
data[num_nan]=imp_data
mnist_train_data = np.array(mnist_train_data)
 mnist_train_label = np.array(mnist_train_label )<import_modules>
cat_nan=[set(nan)-set(num_nan)]
cat_nan
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Lambda, Flatten, BatchNormalization
from tensorflow.keras.layers import Conv2D, MaxPool2D, AvgPool2D
from tensorflow.keras.optimizers import Adadelta
from keras.utils.np_utils import to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.callbacks import LearningRateScheduler<categorify>
data['Alley'] = data['Alley'].fillna(data["Alley"].mode() [0])
 for i in('BsmtCond','BsmtFinType1','BsmtFinType2','BsmtQual','BsmtExposure'):
data[i] = data[i].fillna('None')
 data['Electrical'] = data['Electrical'].fillna('SBrkr')
 data['MasVnrType']=data['MasVnrType'].fillna(data['MasVnrType'].mode() [0])
 data['Fence'] = data['Fence'].fillna(data['Fence'].mode() [0])
 for i in('GarageFinish','GarageCond','GarageQual','GarageType'):
data[i]=data[i].fillna('None')
 data['FireplaceQu'] = data['FireplaceQu'].fillna('Fire')
 data['MiscFeature']=data['MiscFeature'].fillna('Misc')
 data['PoolQC']=data['PoolQC'].fillna('Pool')
nclasses = mnist_train_label.max() - mnist_train_label.min() + 1
mnist_train_label = to_categorical(mnist_train_label, num_classes = nclasses)
 print("Shape of ytrain after encoding: ", mnist_train_label.shape )<choose_model_class>
data.update(data[numeric].fillna(0))
data.update(data[category].fillna('None'))
def build_model(input_shape=(28, 28, 1)) :
model = Sequential()
model.add(Conv2D(32, kernel_size = 3, activation='relu', input_shape = input_shape))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size = 4, activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
return model
def compile_model(model, optimizer='adam', loss='categorical_crossentropy'):
model.compile(optimizer=optimizer, loss=loss, metrics=["accuracy"])
 def train_model(model, train, test, epochs, split):
history = model.fit(train, test, shuffle=True, epochs=epochs, validation_split=split)
 return history<train_model>
data['hasallutilities']=1*(data['Utilities'] == 'AllPub')
 data['average']=1*(data['OverallQual'] == 5 | 6)
 data['ext_qual']=1*(data['ExterQual'] == 'Gd')
 data['fencing']=1*(data['Fence'] == 'GdPrv')
 data['total_qual'] = data['OverallQual'] + data['OverallCond']
data['total_floors_area'] = data['TotalBsmtSF'] + data['1stFlrSF'] + data['2ndFlrSF']
data['total_sqr'] = data['TotalBsmtSF'] + data['1stFlrSF'] + data['2ndFlrSF']
data['total_poarch_sf'] =(data['OpenPorchSF'] + data['3SsnPorch'] +data['EnclosedPorch'] + data['ScreenPorch'] +data['WoodDeckSF'])
 data['hasfireplace'] = data['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
 data['haspool'] = data['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 data['has2ndfloor'] = data['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 data['hasgarage'] = data['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 data=data.drop(['PavedDrive','Street','PavedDrive'],axis=1 )
cnn_model = build_model(( 28, 28, 1))
compile_model(cnn_model, 'adam', 'categorical_crossentropy')
 model_history = train_model(cnn_model, mnist_train_data, mnist_train_label, 80, 0.2 )<predict_on_test>
data=pd.get_dummies(data ).reset_index(drop=True)
 data.head(3 )
predictions = cnn_model.predict(mnist_test_arr )<define_variables>
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
import lightgbm as lgb
from lightgbm import LGBMRegressor
from sklearn.model_selection import KFold, cross_val_score
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import RobustScaler
from sklearn.pipeline import make_pipeline
predictions_test = []
for i in predictions:
predictions_test.append(np.argmax(i))<save_to_csv>
model1 = RandomForestRegressor(n_estimators=1000,max_depth=12,min_samples_split=5,min_samples_leaf=5,oob_score=True,random_state=50)
 model2 = XGBRegressor(n_estimators=5000,learning_rate=0.01,early_stopping_round=10,max_depth=4,min_child_weight=0,gamma=0.6,verbose=False,random_state=50)
 model3= LGBMRegressor(objective='regression',
num_leaves=6,
learning_rate=0.01,
n_estimators=5000,
max_bin=200,
bagging_fraction=0.8,
bagging_freq=4,
bagging_seed=8,
feature_fraction=0.2,
feature_fraction_seed=8,
verbose=-1,
random_state=50 )
submission = pd.DataFrame({
"ImageId": mnist_test.index+1,
"Label": predictions_test
})
 submission.to_csv('my_submission.csv', index=False )<load_from_csv>
def cv(model,x=train):
er = -1*cross_val_score(model,x,label,cv=10,scoring='neg_mean_absolute_error')
 rmse=np.sqrt(er)
 return rmse
dataset_train = pd.read_csv("/kaggle/input/titanic/train.csv")
 dataset_train.head()<load_from_csv>
score1=cv(model1)
 Rf=[score1.mean() ,score1.std() ]
Rf
dataset_test = pd.read_csv("/kaggle/input/titanic/test.csv")
 dataset_test.head()<load_from_csv>
score2=cv(model2)
 Xg_mean=score2.mean()
Xg_std=score2.std()
Xg=[Xg_mean,Xg_std]
Xg
gender = pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
 gender.head()<count_missing_values>
score3=cv(model3)
 Lg_mean=score3.mean()
Lg_std=score3.std()
Lg=[Lg_mean,Lg_std]
Lg
dataset_train.isnull().sum()<count_missing_values>
model2.fit(train,label )
dataset_test.isnull().sum()<feature_engineering>
pred= model2.predict(test )
dataset_train['Age'] = dataset_train['Age'].fillna(dataset_train['Age'].mean())
 mean_train = int(dataset_train['Age'].mean())
 mean_test = int(dataset_test['Age'].mean())
 dataset_train['Embarked'] = dataset_train['Embarked'].fillna(np.random.choice(['C','Q','S']))
dataset_test['Age'] = dataset_test['Age'].fillna(dataset_test['Age'].mean())
 dataset_test['Fare'] = dataset_test['Fare'].fillna(dataset_test['Fare'].mean() )<count_values>
sample=pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv' )
temp_title.value_counts()<categorify>
Id= np.array(range(1461,2920))
fare_cabin = dataset_train[['Cabin', 'Fare']].groupby(by = 'Cabin' ).mean()
fare_cabin = fare_cabin.sort_values(by = 'Fare', ascending = False)
 fare_cabin = fare_cabin[:16][:]
fare_cabin = fare_cabin.reset_index()
fare_cabin = fare_cabin.sort_values(by = 'Cabin', ascending = True)
 def label_encoder(string):
num = re.findall('\d+', string)
 alpha = string[0]
if len(num)>1:
label = label = alpha + '(' + '-'.join(num)+ ')'
else:
label = alpha + '-'.join(num)
 return label
fare_cabin['Cabin'] = fare_cabin['Cabin'].apply(label_encoder )<categorify>
out1 =pd.DataFrame({"Id":Id,"SalePrice": pred})
 out1
<categorify><EOS> 
out1.to_csv('my_submission.csv', index=False )