Kaggle competition: finec-1941-hw6
train.drop(train.query(query ).index, inplace=True )<drop_column>
import json
from sklearn.model_selection import train_test_split
from sklearn.base import BaseEstimator
from sklearn.metrics import f1_score
import scipy
train.drop(train.query(query ).index, inplace=True )<drop_column>
train = pd.read_csv('/kaggle/input/finec-1941-hw6/train.csv', encoding='utf-8')
 test = pd.read_csv('/kaggle/input/finec-1941-hw6/test.csv', encoding='utf-8')
 for col in(['production_companies', 'production_countries', 'spoken_languages', 'cast']):
train[col] = train[col].apply(json.loads)
 test[col] = test[col].apply(json.loads)
 train.head().T
train.drop(train.query('Id == "da68d2812229a8"' ).index, inplace=True )<drop_column>
x_train = train.drop(['index', 'genre_id'], axis=1)
 y_train = train['genre_id']
x_test = test.drop('index', axis=1)
 np.random.seed(514229)
 x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.33 )
train.drop(train.query(query ).index, inplace=True )<drop_column>
class ClusteringClassifier(BaseEstimator):
def __init__(self, clustering):

 self.clustering = clustering
def fit(self, x, y):
self.clustering.fit(x)
 clustered = self.clustering.predict(x)
 self.cluster_labels = {}
for c in np.unique(clustered):
ys = y[clustered == c]
most_popular_label = scipy.stats.mode(ys ).mode[0]
self.cluster_labels[c] = most_popular_label
return self
def predict(self, x):
clustered = self.clustering.predict(x)
 prediction = [self.cluster_labels[c] for c in clustered]
return np.array(prediction)
train.drop(train.query(query ).index, inplace=True )<drop_column>
model = ClusteringClassifier(clustering=ConstClustering())
 model.fit(x_train, y_train)
 pred_train = model.predict(x_train)
 pred_val = model.predict(x_val)
 print('f1 train:', f1_score(y_train, pred_train, average='macro'))
print('f1 val:', f1_score(y_val, pred_val, average='macro'))
<drop_column><EOS> 
pred = model.predict(x_test)
 pd.Series(pred, index=test['index'], name='genre_id' ).to_frame().to_csv(f"default_const_submit.csv" )