Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<prepare_output>
%matplotlib inline
model = Lasso(alpha=0.0005)
 model.fit(trainX,trainY)
 y_pred = model.predict(testX)
 y_pred = np.exp(y_pred)
 print(rmsle(testY, y_pred))
res = pd.DataFrame({"Model":['LASSO'],
"RMSLE": [rmsle(testY, y_pred)]})
 Results = Results.append(res )<predict_on_test>
species = ['Black-grass', 'Charlock', 'Cleavers', 'Common Chickweed', 'Common wheat', 'Fat Hen',
'Loose Silky-bent', 'Maize','Scentless Mayweed', 'Shepherds Purse',
'Small-flowered Cranesbill', 'Sugar beet']
data_dir = '.. /input/plant-seedlings-classification/'
train_dir = os.path.join(data_dir, 'train')
 test_dir = os.path.join(data_dir, 'test' )
X2 = sm.add_constant(trainX)
 o=0
for i in X2.columns:
o+=1
print(o)
 model = sm.OLS(trainY, X2.astype(float))
model = model.fit()
p_values = pd.DataFrame(model.pvalues)
 p_values = p_values.sort_values(by=0, ascending=False)
 if float(p_values.loc[p_values.index[0]])>=0.05:
X2=X2.drop(p_values.index[0],axis=1)
 else:
break
kolumny = X2.columns
testX2 = sm.add_constant(testX)
 testX2 = testX2[kolumny]
y_pred = model.predict(testX2)
 y_pred = np.exp(y_pred)
 res = pd.DataFrame({"Model":['Stepwise Regression'],
"RMSLE": [rmsle(testY, y_pred)]})
 Results = Results.append(res )<compute_train_metric>
test_data = []
for file in os.listdir(test_dir):
test_data.append(['test/{}'.format(file), file])
 test = pd.DataFrame(test_data, columns=['Filepath', 'File'])
 test.head()
model = Ridge(alpha=0.0005)
 model.fit(trainX,trainY)
 y_pred = model.predict(testX)
 y_pred = np.exp(y_pred)
 print(rmsle(testY, y_pred))
res = pd.DataFrame({"Model":['Ridge'],
"RMSLE": [rmsle(testY, y_pred)]})
 Results = Results.append(res )<prepare_output>
IMAGE_SIZE = 66
def read_image(filepath):
return cv2.imread(os.path.join(data_dir, filepath))
def resize_image(image, image_size):
return cv2.resize(image.copy() , image_size, interpolation=cv2.INTER_AREA )
model = Lasso(alpha=0)
 model.fit(trainX,trainY)
 y_pred = model.predict(testX)
 y_pred = np.exp(y_pred)
 print(rmsle(testY, y_pred))
res = pd.DataFrame({"Model":['Linear Regression'],
"RMSLE": [rmsle(testY, y_pred)]})
 Results = Results.append(res )<save_to_csv>
def create_mask(image):
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
 lower_green = np.array([30, 100, 50])
 upper_green = np.array([85, 255, 255])
 mask = cv2.inRange(image_hsv, lower_green, upper_green)
 kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(15, 15))
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
 return mask
def segment_image(image):
mask = create_mask(image)
 res = cv2.bitwise_and(image, image, mask=mask)
 return res
trainX = data[data.SalePrice.isnull() ==False].drop(['SalePrice','train'],axis=1)
 trainY = data.SalePrice[data.SalePrice.isnull() ==False]
testX = data[data.SalePrice.isnull() ==True].drop(['SalePrice','train'],axis=1)
 trainY = np.log(trainY)
 model = Lasso(alpha=0.0005)
 model.fit(trainX, trainY)
 test = data[data.train==0]
test['SalePrice'] = model.predict(testX)
 test['SalePrice'] = np.exp(test['SalePrice'])
 test = test.reset_index()
test[['Id','SalePrice']].to_csv("submissionLASSO.csv",index=False)
 print("done1" )<load_from_csv>
X_train = np.zeros(( train.shape[0], IMAGE_SIZE, IMAGE_SIZE, 3))
for i, file in tqdm(enumerate(train['File'].values)) :
image = read_image(file)
 image_segmented = segment_image(image)
 X_train[i] = resize_image(image_segmented,(IMAGE_SIZE, IMAGE_SIZE))
X_train = X_train / 255.
 print('Train Shape: {}'.format(X_train.shape))
def load_titanic_data(filename, titanic_path):
csv_path = os.path.join(titanic_path, filename)
 return pd.read_csv(csv_path )<load_from_csv>
Y_train = train['SpeciesId'].values
Y_train = to_categorical(Y_train, num_classes=12 )
train_data = load_titanic_data('train.csv',".. /input")
 test_data = load_titanic_data('test.csv','.. /input' )<set_options>
BATCH_SIZE = 16
EPOCHS = 75
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size=0.1, random_state=SEED )
%matplotlib inline
train_data[['Age','SibSp','Parch','Fare']].hist(figsize=(8,8))<count_values>
def construct_model() :
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3),
activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
 model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dense(12, activation='softmax'))
optimizer = Adam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=0.1, decay=0.0)
 model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
 model.summary()
return model
train_data['Survived'].value_counts()<count_values>
def train_model() :
model = construct_model()
annealer = ReduceLROnPlateau(monitor='val_acc', factor=0.5, patience=5, verbose=1, min_lr=1e-5)
 checkpoint = ModelCheckpoint('model.h5', verbose=1, save_best_only=True)
 datagen = ImageDataGenerator(rotation_range=360,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
vertical_flip=True)
 datagen.fit(X_train)
 hist = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=BATCH_SIZE),
steps_per_epoch=X_train.shape[0] // BATCH_SIZE,
epochs=EPOCHS,
verbose=2,
callbacks=[annealer, checkpoint],
validation_data=(X_val, Y_val))
train_data['Pclass'].value_counts()<count_values>
final_model = load_model('.. /input/plant-seedling-models/model.h5')
 final_loss, final_accuracy = final_model.evaluate(X_val, Y_val)
 print('Final Loss: {}, Final Accuracy: {}'.format(final_loss, final_accuracy))
train_data['Sex'].value_counts()<count_values>
X_test = np.zeros(( test.shape[0], IMAGE_SIZE, IMAGE_SIZE, 3))
for i, file in tqdm(enumerate(test['Filepath'].values)) :
image = read_image(file)
 image_segmented = segment_image(image)
 X_test[i] = resize_image(image_segmented,(IMAGE_SIZE, IMAGE_SIZE))
X_test = X_test / 255 .
<train_model><EOS> 
predictions = final_model.predict(X_test)
 predictions = np.argmax(predictions, axis=1)
 df = pd.DataFrame({'file': [file for file in test['File'].values], 'species': [species[i] for i in predictions]})
 df.to_csv('submission.csv', index=False )