<jupyter_start><jupyter_text>Hotel Recommender  Reading Data<jupyter_code>hotels = pd.read_csv('../datasets/hotel_features_dataset.csv')
hotels = hotels.drop('Unnamed: 0',axis=1)
hotels.head()<jupyter_output><empty_output><jupyter_text>Feature Extraction<jupyter_code>features = set()
groups = pd.DataFrame(hotels['country'].value_counts().head(20))
groups.sort_values(by='country',ascending=False)
from matplotlib.pyplot import figure
figure(num=None, figsize=(15,8), dpi=100, facecolor='w', edgecolor='r')
sns.barplot(x=groups.index,y=groups['country'],data=groups,orient='v')
plt.xticks(rotation=30)
stars = hotels.groupby('rating')['name'].count()
stars
fig1,ax1 = plt.subplots(figsize=(8,8))
ax1.pie(stars,labels=stars.index,explode= (0.1, 0.2,0.3,0,0,0.1, 0, 0,0,0), autopct='%1.2f%%' )
plt.show()
hotels['amenities'].apply(lambda item: [features.add(x) for x in str(item).split(',')])
hotels['rooms'].apply(lambda item: [features.add(x) for x in str(item).split(',')])
hotels['types'].apply(lambda item: [features.add(x) for x in str(item).split(',')])

print()
features
features = {x for x in features if pd.notna(x) or x != ''}
features.discard('')
features.discard('nan')
# features.discard(' DVDs')
# features.discard(' music for children')
features
features_df = pd.DataFrame(columns=features)
features_df<jupyter_output><empty_output><jupyter_text>Getting each hotel feature as a Vector<jupyter_code>amenities  = hotels['amenities'] + ',' +  hotels['rooms'] + ',' +  hotels['types']
amenities[0]
def extract_features(amenities,features) -> list:
    hotel_features = []
    col_names = pd.Index(features)
    for i,amenity in enumerate(amenities):
        if pd.notna(amenity):
            feats = amenity.split(',')
            row = np.zeros(len(features))
            for feat in feats:
                if feat != '':
                    index = np.where(col_names == feat)[0][0] #index
                    row[index] = 1
            hotel_features.append(row)
    return hotel_features

hotel_features = extract_features(amenities,features)
import pickle
with open('hotel_features.pkl','wb') as hf:
    pickle.dump(hotel_features,hf)
with open('hotel_features.pkl','rb') as hf:
    hotel_features = pickle.load(hf)
features_df = pd.DataFrame(hotel_features,columns= features)
features_df.head()
# add location feature
locations = set(hotels['country'])
locations_list = [c for c in locations]
features_df['country'] = hotels['country'].apply(lambda loc: locations_list.index(loc))
features_df['country']
y= features_df.copy()
y['rating'] = hotels['rating'].apply(lambda x:int(x))
features_df.shape
hotels_names = hotels['name']
features_df = features_df.rename(index=hotels_names)
features_df.head()<jupyter_output><empty_output><jupyter_text>KNN Content-Based Recommender<jupyter_code>from sklearn.neighbors import NearestNeighbors
from sklearn.model_selection import train_test_split
from sklearn.manifold import TSNE
from sklearn.decomposition import TruncatedSVD
knn = NearestNeighbors(n_neighbors=20,metric='cosine')
knn.fit(features_df)
distances,indecies = knn.kneighbors(features_df)
features_df.shape
tsne = TSNE(n_components=2).fit_transform(indecies[:1000])
plt.scatter(tsne[:,0],tsne[:,1],alpha=0.5)<jupyter_output><empty_output><jupyter_text>Get Recommendation<jupyter_code>def get_recommendation(name,k_indecies,k_distances,features_df,df):
    index = np.where(features_df.index == name)
    neighbours =  k_indecies[index].tolist()[0][1:21]
    distance = k_distances[index][0].tolist()
    for i,neighbour in enumerate(neighbours):
            print(df['name'].loc[neighbour],':',1 - distance[i+1])
get_recommendation('Hotel Pamphylia',indecies,distances,features_df,hotels)
def get_feature_recommendation(features,knn,n_neighbors):
    vec = np.zeros(len(features_df.columns))
    data = list(features_df.columns)
    for feat in features:
        try:
            index = data.index(feat)
            vec[index] = 1
        except:
            continue
    distances,indecies = knn.kneighbors([vec],n_neighbors)
    hotels = []
    for i,ind in enumerate(indecies):
        hotels.append((features_df.iloc[ind].index,distances[i]))
    return hotels
from sklearn.metrics.pairwise import cosine_similarity
from numpy.linalg import norm
def similarity(one,two,df):
    one = df.loc[one]
    two = df.loc[two]
    return np.dot(one,two)/ np.multiply(norm(one),norm(two))
similarity("Hampton Inn Suites Bolingbrook","Warwick Stone 55 Hotel",features_df)
import pickle
import pickle
with open("hotels_knn.pkl",'wb') as f:
    pickle.dump([distances,indecies],f)
with open("hotels_knn.pkl",'rb') as f:
    model = pickle.load(f)
k_distances,k_indecies = model
feats =['72 smoke-free guestrooms,Daily housekeeping,Restaurant and bar/lounge,Breakfast available,6 meeting rooms,24-hour front desk,Air conditioning,Garden,Laundry service,Conference space,Multilingual staff,Concierge services,Free WiFi ,Free WiFi,BROOKLYN KING ROOM,BROOKLYN QUEEN ROOM,BUNK BED,MANHATTAN VIEW KING ROOM,STUDIO QUEEN ROOM,Air conditioning,Minibar,Bathrobes,Iron/ironing board,Blackout drapes/curtains,In-room massage available,Individually decorated,Rainfall showerhead,Shower only,Free toiletries,Hair dryer,40-inch Smart TV,Premium TV channels,Smart speaker,Free newspaper,Free WiFi,Free long-distance calls,Daily housekeeping,In-room safe,']
feats = '72 smoke-free guestrooms,Daily housekeeping,Restaurant and bar/lounge,Breakfast available,6 meeting rooms,24-hour front desk,Air conditioning,Garden,Laundry service,Conference space,Multilingual staff,Concierge services,Free WiFi,BROOKLYN KING ROOM,BROOKLYN QUEEN ROOM,BUNK BED,MANHATTAN VIEW KING ROOM,STUDIO QUEEN ROOM,Air conditioning,Minibar,Bathrobes,Iron/ironing board,Blackout drapes/curtains,In-room massage available,Individually decorated,Rainfall showerhead,Shower only,Free toiletries,Hair dryer,40-inch Smart TV,Premium TV channels,Smart speaker,Free newspaper,Free WiFi,Free long-distance calls,Daily housekeeping,In-room safe'.split(',')
knn1 = '10 guestrooms,Daily housekeeping,24-hour room service,Airport shuttle,24-hour front desk,Coffee/tea in a common area,Air conditioning,Garden,Laundry service,Concierge services,Free newspapers in the lobby,Luggage storage,Free WiFi ,Free WiFi,Single Room,Double or Twin Room,Standard Triple Room,Family Room,Economy Double Room,Apartment, 2 Bedrooms,Family Condo, 2 Bedrooms, City View,Classic Apartment,Family Apartment,Air conditioning,Slippers,Soundproofed rooms,Private bathroom,Shower only,Free toiletries,Hair dryer,LCD TV,Satellite TV channels,Free WiFi,Daily housekeeping,In-room safe'.split(',')
knn1
vec = np.zeros(len(features_df.columns))
data = list(features_df.columns)
for feat in knn1:
    try:
        index = data.index(feat)
        vec[index] = 1
    except:
        continue
vec.shape
vec
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder,MinMaxScaler
X_train,X_test,y_train,y_test = train_test_split(features_df,y['rating'],random_state=42)
scalar = MinMaxScaler()
X_train = scalar.fit_transform(X_train)
X_test = scalar.transform(X_test)
knn = KNeighborsClassifier(n_neighbors=6,)
y_train
knn.fit(X_train,y_train)
X_train.shape
y_model = knn.predict(X_test)
from sklearn.metrics import accuracy_score
accuracy_score(y_test,y_model)
y['rating'].unique()
distances,indecies = knn.kneighbors(features_df)
with open('hotels_knn_classifier.pkl','rb') as hf:
       model =  pickle.load(hf)
distances,indecies = model
neighbours = indecies[index].tolist()[1:21]
distance = distances[index].tolist()
for i,neighbour in enumerate(neighbours):
        print(hotels['name'].loc[neighbour],':', distance[i])
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
vis = pca.fit_transform(indecies)[:1000]
plt.scatter(x=vis[:,0],y=vis[:,1],alpha=0.3)
get_feature_recommendation(knn1,knn,20)
x = features_df.iloc[10
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    3]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12493917274939173, 'fraction_numerical': 0.012773722627737226, 'mean_word_length': 5.304447852760736, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '440570', 'license': 'MIT', 'n_tokens_mistral': 2912, 'n_tokens_neox': 2774, 'n_words': 518, 'path': 'search_engine/recommendation_system/Hotels_Recommender.ipynb', 'repo_name': 'OsamaAlhalabi/Hotel-recommendation-engine', 'size': '8220'}