<jupyter_start><jupyter_text>data['duration']=Nonedata_t['duration']=None for i in range(0,len(data['Duration'])):    if('m' not in data['Duration'][i]):        c=data['Duration'][i]+' 0m'    elif('h' not in data['Duration'][i]):        c='0h '+data['Duration'][i]    else:        c=data['Duration'][i]    a,b=c.split('h')    a=int(a)*60    b=int(b[:-1])    data['duration'][i]=a+b for i in range(0,len(data_t['Duration'])):    if('m' not in data_t['Duration'][i]):        c=data_t['Duration'][i]+' 0m'    elif('h' not in data_t['Duration'][i]):        c='0h '+data_t['Duration'][i]    else:        c=data_t['Duration'][i]    a,b=c.split('h')    a=int(a)*60    b=int(b[:-1])    data_t['duration'][i]=a+b<jupyter_code>sns.stripplot(x='Additional_Info',y='Price',data=data)
plt.xticks(rotation=90)
sns.stripplot(x='Additional_Info',y='Price',data=data)
plt.xticks(rotation=90)
data['Additional_Info'].value_counts().plot(kind='bar')
data_t['Additional_Info'].value_counts().plot(kind='bar')
data['Additional_Info'].replace(to_replace={'1 Long layover':'other','1 Short layover':'other','2 Long layover':'other','Business class':'other','Change airports':'other','Red-eye flight':'other'},inplace=True)
data_t['Additional_Info'].replace(to_replace={'1 Long layover':'other','1 Short layover':'other','2 Long layover':'other','Business class':'other','Change airports':'other','Red-eye flight':'other'},inplace=True)
data['Additional_Info'].replace('No Info','No info',inplace=True)
data_t['Additional_Info'].replace('No Info','No info',inplace=True)
data.head()
data.dropna(inplace=True)
data_t.dropna(inplace=True)
data['Total_Stops'].replace({'non-stop':0,'2 stops':2,'1 stop':1,'3 stops':3,'4 stops':4},inplace=True)
data_t['Total_Stops'].replace({'non-stop':0,'2 stops':2,'1 stop':1,'3 stops':3,'4 stops':4},inplace=True)
data_t['Total_Stops']=data_t['Total_Stops'].astype(int)
df_tr=pd.DataFrame(data=data,columns=['Airline','Source','Destination','Additional_Info','day','month'])
df_te=pd.DataFrame(data=data_t,columns=['Airline','Source','Destination','Additional_Info','day','month'])
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
from sklearn.preprocessing import StandardScaler,RobustScaler
le=LabelEncoder()
df_trn=pd.DataFrame(data=data,columns=['Total_Stops','Duration_hours','Duration_mins','Dep_ti_h','Dep_ti_m','arr_ti_h','arr_ti_m'])
df_tes=pd.DataFrame(data=data_t,columns=['Total_Stops','Duration_hours','Duration_mins','Dep_ti_h','Dep_ti_m','arr_ti_h','arr_ti_m'])
df_trn['check']=0
df_tr=df_tr.append(df_te,ignore_index=True)
df_tes['check']=1
df_trn=df_trn.append(df_tes,ignore_index=True)
y=data['Price']
df_tr=df_tr.apply(le.fit_transform)
#df_te=df_te.apply(le.fit_transform)
x=df_tr.merge(df_trn,right_index=True,left_index=True)
#xx=df_te.merge(df_tes,right_index=True,left_index=True)
df_trn.shape
df_tes.shape
dfs=OneHotEncoder(sparse=True).fit_transform(x)
#dfss=OneHotEncoder(sparse=True).fit_transform(xx)
x.head()
x.shape
y.shape
scaler=StandardScaler()
sca=RobustScaler(quantile_range=(10,90))
#df1=sca.fit_transform(x)
#df2=sca.fit_transform(xx)
df1=x[x['check']==0]
df2=x[x['check']==1]
df1.drop(columns='check',inplace=True)
df2.drop(columns='check',inplace=True)
df2.reset_index(inplace=True)
df2.drop(columns='index',inplace=True)
df1=np.array(df1)
df2=np.array(df2)
import xgboost as xg 
from sklearn.model_selection import train_test_split 
from sklearn.metrics import mean_squared_error as MSE 
#train_X, test_X, train_y, test_y = train_test_split(df1, y, test_size = 0.3, random_state = 123) 
train_X, test_X, train_y, test_y=df1,df2,y,data_s['Price']
# Instantiation 
xgb_r = xg.XGBRegressor(colsample_bytree=0.5, gamma=5, 
                             learning_rate=0.05, max_depth=3, 
                             min_child_weight=1.8, n_estimators=2300,
                             reg_alpha=0.5, reg_lambda=0.9,
                             subsample=0.5, silent=1,
                             random_state =42, nthread = -1) 
  
# Fitting the model 
xgb_r.fit(train_X, train_y) 
  
# Predict the model 
pred = xgb_r.predict(test_X) 
# RMSE Computation 
rmse = np.sqrt(MSE(test_y, pred))
print("RMSE : % f" %(rmse))
df1[1]
df2[5]
xgb_r.score(train_X,train_y)
#from sklearn.ensemble import GradientBoostingRegressor 
#X_train, X_test, y_train, y_test = train_test_split(df1, y,random_state=42, test_size=0.3)
#rf = GradientBoostingRegressor()
#rf.fit(X_train, y_train)
#y_pred = rf.predict(X_test)
#rmse = np.sqrt(MSE(y_test, y_pred))
#print("RMSE : % f" %(rmse))
from sklearn.ensemble import RandomForestRegressor
X_train, X_test, y_train, y_test = train_test_split(df1, y,random_state=42, test_size=0.3)
#X_train, X_test, y_train, y_test = df1,df2,y,data_s['Price']
rf = RandomForestRegressor(random_state=42)
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
rmse = np.sqrt(MSE(y_test, y_pred))
print("RMSE : % f" %(rmse))
rf.score(X_train,y_train)
from sklearn.metrics import r2_score as r2
print(r2(test_y, pred))
print(r2(y_test,y_pred))
rf.predict([[ 4,  2,  1,  0,  0,  1,  1, 18, 20, 18, 15, 12, 35] ])
plt.scatter(pred,test_y,marker=True)
sns.regplot(pred,test_y)
y_pred
y=data["Price"]
data['Price'].skew()
data['Price'].kurt()<jupyter_output><empty_output><jupyter_text>from scipy.stats import norm, skew sns.distplot(np.log1p(data["Price"]) , fit=norm)<jupyter_code>import pickle
# open a file, where you ant to store the data
file4 = open('flight3.pkl', 'wb')
f4 = open('flight3.pkl', 'rb')
# dump information to that file
model=pickle.dump(rf,file4)
forest = pickle.load(f4)
forest.predict([[ 3,  3,  0,  2,  4,  1,  1,  4,  0,  6, 20, 10, 20]])
ss=np.array(X_test)
ss
model
f4
file4<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1863692253644827, 'fraction_numerical': 0.027577727033198666, 'mean_word_length': 6.908333333333333, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '618486', 'license': 'MIT', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2301, 'n_words': 327, 'path': 'flight price predicition.ipynb', 'repo_name': 'Ankush-428/flight-price-prediction-and-book-recommendation', 'size': '5693'}