<jupyter_start><jupyter_text>Read Data<jupyter_code>dataTraining = pd.read_csv('dataTraining.csv', encoding='UTF-8', index_col=0)
dataTesting = pd.read_csv('dataTesting.csv', encoding='UTF-8', index_col=0)
dataTraining.head()
dataTesting.head()<jupyter_output><empty_output><jupyter_text>Read Images<jupyter_code>path = 'C:\\Users\\admicf\\Documents\\AppliedDeepLearningClass-master\\FinalProject\\'
image = io.imread(os.path.join(path, 'images', str(dataTraining.index[666]) + '.jpeg'))
image
fig, ax = plt.subplots(nrows=1, ncols=1)
ax.imshow(image, cmap='gray')<jupyter_output><empty_output><jupyter_text>Read all images<jupyter_code>images_training = []
for i in tqdm(dataTraining.index):
    images_training.append(io.imread(os.path.join(path, 'images_resize_gray', str(i) + '_resize_gray.jpeg')).flatten())
images_training = np.stack(images_training)
images_training.shape, dataTraining.shape[0]
images_testing = []
for i in tqdm(dataTesting.index):
    images_testing.append(io.imread(os.path.join(path, 'images_resize_gray', str(i) + '_resize_gray.jpeg')).flatten())
images_testing = np.stack(images_testing)
images_testing.shape, dataTesting.shape[0]<jupyter_output><empty_output><jupyter_text>Apply PCA<jupyter_code>from sklearn.decomposition import RandomizedPCA
from sklearn.decomposition import PCA
import time
time0 = time.time()
pca = PCA(n_components = 32)
images_training_pca = pca.fit_transform(images_training)
time1 = time.time()
print(time1-time0)
images_testing_pca = pca.transform(images_testing)<jupyter_output><empty_output><jupyter_text>Create y<jupyter_code>dataTraining['genres'] = dataTraining['genres'].map(lambda x: eval(x))
le = MultiLabelBinarizer()
y_genres = le.fit_transform(dataTraining['genres'])
X_train, X_test, y_train_genres, y_test_genres = train_test_split(images_training_pca, y_genres, test_size=0.20, random_state=666)<jupyter_output><empty_output><jupyter_text>Train multi-class multi-label model<jupyter_code>from sklearn.multiclass import OneVsRestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve, auc
from sklearn.preprocessing import label_binarize
from scipy import interp
ovr = OneVsRestClassifier(LogisticRegression())
clf = OneVsRestClassifier(RandomForestClassifier(n_jobs=-1, n_estimators=100, max_depth=24, random_state=666))
neigh = OneVsRestClassifier(KNeighborsClassifier(n_neighbors=20))
ovr_mod = ovr.fit(X_train,y_train_genres)
clf_mod = clf.fit(X_train, y_train_genres)
neigh_mod = neigh.fit(X_train, y_train_genres)<jupyter_output><empty_output><jupyter_text>Predict Logistic Regression<jupyter_code>y_pred_genres_ovr = ovr_mod.predict_proba(X_test)
roc_auc_score(y_test_genres, y_pred_genres_ovr, average='micro')
cols = ['p_Action', 'p_Adventure', 'p_Animation', 'p_Biography', 'p_Comedy', 'p_Crime', 'p_Documentary', 'p_Drama', 'p_Family',
        'p_Fantasy', 'p_Film-Noir', 'p_History', 'p_Horror', 'p_Music', 'p_Musical', 'p_Mystery', 'p_News', 'p_Romance',
        'p_Sci-Fi', 'p_Short', 'p_Sport', 'p_Thriller', 'p_War', 'p_Western']

y_pred_test_genres = ovr.predict_proba(images_testing_pca)

pd.DataFrame(y_pred_test_genres, index=dataTesting.index, columns=cols).to_csv('pred_genres_img_LR.csv', index_label='ID')<jupyter_output><empty_output><jupyter_text>Predict Random Forest<jupyter_code>y_pred_genres_clf = clf_mod.predict_proba(X_test)
roc_auc_score(y_test_genres, y_pred_genres_clf, average='micro')
cols = ['p_Action', 'p_Adventure', 'p_Animation', 'p_Biography', 'p_Comedy', 'p_Crime', 'p_Documentary', 'p_Drama', 'p_Family',
        'p_Fantasy', 'p_Film-Noir', 'p_History', 'p_Horror', 'p_Music', 'p_Musical', 'p_Mystery', 'p_News', 'p_Romance',
        'p_Sci-Fi', 'p_Short', 'p_Sport', 'p_Thriller', 'p_War', 'p_Western']

y_pred_test_genres = clf.predict_proba(images_testing_pca)

pd.DataFrame(y_pred_test_genres, index=dataTesting.index, columns=cols).to_csv('pred_genres_img_RF.csv', index_label='ID')<jupyter_output><empty_output><jupyter_text>Predict K Neighbors<jupyter_code>y_pred_genres_neigh = neigh_mod.predict_proba(X_test)
roc_auc_score(y_test_genres, y_pred_genres_neigh, average='micro')
cols = ['p_Action', 'p_Adventure', 'p_Animation', 'p_Biography', 'p_Comedy', 'p_Crime', 'p_Documentary', 'p_Drama', 'p_Family',
        'p_Fantasy', 'p_Film-Noir', 'p_History', 'p_Horror', 'p_Music', 'p_Musical', 'p_Mystery', 'p_News', 'p_Romance',
        'p_Sci-Fi', 'p_Short', 'p_Sport', 'p_Thriller', 'p_War', 'p_Western']

y_pred_test_genres = neigh.predict_proba(images_testing_pca)

pd.DataFrame(y_pred_test_genres, index=dataTesting.index, columns=cols).to_csv('pred_genres_img_KN.csv', index_label='ID')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup