<jupyter_start><jupyter_text><jupyter_code>import os 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import datetime 
#WordCounting
from wordcloud import WordCloud, ImageColorGenerator, STOPWORDS
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
#warnings
import warnings
warnings.filterwarnings("ignore")
!mkdir ~/.kaggle
#!cd ~/.kaggle
!touch ~/.kaggle/kaggle.json

api_token = {"username":"xxx","key":"xxx"}

import json

with open('/root/.kaggle/kaggle.json', 'w') as file:
    json.dump(api_token, file)

!chmod 600 ~/.kaggle/kaggle.json
!kaggle datasets download -d brittabettendorf/berlin-airbnb-data
import zipfile

zf = zipfile.ZipFile('berlin-airbnb-data.zip') 
df_calender_summary = pd.read_csv(zf.open('calendar_summary.csv'))
df_listings = pd.read_csv(zf.open('listings.csv'))
df_listings_summary = pd.read_csv(zf.open('listings_summary.csv'))
df_neighbourhoods = pd.read_csv(zf.open('neighbourhoods.csv'))
df_reviews1 = pd.read_csv(zf.open('reviews.csv'))
df_reviews_summary = pd.read_csv(zf.open('reviews_summary.csv'))
df_listings.head(3)
df_listings_summary.head(3)
df_listings_summary.columns
#what columns have the most unique values?
for i in df_listings_summary.columns:
    print("Column name {} and unique values are {}".format(i,len(df_listings_summary[i].unique())))
#List of all catagories we're using to predict 
df2 = df_listings_summary[['minimum_nights','street','maximum_nights','cleaning_fee','property_type','extra_people','zipcode',
                          'room_type','bed_type','review_scores_value', 'instant_bookable','host_has_profile_pic', 'requires_license','price']]
#catagorical features for model
catagorical_features = df_listings_summary[['room_type','bed_type','review_scores_value', 'instant_bookable','host_has_profile_pic', 'requires_license','price']]
#continueous features
df = df_listings_summary[['minimum_nights','street','maximum_nights','cleaning_fee','property_type','extra_people','zipcode']]<jupyter_output><empty_output><jupyter_text>What do the graphs have to say about the catagories?<jupyter_code>for Categorical_Values in catagorical_features.columns:
    cat_num = catagorical_features[Categorical_Values].value_counts()
    print('graph for %s: total = %d' % (Categorical_Values, len(cat_num)))
    chart = sns.barplot(x=cat_num.index, y=cat_num)
    chart.set_xticklabels(chart.get_xticklabels(), rotation=90)
    plt.show()<jupyter_output>graph for room_type: total = 3<jupyter_text>Based on our analysis we can conclude that:*   Apparments are the predominant property type *   Supper hosts make up the minority of property renters*   Mosts review range from 8 stars to 10*   Most propertys are not instant bookable*   Most people have just listen 1 house while some own 2 and 3 *   Most listed propertys are not instant bookable*   1 bedroom houses were among the most common  What's the maxiumum/minimum cost per night?<jupyter_code>catagorical_features['price'].max()
catagorical_features['price'].min()<jupyter_output><empty_output><jupyter_text>correlation between features:<jupyter_code>catagorical_features.head(1)<jupyter_output><empty_output><jupyter_text>Cleaning Data:<jupyter_code>catagorical_features['price'] = catagorical_features['price'].apply(lambda x: float(x.replace(',','').replace('$',''))) #Removing symbolls<jupyter_output><empty_output><jupyter_text>Numberical representation of data types<jupyter_code>catagorical_features['room_type'] = catagorical_features['room_type'].apply(lambda x: (x.replace('Entire home/apt','1').replace('Private room','2').replace('Shared room','3')))
catagorical_features['room_type'] = catagorical_features['room_type'].astype(int) 
catagorical_features['bed_type'] = catagorical_features['bed_type'].apply(lambda x: (x.replace('Real Bed','1').replace('Pull-out Sofa','2').replace('Futon','3')).replace('Airbed','4'))
catagorical_features['bed_type'] = catagorical_features['room_type'].astype(int)
catagorical_features['instant_bookable'] = catagorical_features.host_has_profile_pic.map(dict(t=1, f=0))
catagorical_features['host_has_profile_pic'] = catagorical_features.host_has_profile_pic.map(dict(t=1, f=0))
catagorical_features['requires_license'] = catagorical_features.requires_license.map(dict(t=1, f=0))
catagorical_features.head(1)<jupyter_output><empty_output><jupyter_text>Prepocessing our data:<jupyter_code>from sklearn.preprocessing import LabelEncoder, OneHotEncoder
cat_features=['room_type','bed_type','review_scores_value', 'instant_bookable','host_has_profile_pic', 'requires_license','price']
out_feature="price"
from sklearn.preprocessing import LabelEncoder
lbl_encoders={}
for feature in cat_features:
    lbl_encoders[feature]=LabelEncoder()
    catagorical_features[feature]=lbl_encoders[feature].fit_transform(catagorical_features[feature])
z, ax = plt.subplots(figsize=(20, 11))
sns.heatmap(catagorical_features.corr(), center=0,
            square=True, cmap="twilight", linewidths=1, cbar_kws={"shrink": 1})<jupyter_output><empty_output><jupyter_text>Creating numpy array from our catagorical features:<jupyter_code>cat_features = np.stack([catagorical_features['room_type'], catagorical_features['bed_type'],catagorical_features['review_scores_value'],
                          catagorical_features['instant_bookable'],catagorical_features['host_has_profile_pic'],catagorical_features['requires_license']],1)
cat_features<jupyter_output><empty_output><jupyter_text>Converting Catagory features into tensors:<jupyter_code>import torch
cat_features=torch.tensor(cat_features,dtype=torch.int64)
cat_features<jupyter_output><empty_output><jupyter_text>Create continuous variable:<jupyter_code>cont_features=[]
for i in df2.columns:
    if i in ['minimum_nights','street','maximum_nights','cleaning_fee','property_type','extra_people','zipcode']:
        pass
    else:
        cont_features.append(i)
cont_features<jupyter_output><empty_output><jupyter_text>Stacking continuous variable to a tensor:<jupyter_code>cont_values=np.stack([catagorical_features[i].values for i in cont_features],axis=1)
cont_values=torch.tensor(cont_values,dtype=torch.float)
cont_values<jupyter_output><empty_output><jupyter_text>Dependent Feature:<jupyter_code>y=torch.tensor(catagorical_features['price'].values,dtype=torch.float).reshape(-1,1)
y
catagorical_features.shape
cat_features.shape,cont_values.shape,y.shape<jupyter_output><empty_output><jupyter_text>Embedding Size For Categorical columns:<jupyter_code>cat_dims = [len(catagorical_features[col].unique()) for col in ['room_type','bed_type','review_scores_value', 'instant_bookable','host_has_profile_pic', 'requires_license']]
cat_dims<jupyter_output><empty_output><jupyter_text>embedding dimentions:<jupyter_code>embedding_dim = [(x, min(50, (x + 1) // 2)) for x in cat_dims]
embedding_dim<jupyter_output><empty_output><jupyter_text>Model Building:<jupyter_code>import torch
import torch.nn as nn
import torch.nn.functional as F<jupyter_output><empty_output><jupyter_text>embed representation:<jupyter_code>embed_representation=nn.ModuleList([nn.Embedding(inp,out) for inp,out in embedding_dim])
embed_representation
cat_featuresz=cat_features[:6]
cat_featuresz<jupyter_output><empty_output><jupyter_text>Creating embeded values:<jupyter_code>pd.set_option('display.max_rows', 500)
embedding_val=[]
for i,e in enumerate(embed_representation):
    embedding_val.append(e(cat_features[:,i]))
embedding_val
z = torch.cat(embedding_val, 1)
z<jupyter_output><empty_output><jupyter_text>Implement dropupout:<jupyter_code>droput=nn.Dropout(.4)
final_embed=droput(z)
final_embed<jupyter_output><empty_output><jupyter_text>Create a Feed Forward Neural Network:<jupyter_code>import torch
import torch.nn as nn
import torch.nn.functional as F
class FeedForwardNN(nn.Module):

    def __init__(self, embedding_dim, n_cont, out_sz, layers, p=0.5):
        super().__init__()
        self.embeds = nn.ModuleList([nn.Embedding(inp,out) for inp,out in embedding_dim])
        self.emb_drop = nn.Dropout(p)
        self.bn_cont = nn.BatchNorm1d(n_cont)
        
        layerlist = []
        n_emb = sum((out for inp,out in embedding_dim))
        n_in = n_emb + n_cont
        
        for i in layers:
            layerlist.append(nn.Linear(n_in,i)) 
            layerlist.append(nn.ReLU(inplace=True))
            layerlist.append(nn.BatchNorm1d(i))
            layerlist.append(nn.Dropout(p))
            n_in = i
        layerlist.append(nn.Linear(layers[-1],out_sz))
            
        self.layers = nn.Sequential(*layerlist)
    
    def forward(self, x_cat, x_cont):
        embeddings = []
        for i,e in enumerate(self.embeds):
            embeddings.append(e(x_cat[:,i]))
        x = torch.cat(embeddings, 1)
        x = self.emb_drop(x)
        
        x_cont = self.bn_cont(x_cont)
        x = torch.cat([x, x_cont], 1)
        x = self.layers(x)
        return x
torch.manual_seed(100)
model=FeedForwardNN(embedding_dim,len(cont_features),1,[100,50],p=0.4)
loss_function=nn.MSELoss()
optimizer=torch.optim.Adam(model.parameters(),lr=0.001)<jupyter_output><empty_output><jupyter_text>Creating test and train set:<jupyter_code>batch_size=22550
test_size=int(batch_size*0.30)
train_categorical=cat_features[:batch_size-test_size]
test_categorical=cat_features[batch_size-test_size:batch_size]
train_cont=cont_values[:batch_size-test_size]
test_cont=cont_values[batch_size-test_size:batch_size]
y_train=y[:batch_size-test_size]
y_test=y[batch_size-test_size:batch_size]<jupyter_output><empty_output><jupyter_text>Size of our train / test data:<jupyter_code>len(train_categorical),len(test_categorical),len(train_cont),len(test_cont),len(y_train),len(y_test)
epochs=950
final_losses=[]
for i in range(epochs):
    i=i+1
    y_pred=model(train_categorical,train_cont)
    loss=torch.sqrt(loss_function(y_pred,y_train)) ### RMSE
    final_losses.append(loss)
    if i%10==1:
        print("Epoch number: {} and the loss : {}".format(i,loss.item()))
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
plt.plot(range(epochs), final_losses)
plt.ylabel('RMSE Loss')
plt.xlabel('epoch');<jupyter_output><empty_output><jupyter_text>Validating the Test Data:<jupyter_code>y_pred=""
with torch.no_grad():
    y_pred=model(test_categorical,test_cont)
    loss=torch.sqrt(loss_function(y_pred,y_test))
print('RMSE: {}'.format(loss))
data_verify=pd.DataFrame(y_test.tolist(),columns=["Test"])
data_predicted=pd.DataFrame(y_pred.tolist(),columns=["Prediction"])
model.eval()<jupyter_output><empty_output><jupyter_text>Prediction VS Actual:<jupyter_code>final_output=pd.concat([data_verify,data_predicted],axis=1)
final_output['Difference']=final_output['Test']-final_output['Prediction']
final_output.head()<jupyter_output><empty_output><jupyter_text>On average our model was off by:<jupyter_code>final_output['Difference'].mean()<jupyter_output><empty_output><jupyter_text>Saving the model<jupyter_code>torch.save(model,'Kaggle_House_data.pt')
torch.save(model.state_dict(),'Kaggle_House_data.pt')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13117117117117116, 'fraction_numerical': 0.008468468468468469, 'mean_word_length': 7.645638629283489, 'pattern_counts': {'":': 3, '<': 100, '<?xml version=': 0, '>': 100, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '25', 'dir': 'jupyter-structured-clean-dedup', 'id': '141382', 'license': 'Apache-2.0', 'n_tokens_mistral': 4032, 'n_tokens_neox': 3904, 'n_words': 556, 'path': 'German_house_Kaggle.ipynb', 'repo_name': 'SamuelLawrence876/Analysing-Berlin-AirBnB-Rental', 'size': '11100'}