Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<choose_model_class>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from statsmodels.stats.outliers_influence import variance_inflation_factor
from scipy import stats
from scipy.stats import skew, boxcox_normmax
from scipy.special import boxcox1p
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
import xgboost as xgb
from xgboost.sklearn import XGBRegressor
from xgboost import plot_importance
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import RandomForestRegressor
import lightgbm as lgb
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import LassoCV
from sklearn.linear_model import ElasticNetCV
from sklearn.svm import SVR
from mlxtend.regressor import StackingCVRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn import metrics
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
import math
from math import sqrt
from sklearn.metrics import r2_score
def get_model(use_model):
base_model = use_model(weights='imagenet', include_top=False, pooling='avg', input_shape=(*IMAGE_SIZE, 3))
x = base_model.output
predictions = Dense(104, activation='softmax' )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 model.compile(optimizer='nadam', loss = 'sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])
 return model
with strategy.scope() :
model2 = get_model(InceptionV3)
 model1.load_weights("/kaggle/input/start-with-pre-train/my_ef_net_b7.h5" )<predict_on_test>
if not sys.warnoptions:
warnings.simplefilter("ignore" )
val_dataset = get_validation_dataset()
images_ds = val_dataset.map(lambda image, label: image)
 labels_ds = val_dataset.map(lambda image, label: label ).unbatch()
val_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
m1 = model1.predict(images_ds)
 m2 = model2.predict(images_ds)
 scores = []
for alpha in np.linspace(0,1,100):
val_probabilities = alpha*m1+(1-alpha)*m2
val_predictions = np.argmax(val_probabilities, axis=-1)
 scores.append(f1_score(val_labels, val_predictions, labels=range(104), average='macro'))
best_alpha = np.argmax(scores)/100
print('Best alpha: ' + str(best_alpha))<predict_on_test>
house_data = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 house_data_test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )
test_ds = get_test_dataset(ordered=True)
 print('Вычисляем предсказания...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities1 = model1.predict(test_images_ds)
 probabilities2 = model2.predict(test_images_ds)
 probabilities = best_alpha * probabilities1 +(1 - best_alpha)* probabilities2
predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Создание файла submission.csv...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='' )<install_modules>
house_data[(house_data['GrLivArea']>5000)&(house_data['SalePrice']<200000)]
!pip install -q efficientnet<set_options>
house_data = house_data.drop(house_data[(house_data['GrLivArea']>5000)&(house_data['SalePrice']<200000)].index )
%matplotlib inline
print("Tensorflow version " + tf.__version__ )<define_variables>
house_all_data = pd.concat([house_data, house_data_test], axis=0)
 house_all_data[:1459]
GCS_DS_PATH = KaggleDatasets().get_gcs_path("tpu-getting-started" )<define_variables>
null_list = house_all_data.isnull().sum()
def find_null_values(list):
for name, value in list.items() :
if value != 0:
print(name, value)
 print(find_null_values(null_list))
IMAGE_SIZE = [512, 512]
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
GCS_PATH_SELECT = {
192: GCS_DS_PATH + '/tfrecords-jpeg-192x192',
224: GCS_DS_PATH + '/tfrecords-jpeg-224x224',
331: GCS_DS_PATH + '/tfrecords-jpeg-331x331',
512: GCS_DS_PATH + '/tfrecords-jpeg-512x512'
}
GCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]]
VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')
 SEED = 2020<data_type_conversions>
house_all_data['LotFrontage'] = house_all_data.groupby('Neighborhood')['LotFrontage'].transform(
 lambda x: x.fillna(x.median()))
for col in('MasVnrArea', 'GarageYrBlt'):
house_all_data[col] = house_all_data[col].fillna(0)
 for col in('LotFrontage', 'MasVnrArea', 'GarageYrBlt'):
house_all_data[col] = house_all_data[col].astype(int)
 for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF','BsmtFullBath', 'BsmtHalfBath',
'GarageCars','GarageArea'):
house_all_data[col] = house_all_data[col].fillna(0)
 for col in('BsmtQual', 'BsmtCond', 'BsmtExposure','BsmtFinType1','BsmtFinType2', 'FireplaceQu','GarageType',
'GarageFinish','GarageQual','GarageCond', 'Fence', 'Alley', 'PoolQC', 'MiscFeature'):
house_all_data[col] = house_all_data[col].fillna('not_applicable' )
def decode_image(image_data):

 image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['id']
return image, idnum
def load_dataset(filenames, labeled=True, ordered=False):

 ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset
def get_validation_dataset() :
dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=True)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 NUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 print('Dataset: {} validation images, {} unlabeled test images'.format(NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))<choose_model_class>
house_all_data['Utilities'].value_counts()
def get_model(use_model):
base_model = use_model(weights='imagenet',
include_top=False, pooling='avg',
input_shape=(*IMAGE_SIZE, 3))
x = base_model.output
predictions = Dense(104, activation='softmax' )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 model.compile(
 optimizer='nadam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 return model
with strategy.scope() :
model1 = get_model(ResNet152V2)
 model1.load_weights("/kaggle/input/fork-of-start-with-pre-train-255147/my_ef_net_b7.h5" )<choose_model_class>
for col in('MSZoning', 'Exterior1st', 'Exterior2nd','MasVnrType','Electrical', 'KitchenQual',
'Functional','SaleType'):
house_all_data[col] = house_all_data[col].fillna(house_all_data[col].mode() [0] )
def get_model(use_model):
base_model = use_model(weights='noisy-student',
include_top=False, pooling='avg',
input_shape=(*IMAGE_SIZE, 3))
x = base_model.output
predictions = Dense(104, activation='softmax' )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 model.compile(
 optimizer='nadam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 return model
with strategy.scope() :
model2 = get_model(EfficientNetB7)
 model2.load_weights("/kaggle/input/more-data-with-efficientnetb7/my_ef_net_b7.h5" )<predict_on_test>
house_all_data['Total_SF'] = house_all_data['TotalBsmtSF'] + house_all_data['1stFlrSF'] + house_all_data['2ndFlrSF']
house_all_data['Additional_Area'] = house_all_data['GarageArea'] + house_all_data['PoolArea']
house_all_data['Total_Bathroom'] = house_all_data['BsmtFullBath'] + house_all_data['BsmtHalfBath'] + \
house_all_data['FullBath'] + house_all_data['HalfBath']
house_all_data['Total_PorchSF'] = house_all_data['OpenPorchSF'] + house_all_data['EnclosedPorch'] + \
house_all_data['3SsnPorch'] + house_all_data['ScreenPorch'] + \
house_all_data['WoodDeckSF']
val_dataset = get_validation_dataset()
images_ds = val_dataset.map(lambda image, label: image)
 labels_ds = val_dataset.map(lambda image, label: label ).unbatch()
val_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
m1 = model1.predict(images_ds)
 m2 = model2.predict(images_ds)
 scores = []
for alpha in np.linspace(0,1,100):
val_probabilities = alpha*m1+(1-alpha)*m2
val_predictions = np.argmax(val_probabilities, axis=-1)
 scores.append(f1_score(val_labels, val_predictions, labels=range(104), average='macro'))
best_alpha = np.argmax(scores)/100
print('Best alpha: ' + str(best_alpha))<predict_on_test>
house_all_data['remodel_year'] = house_all_data['YearRemodAdd'] - house_all_data['YearBuilt']
house_all_data ['Remodel_yn'] = house_all_data['remodel_year'].apply(lambda x: 1 if x > 0 else 0)
 house_all_data['Fireplace_yn'] = house_all_data['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
 house_all_data['Pool_yn'] = house_all_data['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 house_all_data['Basement_yn'] = house_all_data['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 house_all_data['Garage_yn'] = house_all_data['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 house_all_data['Garage_car_yn'] = house_all_data['GarageCars'].apply(lambda x: 1 if x > 0 else 0)
 house_all_data['2ndfloor_yn'] = house_all_data['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0 )
test_ds = get_test_dataset(ordered=True)
 print('Вычисляем предсказания...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities1 = model1.predict(test_images_ds)
 probabilities2 = model2.predict(test_images_ds)
 probabilities = best_alpha * probabilities1 +(1 - best_alpha)* probabilities2
predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Создание файла submission.csv...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='' )<install_modules>
house_all_data[house_all_data['remodel_year'] < 0]
!pip install -q efficientnet<set_options>
house_all_data.loc[house_all_data['remodel_year'] == -1, 'remodel_year'] = 1
%matplotlib inline
print("Tensorflow version " + tf.__version__ )<define_variables>
house_all_data.loc[house_all_data['Id'] == 1877, 'Remodel_yn'] = 1
GCS_DS_PATH = KaggleDatasets().get_gcs_path("tpu-getting-started" )<define_variables>
house_all_data.loc[416]
IMAGE_SIZE = [512, 512]
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
GCS_PATH_SELECT = {
192: GCS_DS_PATH + '/tfrecords-jpeg-192x192',
224: GCS_DS_PATH + '/tfrecords-jpeg-224x224',
331: GCS_DS_PATH + '/tfrecords-jpeg-331x331',
512: GCS_DS_PATH + '/tfrecords-jpeg-512x512'
}
GCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]]
VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')
 SEED = 2020<data_type_conversions>
house_all_data = house_all_data.drop([ 'Id', 'Utilities','PoolQC', 'Heating', 'RoofMatl', 'Condition2', 'Street'], axis=1 )
def decode_image(image_data):

 image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['id']
return image, idnum
def load_dataset(filenames, labeled=True, ordered=False):

 ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset
def get_validation_dataset() :
dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=True)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 NUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 print('Dataset: {} validation images, {} unlabeled test images'.format(NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))<choose_model_class>
for col in ['MSSubClass', 'OverallCond', 'YrSold', 'MoSold']:
house_all_data[col] = house_all_data[col].astype('str' )
def get_model(use_model):
base_model = use_model(weights='imagenet',
include_top=False, pooling='avg',
input_shape=(*IMAGE_SIZE, 3))
x = base_model.output
predictions = Dense(104, activation='softmax' )(x)
 return Model(inputs=base_model.input, outputs=predictions)
 with strategy.scope() :
model1 = get_model(MobileNet)
 model1.load_weights("/kaggle/input/rashirdan24-11-start-with-pre-train-mobilenet/my_ef_net_b7.h5" )<choose_model_class>
label_encoding_feat =('ExterQual', 'ExterCond', 'BsmtQual','BsmtCond', 'BsmtExposure', 'BsmtFinType1',
'BsmtFinType2','HeatingQC', 'KitchenQual', 'FireplaceQu', 'GarageQual','GarageCond',
'LotShape', 'PavedDrive', 'CentralAir', 'MSSubClass', 'OverallCond', 'YrSold','MoSold',
'Remodel_yn','Fireplace_yn','Pool_yn','Basement_yn', 'Garage_yn', '2ndfloor_yn', 'Garage_car_yn')
 for feat in label_encoding_feat:
labelencoder = LabelEncoder()
house_all_data[feat] = labelencoder.fit_transform(( house_all_data[feat].values))
def get_model(use_model):
base_model = use_model(weights='imagenet',
include_top=False, pooling='avg',
input_shape=(*IMAGE_SIZE, 3))
x = base_model.output
predictions = Dense(104, activation='softmax' )(x)
 return Model(inputs=base_model.input, outputs=predictions)
 with strategy.scope() :
model2 = get_model(EfficientNetB1)
 model2.load_weights("/kaggle/input/rashirdan-2-start-with-pre-train-efficientnetb1/my_ef_net_b7.h5" )<predict_on_test>
def one_hot_encoding(dataset, feature):
dummy_dataset = pd.get_dummies(dataset[[feature]])
 encode_dataset = pd.concat([dataset, dummy_dataset], axis=1)
 encode_dataset = encode_dataset.drop([feature], axis=1)
 return(encode_dataset )
val_dataset = get_validation_dataset()
images_ds = val_dataset.map(lambda image, label: image)
 labels_ds = val_dataset.map(lambda image, label: label ).unbatch()
val_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
m1 = model1.predict(images_ds)
 m2 = model2.predict(images_ds)
 scores = []
for alpha in np.linspace(0,1,100):
val_probabilities = alpha*m1+(1-alpha)*m2
val_predictions = np.argmax(val_probabilities, axis=-1)
 scores.append(f1_score(val_labels, val_predictions, labels=range(104), average='macro'))
best_alpha = np.argmax(scores)/100
print('Best alpha: ' + str(best_alpha))<predict_on_test>
encoding_features = ['MSZoning', 'LandContour', 'LotConfig','BldgType', 'HouseStyle','RoofStyle',
'MasVnrType', 'Foundation', 'Electrical','GarageType','SaleCondition', 'SaleType',
'Functional','GarageFinish', 'LandSlope', 'Condition1', 'Neighborhood','Exterior1st',
'Exterior2nd', 'Fence', 'Alley', 'MiscFeature' ]
for feature in encoding_features:
house_all_data = one_hot_encoding(house_all_data, feature)
test_ds = get_test_dataset(ordered=True)
 best_alpha = 0.48
print('Вычисляем предсказания...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities1 = model1.predict(test_images_ds)
 probabilities2 = model2.predict(test_images_ds)
 probabilities = best_alpha * probabilities1 +(1 - best_alpha)* probabilities2
predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Создание файла submission.csv...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='' )<install_modules>
house_numerical_data = house_all_data[['BsmtFinSF1','BsmtFinSF2','BsmtUnfSF','TotalBsmtSF','BsmtFullBath',
'BsmtHalfBath','GarageCars','GarageArea','Total_SF','Additional_Area','LotFrontage',
'LotArea','OverallQual','YearBuilt','YearRemodAdd','MasVnrArea','1stFlrSF',
'2ndFlrSF','LowQualFinSF','GrLivArea','FullBath','HalfBath','BedroomAbvGr',
'KitchenAbvGr','TotRmsAbvGrd','Fireplaces','GarageYrBlt','WoodDeckSF',
'OpenPorchSF','EnclosedPorch', '3SsnPorch','ScreenPorch','PoolArea','MiscVal',
'Total_Bathroom','Total_PorchSF','remodel_year','SalePrice']]
!pip install efficientnet --quiet 
<find_best_params>
corr_mask_numerical = corr.mask(mask)
 corr_values_numerical = [c for c in corr_mask_numerical.columns if any(corr_mask_numerical[c] > 0.90)]
print(corr_values_numerical )
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<define_variables>
house_all_data['SalePrice_log'] = np.log1p(house_all_data['SalePrice'] )
GCS_DS_PATH = KaggleDatasets().get_gcs_path('tpu-getting-started')
 print(GCS_DS_PATH )<define_variables>
house_numerical_data = house_numerical_data.drop(['SalePrice'], axis=1)
 skew_features = house_numerical_data.apply(lambda x: skew(x.dropna())).sort_values(ascending=False)
 high_skew = skew_features[skew_features > 0.5]
IMAGE_SIZE = [224, 224]
GCS_PATH = GCS_DS_PATH + '/tfrecords-jpeg-224x224'
AUTO = tf.data.experimental.AUTOTUNE
TRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')
 VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')
 CLASSES = ['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea', 'wild geranium', 'tiger lily', 'moon orchid', 'bird of paradise', 'monkshood', 'globe thistle',
'snapdragon', "colt's foot", 'king protea', 'spear thistle', 'yellow iris', 'globe-flower', 'purple coneflower', 'peruvian lily', 'balloon flower', 'giant white arum lily',
'fire lily', 'pincushion flower', 'fritillary', 'red ginger', 'grape hyacinth', 'corn poppy', 'prince of wales feathers', 'stemless gentian', 'artichoke', 'sweet william',
'carnation', 'garden phlox', 'love in the mist', 'cosmos', 'alpine sea holly', 'ruby-lipped cattleya', 'cape flower', 'great masterwort', 'siam tulip', 'lenten rose',
'barberton daisy', 'daffodil', 'sword lily', 'poinsettia', 'bolero deep blue', 'wallflower', 'marigold', 'buttercup', 'daisy', 'common dandelion',
'petunia', 'wild pansy', 'primula', 'sunflower', 'lilac hibiscus', 'bishop of llandaff', 'gaura', 'geranium', 'orange dahlia', 'pink-yellow dahlia',
'cautleya spicata', 'japanese anemone', 'black-eyed susan', 'silverbush', 'californian poppy', 'osteospermum', 'spring crocus', 'iris', 'windflower', 'tree poppy',
'gazania', 'azalea', 'water lily', 'rose', 'thorn apple', 'morning glory', 'passion flower', 'lotus', 'toad lily', 'anthurium',
'frangipani', 'clematis', 'hibiscus', 'columbine', 'desert-rose', 'tree mallow', 'magnolia', 'cyclamen ', 'watercress', 'canna lily',
'hippeastrum ', 'bee balm', 'pink quill', 'foxglove', 'bougainvillea', 'camellia', 'mallow', 'mexican petunia', 'bromelia', 'blanket flower',
'trumpet creeper', 'blackberry lily', 'common tulip', 'wild rose']<categorify>
skewness = pd.DataFrame({'Skew' :skew_features})
 skewness
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['id']
return image, idnum
def load_dataset(filenames, labeled=True, ordered=False):
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset<normalization>
for features in high_skew.index:
house_all_data[features]= boxcox1p(house_all_data[features], boxcox_normmax(house_all_data[features] + 1))
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = tf.reshape(tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = tf.reshape(tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3])
 zoom_matrix = tf.reshape(tf.concat([one/height_zoom,zero,zero, zero,one/width_zoom,zero, zero,zero,one],axis=0),[3,3])
 shift_matrix = tf.reshape(tf.concat([one,zero,height_shift, zero,one,width_shift, zero,zero,one],axis=0),[3,3])
 return K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))
def transform(image,label):
DIM = IMAGE_SIZE[0]
XDIM = DIM%2
rot = 15.* tf.random.normal([1],dtype='float32')
 shr = 5.* tf.random.normal([1],dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 w_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 h_shift = 16.* tf.random.normal([1],dtype='float32')
 w_shift = 16.* tf.random.normal([1],dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2,-DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2,DIM//2),[DIM])
 z = tf.ones([DIM*DIM],dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m,tf.cast(idx,dtype='float32'))
idx2 = K.cast(idx2,dtype='int32')
 idx2 = K.clip(idx2,-DIM//2+XDIM+1,DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image,tf.transpose(idx3))
return tf.reshape(d,[DIM,DIM,3]),label
def get_training_dataset() :
dataset = load_dataset(TRAINING_FILENAMES, labeled=True)
 dataset = dataset.map(transform, num_parallel_calls=AUTO)
 dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_validation_dataset(ordered=False):
dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
NUM_TRAINING_IMAGES = 12753
NUM_VALIDATION_IMAGES = 3712
NUM_TEST_IMAGES = 7382<train_model>
house_all_data= house_all_data.drop(['GarageArea','SalePrice'],axis=1 )
BATCH_SIZE = 32 * strategy.num_replicas_in_sync
ds_train = get_training_dataset()
ds_valid = get_validation_dataset()
ds_test = get_test_dataset()
print("Training:", ds_train)
 print("Validation:", ds_valid)
 print("Test:", ds_test )<choose_model_class>
train_house_data = house_all_data[:1459]
test_house_data = house_all_data.iloc[1459:]
with strategy.scope() :
pretrained_model1 = efficientnet.EfficientNetB7(
 weights='noisy-student',
include_top=False ,
input_shape=[*IMAGE_SIZE, 3])
 pretrained_model1.trainable = True
model1 = tf.keras.Sequential([
pretrained_model1,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 pretrained_model2 = tf.keras.applications.Xception(
 weights='imagenet',
include_top=False,
input_shape=[*IMAGE_SIZE,3])
 pretrained_model2.trainable = True
model2 = tf.keras.Sequential([
pretrained_model2,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 pretrained_model3 = tf.keras.applications.InceptionResNetV2(
 weights='imagenet',
include_top=False,
input_shape=[*IMAGE_SIZE,3])
 pretrained_model3.trainable = True
model3 = tf.keras.Sequential([
pretrained_model3,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
 pretrained_model4 = tf.keras.applications.DenseNet201(
 weights='imagenet',
include_top=False,
input_shape=[*IMAGE_SIZE,3])
 pretrained_model4.trainable = True
model4 = tf.keras.Sequential([
pretrained_model4,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ])
<choose_model_class>
test_house_data = test_house_data.drop(['SalePrice_log'],axis=1)
 test_house_data
model1.compile(
 optimizer='adam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'], 
)
 model2.compile(
 optimizer='adam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'], 
)
 model3.compile(
 optimizer='adam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'], 
)
 model4.compile(
 optimizer='adam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'],
)<choose_model_class>
train_data, label_data = train_house_data.iloc[:,:-1],train_house_data.iloc[:,-1]
def exponential_lr(epoch,
start_lr = 0.00001, min_lr = 0.00001, max_lr = 0.00005,
rampup_epochs = 5, sustain_epochs = 0,
exp_decay = 0.8):
def lr(epoch, start_lr, min_lr, max_lr, rampup_epochs, sustain_epochs, exp_decay):
if epoch < rampup_epochs:
lr =(( max_lr - start_lr)/ rampup_epochs * epoch + start_lr)
 elif epoch < rampup_epochs + sustain_epochs:
lr = max_lr
else:
lr =(( max_lr - min_lr)* exp_decay**(epoch - rampup_epochs - sustain_epochs)+ min_lr)
 return lr
return lr(epoch,start_lr,min_lr,max_lr,rampup_epochs,sustain_epochs,exp_decay)
 lr_callback = tf.keras.callbacks.LearningRateScheduler(exponential_lr, verbose=True )<train_model>
X_train, X_test, y_train, y_test = train_test_split(
 train_data, label_data, test_size=0.3,random_state=42 )
EPOCHS = 15
STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
history1 = model1.fit(
 ds_train,
validation_data=ds_valid,
epochs=EPOCHS,
steps_per_epoch=STEPS_PER_EPOCH,
callbacks=[lr_callback],
)<train_model>
kfold_cv=KFold(n_splits=15, random_state=42, shuffle=True)
 for train_index, test_index in kfold_cv.split(train_data,label_data):
X_train, X_test = train_data.iloc[train_index], train_data.iloc[test_index]
y_train, y_test = label_data.iloc[train_index], label_data.iloc[test_index]
history2 = model2.fit(
 ds_train,
validation_data=ds_valid,
epochs=EPOCHS,
steps_per_epoch=STEPS_PER_EPOCH,
callbacks=[lr_callback],
)<train_model>
model_xgb= XGBRegressor(colsample_bytree = 0.3,
learning_rate = 0.03,
max_depth = 5,
min_child_weight = 5,
n_estimators = 500,
objective ='reg:squarederror')
 model_xgb.fit(X_train, y_train)
 y_pred_xgbm = model_xgb.predict(X_test )
history3 = model3.fit(
 ds_train,
validation_data=ds_valid,
epochs=EPOCHS,
steps_per_epoch=STEPS_PER_EPOCH,
callbacks=[lr_callback],
)<train_model>
model_lgbm = lgb.LGBMRegressor(boosting_type ='gbdt',
colsample_bytree = 0.3,
learning_rate = 0.03,
n_estimators = 1000,
num_leaves = 6,
objective = 'regression',
reg_alpha = 1,
reg_lambda = 1.4)
 model_lgbm.fit(X_train, y_train)
 y_pred_lgbm = model_lgbm.predict(X_test )
history4 = model4.fit(
 ds_train,
validation_data=ds_valid,
epochs=EPOCHS,
steps_per_epoch=STEPS_PER_EPOCH,
callbacks=[lr_callback],
)<predict_on_test>
model_gbr= GradientBoostingRegressor(learning_rate=0.01,
max_depth=6,
max_features=0.1,
min_samples_leaf=30,
n_estimators=1000)
 model_gbr.fit(X_train, y_train)
 y_pred_gbr = model_gbr.predict(X_test )
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities1 = model1.predict(test_images_ds)
 probabilities2 = model2.predict(test_images_ds)
 probabilities3 = model3.predict(test_images_ds)
 probabilities4 = model4.predict(test_images_ds)
 probabilities = probabilities1+probabilities2+probabilities3+probabilities4
predictions = np.argmax(probabilities, axis=-1)
 print(predictions )<save_to_csv>
ridge_alpha = [1e-15, 1e-10, 1e-8, 9e-4, 7e-4, 5e-4, 3e-4, 1e-4, 1e-3, 5e-2, 1e-2, 0.1, 0.3,
1, 3, 5, 10, 15, 18, 20, 30, 50, 75, 100]
lasso_alpha = [5e-05, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008]
enet_alpha = [0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007]
print('Generating submission.csv file...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt(
 'submission.csv',
np.rec.fromarrays([test_ids, predictions]),
fmt=['%s', '%d'],
delimiter=',',
header='id,label',
comments='', 
)
 !head submission.csv<install_modules>
rr = make_pipeline(RobustScaler() , RidgeCV(alphas = ridge_alpha, cv=10))
rr.fit(X_train, y_train)
 pred_ridge= rr.predict(X_test )
!pip install -q efficientnet<set_options>
model_lasso = make_pipeline(RobustScaler() , LassoCV(alphas = lasso_alpha, cv=10))
model_lasso.fit(X_train, y_train)
 pred_lasso= model_lasso.predict(X_test )
%matplotlib inline
print("Tensorflow version " + tf.__version__ )<define_variables>
model_enet = make_pipeline(RobustScaler() ,
ElasticNetCV(fit_intercept=True, alphas=enet_alpha, cv=10))
model_enet.fit(X_train, y_train)
 pred_enet= model_enet.predict(X_test )
GCS_DS_PATH = KaggleDatasets().get_gcs_path("tpu-getting-started" )<define_variables>
model_svr = make_pipeline(RobustScaler() , SVR(C= 20, epsilon= 0.008, gamma=0.0001))
model_svr.fit(X_train, y_train)
 pred_svr= model_svr.predict(X_test )
IMAGE_SIZE = [512, 512]
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
GCS_PATH_SELECT = {
192: GCS_DS_PATH + '/tfrecords-jpeg-192x192',
224: GCS_DS_PATH + '/tfrecords-jpeg-224x224',
331: GCS_DS_PATH + '/tfrecords-jpeg-331x331',
512: GCS_DS_PATH + '/tfrecords-jpeg-512x512'
}
GCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]]
VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')
 SEED = 2020<data_type_conversions>
y_pred_xgbm_train = model_xgb.predict(X_train)
 y_pred_lgbm_train = model_lgbm.predict(X_train)
 y_pred_gbr_train = model_gbr.predict(X_train)
 pred_ridge_train= rr.predict(X_train)
 pred_lasso_train= model_lasso.predict(X_train)
 pred_enet_train= model_enet.predict(X_train)
 pred_svr_train= model_svr.predict(X_train )
def decode_image(image_data):

 image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['id']
return image, idnum
def load_dataset(filenames, labeled=True, ordered=False):

 ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset
def get_validation_dataset() :
dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=True)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 NUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 print('Dataset: {} validation images, {} unlabeled test images'.format(NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))<choose_model_class>
print("Train Error" +", " + 'XGBM– ' + str(math.sqrt(metrics.mean_squared_error(y_train, y_pred_xgbm_train))))
print("Train Error" +", " + 'LGBM– ' + str(math.sqrt(metrics.mean_squared_error(y_train, y_pred_lgbm_train))))
print("Train Error" +", " + 'GBR– ' + str(math.sqrt(metrics.mean_squared_error(y_train, y_pred_gbr_train))))
print("Train Error" +", " + 'Ridge– ' + str(math.sqrt(metrics.mean_squared_error(y_train, pred_ridge_train))))
print("Train Error" +", " + 'Lasso– ' + str(math.sqrt(metrics.mean_squared_error(y_train, pred_lasso_train))))
print("Train Error" +", " + 'Elastic Net– ' + str(math.sqrt(metrics.mean_squared_error(y_train, pred_enet_train))))
print("Train Error" +", " + 'SVR– ' + str(math.sqrt(metrics.mean_squared_error(y_train, pred_svr_train))))
def get_model(use_model):
base_model = use_model(weights='imagenet',
include_top=False, pooling='avg',
input_shape=(*IMAGE_SIZE, 3))
x = base_model.output
predictions = Dense(104, activation='softmax' )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 model.compile(
 optimizer='nadam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 return model
with strategy.scope() :
model1 = get_model(DenseNet201)
 model1.load_weights("/kaggle/input/start-with-densenet201/my_densenet_201.h5" )<choose_model_class>
print("Test Error" +", " + 'XGBM– ' + str(math.sqrt(metrics.mean_squared_error(y_test, y_pred_xgbm))))
print("Test Error" +", " + 'LGBM– ' + str(math.sqrt(metrics.mean_squared_error(y_test, y_pred_lgbm))))
print("Test Error" +", " + 'GBR– ' + str(math.sqrt(metrics.mean_squared_error(y_test, y_pred_gbr))))
print("Test Error" +", " + 'Ridge– ' + str(math.sqrt(metrics.mean_squared_error(y_test, pred_ridge))))
print("Test Error" +", " + 'Lasso– ' + str(math.sqrt(metrics.mean_squared_error(y_test, pred_lasso))))
print("Test Error" +", " + 'Elastic Net– ' + str(math.sqrt(metrics.mean_squared_error(y_test, pred_enet))))
print("Test Error" +", " + 'SVR– ' + str(math.sqrt(metrics.mean_squared_error(y_test, pred_svr))))
def get_model(use_model):
base_model = use_model(weights='noisy-student',
include_top=False, pooling='avg',
input_shape=(*IMAGE_SIZE, 3))
x = base_model.output
predictions = Dense(104, activation='softmax' )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 model.compile(
 optimizer='nadam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 return model
with strategy.scope() :
model2 = get_model(EfficientNetB7)
 model2.load_weights("/kaggle/input/start-with-pre-train/my_ef_net_b7.h5" )<predict_on_test>
def rmse(prediction, target):
return str(math.sqrt(metrics.mean_squared_error(target, prediction)))
 keys = ['XGBM_train', 'LGBM_train', 'GBR_train', 'Ridge_train', 'Lasso_train', 'ElasticN_train','SVR_train',
'XGBM_test', 'LGBM_test', 'GBR_test', 'Ridge_test', 'Lasso_test', 'ElasticN_test', 'SVR_test']
values = [rmse(y_train, y_pred_xgbm_train),rmse(y_train, y_pred_lgbm_train), rmse(y_train, y_pred_gbr_train),
rmse(y_train, pred_ridge_train),rmse(y_train, pred_lasso_train), rmse(y_train, pred_enet_train),
rmse(y_train, pred_svr_train), rmse(y_test, y_pred_xgbm), rmse(y_test, y_pred_lgbm),
rmse(y_test, y_pred_gbr),rmse(y_test, pred_ridge),rmse(y_test, pred_lasso),
rmse(y_test, pred_enet),rmse(y_test, pred_svr)]
val_dataset = get_validation_dataset()
images_ds = val_dataset.map(lambda image, label: image)
 labels_ds = val_dataset.map(lambda image, label: label ).unbatch()
val_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
m1 = model1.predict(images_ds)
 m2 = model2.predict(images_ds)
 scores = []
for alpha in np.linspace(0,1,100):
val_probabilities = alpha*m1+(1-alpha)*m2
val_predictions = np.argmax(val_probabilities, axis=-1)
 scores.append(f1_score(val_labels, val_predictions, labels=range(104), average='macro'))
best_alpha = np.argmax(scores)/100
print('Best alpha: ' + str(best_alpha))<predict_on_test>
stack = StackingCVRegressor(regressors=(model_xgb, model_lgbm, model_gbr,rr, model_lasso, model_enet,model_svr),
meta_regressor=model_xgb, use_features_in_secondary=True)
 stack_model = stack.fit(X_train.values, y_train.values )
test_ds = get_test_dataset(ordered=True)
 print('Вычисляем предсказания...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities1 = model1.predict(test_images_ds)
 probabilities2 = model2.predict(test_images_ds)
 probabilities = best_alpha * probabilities1 +(1 - best_alpha)* probabilities2
predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Создание файла submission.csv...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='' )<import_modules>
kfold_cv=KFold(n_splits=10,random_state =42, shuffle=True)
 for train_index, test_index in kfold_cv.split(train_data,label_data):
X_train, X_test = train_data.iloc[train_index], train_data.iloc[test_index]
y_train, y_test = label_data.iloc[train_index], label_data.iloc[test_index]
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler<define_variables>
def blend_models_predict(X):
return(( 0.05 * model_xgb.predict(X)) + 
(0.1 * model_lgbm.predict(X)) + 
(0.1 * model_gbr.predict(X)) + 
(0.1 * rr.predict(X)) + 
(0.1 * model_lasso.predict(X)) + 
(0.1 * model_enet.predict(X)) + 
(0.1 * model_svr.predict(X)) + 
(0.35 * stack_model.predict(X.values)))
 blend_model_pred = blend_models_predict(X_test )
batch_size = 86
num_nets = 15<load_from_csv>
y_pred_blend = blend_models_predict(X_test)
 print('MAE: %2f'% mean_absolute_error(y_test, y_pred_blend))
print('RMSE: %2f'% np.sqrt(mean_squared_error(y_test, y_pred_blend)))
 print('R2 %2f' % r2_score(y_test, y_pred_blend))
digits_train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 digits_test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )<prepare_x_and_y>
print('Blend Model– ' + str(math.sqrt(metrics.mean_squared_error(y_test, blend_model_pred))))
X = digits_train.drop(columns="label" ).values.reshape(digits_train.shape[0],28,28,1)/ 255.0
Y = to_categorical(digits_train["label"], num_classes=10)
 X_test = digits_test.values.reshape(digits_test.shape[0],28,28,1)/ 255.0<choose_model_class>
test_data_predict = blend_models_predict(test_house_data)
 test_data_predict
datagen_train = ImageDataGenerator(
 rotation_range = 10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1
)<choose_model_class>
submission = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/sample_submission.csv')
 submission.iloc[:,1] = np.floor(np.expm1(blend_models_predict(test_house_data)) )
model = [0] * num_nets
for j in range(num_nets):
model[j] = Sequential()
model[j].add(Conv2D(32, kernel_size = 3, activation = "relu", input_shape =(28, 28, 1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 3, activation = "relu"))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 5, strides = 2, padding = "same", activation = "relu"))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64, kernel_size = 3, activation = "relu"))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 3, activation = "relu"))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 5, strides = 2, padding = "same", activation = "relu"))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(128, kernel_size = 4, activation = "relu"))
model[j].add(BatchNormalization())
 model[j].add(Flatten())
 model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation = "softmax"))
model[j].compile(optimizer = "adam", loss = "categorical_crossentropy", metrics = ["accuracy"] )<train_model>
submission.iloc[:,1] = np.floor(( np.floor(np.expm1(blend_models_predict(test_house_data)))) )
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 history = [0] * num_nets
epochs = 45
for j in range(num_nets):
x_train, x_val, y_train, y_val = train_test_split(X, Y, test_size = 0.1)
 history[j] = model[j].fit_generator(datagen_train.flow(x_train, y_train, batch_size = 64),
epochs = epochs,
steps_per_epoch = x_train.shape[0] // 64,
validation_data =(x_val, y_val),
callbacks = [annealer],
verbose = 0)
 print("CNN {0:d}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 j+1,epochs,max(history[j].history['accuracy']),max(history[j].history['val_accuracy'])) )<save_to_csv>
q1 = submission['SalePrice'].quantile(0.0042)
 q2 = submission['SalePrice'].quantile(0.99)
 submission['SalePrice'] = submission['SalePrice'].apply(lambda x: x if x > q1 else x*0.77)
 submission['SalePrice'] = submission['SalePrice'].apply(lambda x: x if x < q2 else x*1.1)
 submission.to_csv("submission.csv", index=False )