Kaggle competition: Machine Learning Lab - CAS Data Science HS 20
<SOS> metric: mae Kaggle data source: machine-learning-lab-cas-data-science-hs-20<save_to_csv>
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
from sklearn.compose import make_column_transformer
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
from sklearn.base import BaseEstimator, TransformerMixin
model = SVC(C=270, kernel='rbf', degree=3, gamma=1.3, coef0=1.0, shrinking=True, tol=0.001, probability=True, cache_size=500, max_iter=-1)
 ovr = OneVsRestClassifier(model, n_jobs=1)
 ovr.fit(x, y_target)
 ovr_pred = ovr.predict(x_test)
 y_test = encoder.inverse_transform(ovr_pred)
 test_id = test_df["id"]
submit_cook = pd.DataFrame({'id': test_id, 'cuisine': y_test}, columns=['id', 'cuisine'])
 submit_cook.to_csv('svc_tf.csv', index=False )<save_to_csv>
data = pd.read_csv("/kaggle/input/machine-learning-lab-cas-data-science-hs-20/train-data.csv", index_col=0 )
model = xgboost.XGBClassifier(max_depth = 9, eta = 0.003, subsample = 0.7, gamma = 7)
 ovr = OneVsRestClassifier(model, n_jobs = -1)
 ovr.fit(x, y_target)
 ovr_pred = ovr.predict(x_test)
 y_test = encoder.inverse_transform(ovr_pred)
 test_id = test_df["id"]
submit_xg = pd.DataFrame({'id': test_id, 'cuisine': y_test}, columns=['id', 'cuisine'])
 submit_xg.to_csv('xgboost_tf.csv', index=False )<load_from_disk>
train_data, dev_data = train_test_split(data, test_size=0.1, random_state=0 )
with open('.. /input/train.json')as json_file:
train_data = json.load(json_file)
 with open('.. /input/test.json')as json_file:
test_data = json.load(json_file)
 cuisines = []
for i in range(0, len(train_data)) :
cuisines.append(train_data[i]['cuisine'])
 cuisines = list(set(cuisines))
ingredients_train = []
for i in range(0, len(train_data)) :
for j in range(0, len(train_data[i]['ingredients'])) :
ingredients_train.append(train_data[i]['ingredients'][j])
 ingredients_train = list(set(ingredients_train))
ingredients_test = []
for i in range(0, len(test_data)) :
for j in range(0, len(test_data[i]['ingredients'])) :
ingredients_test.append(test_data[i]['ingredients'][j])
 ingredients_test = list(set(ingredients_test))
ingredients = list(set(ingredients_train)| set(ingredients_test))
for i in range(0, len(train_data)) :
train_data[i]['cuisine'] = list.index(cuisines, train_data[i]['cuisine'])+ 1
for j in range(0, len(train_data[i]['ingredients'])) :
train_data[i]['ingredients'][j] = list.index(ingredients, train_data[i]['ingredients'][j])+ 1<define_variables>
X_train, y_train = train_data.drop(columns=['G1', 'G2', 'G3']), train_data['G3']
X_dev, y_dev = dev_data.drop(columns=['G1', 'G2', 'G3']), dev_data['G3']
train_features = []
train_targets = []
for i in range(0, len(train_data)) :
train_features.append(train_data[i]['ingredients'])
 train_targets.append([train_data[i]['cuisine']])
 oneHotFeatures = np.zeros(( len(train_data), len(ingredients)))
 for i in range(0, len(train_data)) :
for j in range(0, len(ingredients)) :
if j + 1 in train_data[i]['ingredients']:
oneHotFeatures[i][j] = 1
else:
oneHotFeatures[i][j] = 0
oneHotTargets = np.zeros(( len(train_data), len(cuisines)))
 for i in range(0, len(train_data)) :
for j in range(0, len(cuisines)) :
if j + 1 == train_data[i]['cuisine']:
oneHotTargets[i][j] = 1
else:
oneHotTargets[i][j] = 0
oneHotFeatures = np.int32(oneHotFeatures)
 train_features = oneHotFeatures
oneHotTargets = np.int32(oneHotTargets)
 train_targets = oneHotTargets<choose_model_class>
num_features = ['age', 'absences', 'failures', 'studytime', 'Medu', 'Fedu', 'goout', 'absences', 'freetime']
cat_features = ['sex']
model = GradientBoostingClassifier(n_estimators=50, max_depth=10)
 model.fit(train_features, train_targets)
<feature_engineering>
class FeatureSelector(BaseEstimator, TransformerMixin):
def __init__(self, columns):
self.columns = columns
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
return X[self.columns]
class OutlierQuantileRemover(BaseEstimator, TransformerMixin):
def __init__(self, column, q=0.99):
self.column = column
self.q = q
self.quantile = None
def fit(self, X, y=None):
self.quantile = X[self.column].quantile()
return self
def transform(self, X, y=None):
X.loc[X[self.column] > self.quantile, self.column] = self.quantile
return X
class OutlierThresholdRemover(BaseEstimator, TransformerMixin):
def __init__(self, column, t):
self.column = column
self.t = t
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
X.loc[X[self.column] > self.t, self.column] = self.t
return X
pipeline = Pipeline([ 
('selector', FeatureSelector(columns=num_features+cat_features)) , 
('out', OutlierThresholdRemover(column='failures', t=3)) , 
('out2', OutlierQuantileRemover(column='freetime')) , 
('pre', make_column_transformer(( OneHotEncoder(handle_unknown='ignore'), cat_features), remainder='passthrough')) , 
('clf', LinearRegression(normalize=True))
])
 pipeline.fit(X_train, y_train )
test_features = []
for i in range(0, len(test_data)) :
test_features.append(test_data[i]['ingredients'])
 oneHotFeatures = np.zeros(( len(test_data), len(ingredients)))
 for i in range(0, len(test_data)) :
for j in range(0, len(ingredients)) :
if ingredients[j] in test_data[i]['ingredients']:
oneHotFeatures[i][j] = 1
else:
oneHotFeatures[i][j] = 0
oneHotFeatures = np.int32(oneHotFeatures)
 test_features = oneHotFeatures<save_to_csv>
y_pred_train = pipeline.predict(X_train)
 y_pred_dev = pipeline.predict(X_dev)
 print('train: ', mean_absolute_error(y_train, y_pred_train))
print('dev: ', mean_absolute_error(y_dev, y_pred_dev))
predictions = model.predict(test_features)
 cuisinePredictions = []
for i in range(0, len(predictions)) :
cuisinePredictions.append(cuisines[list.index(list(predictions[i]), max(predictions[i])) ])
 ids = np.int32(np.array([test_data[i]['id'] for i in range(0, len(test_data)) ]))
submission = pd.DataFrame(ids, columns={"id"})
 submission['cuisine'] = cuisinePredictions
submission = submission[["id", "cuisine"]]
submission.to_csv('cuisine-prediction-submission-3.csv', index=False )<load_from_csv>
X_test = pd.read_csv("/kaggle/input/machine-learning-lab-cas-data-science-hs-20/test-data.csv", index_col=0 )
train=pd.read_csv(".. /input/train.csv")
 test=pd.read_csv(".. /input/test.csv" )<load_from_csv>
y_test_pred = pipeline.predict(X_test )
<groupby><EOS> 
X_test_submission = pd.DataFrame(index=X_test.index)
 X_test_submission['G3'] = y_test_pred
X_test_submission.to_csv('submission_outlier_pipeline.csv', header=True, index_label='id' )