text_plain_output_1.png
from keras.layers import Input, Embedding, Flatten, Dot, Dense

from keras.models import Model

import numpy as np

import pandas as pd

def import_data():
    dataset = pd.read_csv('../input/ratings.csv')
    books = pd.read_csv('../input/books.csv')
    return (dataset, books)

def extract_book_and_user(dataset):
    n_user = len(dataset.user_id.unique())
    n_books = len(dataset.book_id.unique())
    return (n_user, n_books)

def nural_net_model_function():
    from keras.layers import Input, Embedding, Flatten, Dot, Dense
    from keras.models import Model
    book_input = Input(shape=[1], name='Book-Input')
    book_embadding = Embedding(n_books + 1, 5, name='Bok-embadding')(book_input)
    book_vec = Flatten(name='Flatten-books')(book_embadding)
    user_input = Input(shape=[1], name='User-Input')
    user_embedding = Embedding(n_user + 1, 5, name='User-Embedding')(user_input)
    user_vec = Flatten(name='Flatten-Users')(user_embedding)
    prod = Dot(name='Dot-Product', axes=1)([book_vec, user_vec])
    x_train = [user_input, book_input]
    y_train = prod
    model = Model(x_train, y_train)
    OPTIMIZER = 'adam'
    ERROR_FUNCTION = 'mean_squared_error'
    model.compile(OPTIMIZER, ERROR_FUNCTION)
    model.fit([train.user_id, train.book_id], train.rating, epochs=10, verbose=1)
    return model

model = nural_net_model_function()

model.save('regresssion.model.h5')

def get_unique_value():
    book_data = np.array(list(set(datasets.book_id)))
    return book_data

book_data = get_unique_value()

def setting_user(user_id):
    user = np.array([user_id for i in range(len(book_data))])
    return user

user = setting_user(1)

predictions = model.predict([user, book_data])

predictions = np.array([item[0] for item in predictions])

def get_recommended_book_id(predictions):
    recommended_book_ids = (-predictions).argsort()[:5]
    return recommended_book_ids

recomended_book = get_recommended_book_id(predictions)

recomended_book