Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<save_to_csv>
!pip install pytorchcv
titanic_submission = pd.DataFrame({
"PassengerId": b["PassengerId"],
"Survived": final_pred
})
 titanic_submission.to_csv("titanic.csv", index=False )<set_options>
!pip install fastai==1.0.47
warnings.filterwarnings(action='ignore', category=DataConversionWarning)
 warnings.filterwarnings(action='ignore', category=DeprecationWarning)
 warnings.filterwarnings(action='ignore', category=FutureWarning )<load_from_csv>
from pathlib import Path
import fastai
from fastai.vision import *
from pytorchcv.model_provider import get_model as ptcv_get_model
train = pd.read_csv(".. /input/train.csv")
 compe = pd.read_csv(".. /input/test.csv")
 sample_sub = pd.read_csv(".. /input/sample_submission.csv")
 data = train.append(compe, sort=False )<create_dataframe>
PATH = Path('.. /input/train/')
 np.random.seed(42 )
types = pd.DataFrame(data.dtypes ).rename(columns={0: 'type'} ).sort_values(by=['type'],ascending=False)
 types<count_missing_values>
batch_size = 128
def check_missing(df):
null_val = df.isnull().sum()
percent = 100 * df.isnull().sum() /len(df)
 missing_table = pd.concat([null_val, percent], axis=1)
 col = missing_table.rename(columns = {0 : 'Num', 1 : 'Rate'})
 return col
print("Data
cols = check_missing(data)
 types.join(cols ).sort_values(by="Rate", ascending=False )<drop_column>
tfms = get_transforms(flip_vert=True,max_lighting=0.5,max_rotate=360.,max_warp=0.3 )
data.drop(['PoolQC', 'MiscFeature', 'Alley', 'Fence', 'FireplaceQu'], axis=1, inplace = True )<define_variables>
data =(src.transform(tfms,size=224)
.databunch(bs=batch_size)
.normalize(imagenet_stats))
categorical_variables_string = \
['MSZoning', 'Street', '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', 'GarageType',
'GarageFinish', 'GarageQual', 'GarageCond', 'PavedDrive',
'SaleType', 'SaleCondition']
for v in categorical_variables_string:
data[v] = data[v].fillna(data[v].mode() [0])
 data = pd.get_dummies(data, columns=[v], drop_first=True)
<define_variables>
def md(f=None):
mdl = ptcv_get_model('condensenet74_c4_g4', pretrained=True)
 mdl.features.final_pool = nn.AvgPool2d(kernel_size=7, stride=1, padding=3)
 return mdl
categorical_variables_int = \
['OverallQual', 'OverallCond', 'MoSold']<define_variables>
learn = cnn_learner(data, md, metrics=accuracy, path='./' )
numerical_variavles = \
['MSSubClass', 'LotFrontage', 'LotArea', 'YearBuilt',
'YearRemodAdd', 'MasVnrArea', 'BsmtFinSF1', 'BsmtFinSF2',
'BsmtUnfSF', 'TotalBsmtSF', '1stFlrSF', '2ndFlrSF',
'LowQualFinSF', 'GrLivArea', 'BsmtFullBath', 'BsmtHalfBath',
'FullBath', 'HalfBath', 'BedroomAbvGr', 'KitchenAbvGr',
'TotRmsAbvGrd', 'Fireplaces', 'GarageYrBlt', 'GarageCars',
'GarageArea', 'WoodDeckSF', 'OpenPorchSF', 'EnclosedPorch',
'3SsnPorch', 'ScreenPorch', 'PoolArea', 'MiscVal', 'YrSold']
ss = StandardScaler()
for v in numerical_variavles:
data[v] = data[v].fillna(data[v].mean())
 data[v] = ss.fit_transform(data[[v]] )<prepare_x_and_y>
learn.fit_one_cycle(5,slice(1e-2))
train = data[:1460]
test = data[1460:]<features_selection>
learn.fit_one_cycle(5,slice(1e-6))
possible_features = train.columns.copy().drop('SalePrice' ).drop('Id')
 selector = SelectKBest(f_regression, len(possible_features))
selector.fit(train[possible_features], train['SalePrice'])
 scores = -np.log10(selector.pvalues_)
 indices = np.argsort(scores)[::-1]
print('Feature importances:')
 for i in range(len(scores)) :
print('%.2f %s' %(scores[indices[i]], possible_features[indices[i]]))<train_model>
test = ImageList.from_folder('.. /input/test/' )
fparams = \
['OverallQual', 'GrLivArea', 'GarageCars', 'GarageArea', 'TotalBsmtSF',
'1stFlrSF', 'ExterQual_TA', 'FullBath', 'TotRmsAbvGrd', 'YearBuilt',
'KitchenQual_TA', 'GarageFinish_Unf', 'YearRemodAdd', 'BsmtQual_TA',
'Foundation_PConc', 'MasVnrArea', 'GarageYrBlt', 'Fireplaces',
'ExterQual_Gd', 'BsmtFinType1_GLQ', 'Neighborhood_NridgHt', 'BsmtFinSF1',
'MasVnrType_None', 'SaleType_New', 'GarageType_Detchd', 'SaleCondition_Partial',
'Foundation_CBlock', 'LotFrontage', 'MasVnrType_Stone', 'Neighborhood_NoRidge',
'WoodDeckSF', 'KitchenQual_Gd', 'BsmtExposure_No', '2ndFlrSF', 'OpenPorchSF',
'HeatingQC_TA', 'BsmtExposure_Gd', 'Exterior2nd_VinylSd', 'Exterior1st_VinylSd',
'MSZoning_RM', 'HalfBath', 'LotShape_Reg', 'LotArea', 'CentralAir_Y', 'MSZoning_RL',
'HouseStyle_2Story', 'SaleType_WD', 'Electrical_SBrkr', 'RoofStyle_Hip', 'GarageType_BuiltIn',
'BsmtQual_Gd', 'GarageType_Attchd', 'PavedDrive_Y', 'BsmtFullBath', 'RoofStyle_Gable',
'Neighborhood_StoneBr', 'BsmtUnfSF', 'MasVnrType_BrkFace', 'Neighborhood_OldTown',
'Neighborhood_NAmes', 'Neighborhood_Edwards', 'GarageFinish_RFn', 'RoofMatl_WdShngl',
'BedroomAbvGr', 'Exterior1st_MetalSd', 'Neighborhood_IDOTRR', 'Exterior2nd_MetalSd',
'Exterior2nd_Wd Sdng', 'Exterior1st_Wd Sdng', 'KitchenQual_Fa', 'SaleCondition_Normal',
'Neighborhood_BrkSide', 'LotConfig_CulDSac', 'Neighborhood_Somerst', 'ExterCond_Fa',
'GarageCond_TA', 'KitchenAbvGr', 'BsmtFinType1_Rec', 'HeatingQC_Gd', 'HeatingQC_Fa',
'Exterior1st_CemntBd', 'GarageQual_Fa', 'BsmtFinType1_Unf', 'BsmtFinType1_BLQ',
'GarageCond_Fa', 'BsmtQual_Fa', 'EnclosedPorch', 'Neighborhood_Sawyer', 'Exterior2nd_CmentBd',
'Electrical_FuseF', 'Neighborhood_Timber', 'LotShape_IR2', 'LandContour_HLS', 'Foundation_Slab',
'Condition1_Feedr', 'Functional_Typ', 'ExterQual_Fa', 'BldgType_Duplex', 'Condition1_Norm',
'Neighborhood_MeadowV', 'ScreenPorch', 'ExterCond_TA', 'RoofMatl_CompShg', 'Neighborhood_BrDale',
'BldgType_Twnhs', 'BldgType_2fmCon', 'GarageQual_TA', 'Exterior1st_HdBoard', 'HouseStyle_SFoyer',
'Heating_GasA', 'PoolArea', 'Heating_Grav', 'MSZoning_FV', 'BsmtCond_Gd', 'PavedDrive_P',
'HouseStyle_1.5Unf', 'BsmtFinType1_LwQ', 'MSSubClass', 'LotConfig_Inside', 'OverallCond',
'Exterior2nd_ImStucc', 'Neighborhood_CollgCr', 'Functional_Min2', 'Neighborhood_Crawfor',
'GarageType_CarPort', 'Functional_Maj2', 'Exterior2nd_HdBoard']
train_target = train["SalePrice"].values
train_features = train[fparams].values
test_features = test[fparams].values<train_on_grid>
learn = load_learner(path='./', test=test)
 preds, _ = learn.get_preds(ds_type=DatasetType.Test )
svrgs_parameters = {
'kernel': ['rbf'],
'C': [150000,200000,250000],
'gamma': [0.004,0.0045,0.005]
}
svr_cv = GridSearchCV(svm.SVR() , svrgs_parameters, cv=8, scoring= 'neg_mean_squared_log_error')
 svr_cv.fit(train_features, train_target)
 print("SVR GridSearch score: "+str(svr_cv.best_score_))
print("SVR GridSearch params: ")
 print(svr_cv.best_params_ )<save_to_csv>
labelled_preds = [learn.data.classes[np.argmax(pred)] for pred in preds]
prediction = svr_cv.best_estimator_.predict(test_features)
 pred = pd.DataFrame(pd.read_csv(".. /input/test.csv")['Id'])
 pred['SalePrice'] = prediction
pred.to_csv(".. /working/submission.csv", index = False )<set_options>
fnames = [f.name for f in learn.data.test_ds.items]
warnings.filterwarnings("ignore")
 np.random.seed(2019)
 %matplotlib inline
print("done" )<load_from_csv>
df = pd.DataFrame({'file':fnames, 'species':labelled_preds}, columns=['file', 'species'] )
def read_and_concat_dataset(training_path, test_path):
train = pd.read_csv(training_path)
 train['train'] = 1
test = pd.read_csv(test_path)
 test['train'] = 0
data = train.append(test, ignore_index=True)
 return train, test, data
train, test, data = read_and_concat_dataset('.. /input/train.csv', '.. /input/test.csv')
 data = data.set_index('Id' )<filter>
df.to_csv('submission.csv', index=False )