<jupyter_start><jupyter_text>Import libraries<jupyter_code>import pandas as pd
import numpy as np

# Pandas warning messages (optional)
pd.options.mode.chained_assignment = None  #default='warn'

# Plot options
import matplotlib.pyplot as plt
import seaborn as sns
sns.palplot(sns.color_palette('deep'))
sns.set_style("whitegrid")
%matplotlib inline
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

from sklearn import preprocessing
from sklearn.svm import SVC
data=pd.read_csv('data/listings.csv', low_memory=False, 
                 usecols=['id','name','host_id','host_name','neighbourhood','latitude','longitude',
                          'room_type','price','minimum_nights','number_of_reviews','last_review','reviews_per_month',
                          'calculated_host_listings_count','availability_365'])
data.info()
data.head()
len(data[data['room_type'] == 'Entire home/apt'])
len(data[data['room_type'] == 'Private room'])
# Convert 'price' from string currency to float
data['price'] = data['price'].str.replace('$', '').str.replace(',', '').astype(float, 2)
# Fill missing data with mean strategy
data[data==' ']=np.nan
#print the number of NaNs
print(data.isnull().sum())
corr=data.corr(method='kendall')
plt.figure(figsize=(15,8))
sns.heatmap(corr, annot=True)
data.columns
sns.countplot(data['room_type'],palette='plasma')
fig=plt.gcf()
fig.set_size_inches(5,5)
plt.title('Room Type')
data.drop(['id','name','host_id','latitude','longitude',
           'latitude','longitude','number_of_reviews'], axis=1, inplace=True)
#examing the changes
data.head(5)
data.drop(['last_review','reviews_per_month','host_name'], axis=1, inplace=True)
data.head(20)
from numpy.random import seed
def Encode(data): 
    for column in data.columns[data.columns.isin(['neighbourhood','room_type'])]: 
        data[column]=data[column].factorize()[0] 
    return data 
data_en=Encode(data.copy())
data_en.head()
np.random.seed(42)
N_rep=10000
mean_replicate=np.empty(N_rep)
for i in range(N_rep):
    samples=np.random.choice(data_en['price'], len(data_en['price']))
    mean_replicate[i]=np.mean(samples)
mean, std=np.mean(mean_replicate), np.std(mean_replicate)
lower_bound=mean-1.64*std
print(lower_bound)
entire_home=data_en.price[data_en.room_type==0]
private_room=data_en.price[data_en.room_type==1]
private_number=len(private_room)
entire_number=len(entire_home)
print(entire_number)
print(private_number)
mean_diff_observe=np.mean(entire_home)-np.mean(private_room)
print(mean_diff_observe)<jupyter_output>478.8538166947268<jupyter_text>The mean price difference between private room and entire home is $-478.85<jupyter_code>np.random.seed(42)
std_diff_replicate=np.empty(N_rep)
for i in range(N_rep):
    entire_samples=np.random.choice(entire_home, entire_number)
    private_samples=np.random.choice(private_room, private_number)    
    std_diff_replicate[i]=np.std(entire_samples)-np.std(private_samples)
std_diff_mean, std_diff_std=np.mean(std_diff_replicate), np.std(std_diff_replicate)
conf_interval=[std_diff_mean-1.96*std_diff_std,std_diff_mean+1.96*std_diff_std]
print(conf_interval)
np.random.seed(42)
np.random.choice(entire_home, entire_number)<jupyter_output><empty_output><jupyter_text>95% confidence interval for the difference between the private room and entire home/apt is [-1381,-981].<jupyter_code>np.random.seed(42)
entire_shifted=entire_home-np.mean(entire_home)+np.mean(private_room)
def permutation_sample(data1, data2):
    data3=np.random.permutation(np.concatenate((data1,data2)))
    perm_sample_1=data3[:len(data1)]
    perm_sample_2=data3[len(data1):]
    return perm_sample_1, perm_sample_2
perm_mean_replicates=np.empty(N_rep)
for i in range(N_rep):
    perm_private, perm_entire=permutation_sample(private_room, entire_shifted)
    perm_mean_replicates[i]=np.mean(perm_private)-np.mean(perm_entire)
mean_diff=np.empty(N_rep)
for i in range(N_rep):
    mean_diff[i]=np.mean(private_samples)-np.mean(np.random.choice(entire_shifted, len(entire_shifted)))
p_value_permutation=np.sum(perm_mean_replicates>=mean_diff_observe)/len(perm_mean_replicates)
print('p_val_permuation is %s' % p_value_permutation)
p_val=(np.sum(mean_diff)>=mean_diff_observe)/len(mean_diff)
print('p value is %s'% p_val)<jupyter_output>p value is 0.0001
StarcoderdataJupyterStructuredCleanDedup