<jupyter_start><jupyter_text>convert categorical features to numerical featurescategorical_features = ['purpose','info_source','tour_arrangement','age_group',                        'travel_with','main_activity','most_impressing'] One Hot Encoding conversiondata = pd.get_dummies(data, prefix_sep='_', columns = categorical_features)show the shape of the datadata.shapeshow the first five rows data.head()<jupyter_code># import packages 
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
#split dataset into features and target
target = data['total_cost']
features = data.drop('total_cost', axis =1)
#apply SelectKBest class to extract top 27.. best features
bestfeatures = SelectKBest(score_func=chi2, k=21)

#train to find best features
fit = bestfeatures.fit(features,target)

#save in the dataframe 
dfscores = pd.DataFrame(fit.scores_)
dfcolumns = pd.DataFrame(features.columns)

#concat two dataframes for better visualization 
featureScores = pd.concat([dfcolumns,dfscores],axis=1)

#naming the dataframe columns
featureScores.columns = ['Specs','Score'] 

#print 27.. best features 
print(featureScores.nlargest(21,'Score'))<jupyter_output>Specs         Score
0                 country  36581.556608
16         night_mainland  16537.904302
17         night_zanzibar  13530.581942
7             info_source   4127.634475
6           main_activity   3192.275536
5                 purpose   3176.690198
3            total_female   3006.168655
2             travel_with   2551.332359
20        most_impressing   2319.434989
15      package_insurance   1988.989828
9   package_transport_int   1966.785352
13    package_sightseeing   1873.551475
14    package_guided_tour   1842.487958
12   package_transport_tz   1776.001818
11           package_food   1700.899138
10   package_accomodation   1684.297513
8        tour_arrangement   1677.825942
4              total_male   1631.134157
18           payment_mode   1547.821036
1               age_group    984.854423
19          first_trip_tz    658.497640<jupyter_text>main activity is mostly related with the total cost and also other columnsgood thing we didnt mess with it much 😅<jupyter_code># fit and tranform into the 20 best features 
transformer = SelectKBest(chi2, k=21)

#transform from 41 features into top 27.. features
top_20_features = transformer.fit_transform(features, target)

#show the shape 
top_20_features.shape<jupyter_output><empty_output><jupyter_text>Feature Importance<jupyter_code>#import package importing algorithms 
from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier
#create model for training 
#model = RandomForestClassifier()
model = RandomForestClassifier()
model.fit(features,target)

#use inbuilt class feature_importances of tree based classifiers
print(model.feature_importances_) 

#plot graph of feature importances for better visualization
feature_importances = pd.Series(model.feature_importances_, index=features.columns)

# show the first 30 important features 

fig= plt.figure(figsize=(25,25))
sns.set(font_scale = 3)
feature_importances.nlargest(30).plot(kind='barh')
plt.show()<jupyter_output>[0.12935541 0.06940647 0.0650178  0.04984209 0.05235595 0.0444038
 0.06811966 0.07231734 0.00330868 0.01343953 0.00353291 0.00706802
 0.01083983 0.01317079 0.01388198 0.01888908 0.13955782 0.06392916
 0.026623   0.0322887  0.10265197]<jupyter_text>Correlation Matrix with Heatmap<jupyter_code>#get correlations of each features in dataset
plt.figure(figsize=(30,30))

#plot heat map
sns.set(font_scale = 3)
# to show number set annot=True
d = sns.heatmap(data.corr(),annot=False, cmap="RdYlGn")

#save the figure 
figure = d.get_figure()
figure.savefig("heatmap_output.png")

# show the heatamp graph 
d  
# SHOW CORRELATION OF DATA TO THE TARGET COLUMN 
features_corr = pd.DataFrame(abs(data.corr()['total_cost']).sort_values(ascending = False)) 

features_corr<jupyter_output><empty_output><jupyter_text>Let us get to the MODELS NOW<jupyter_code>from sklearn.model_selection import train_test_split

X = np.asarray(data[[
'country',
'age_group',
'travel_with',
'total_female',
'total_male',
'purpose',
'main_activity',
'info_source',
'tour_arrangement',
'package_transport_int',
'package_accomodation',
'package_food',
'package_transport_tz',
'package_sightseeing',
'package_guided_tour',
'package_insurance',
'night_mainland',
'night_zanzibar',
'payment_mode',
'first_trip_tz',
'most_impressing'
    ]])
Y = np.asarray(data['total_cost'])

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, shuffle= True)


from sklearn.linear_model import LinearRegression

lineReg = LinearRegression()
lineReg.fit(X_train, y_train)
print('Score: ', lineReg.score(X_test, y_test))
print('Weights: ', lineReg.coef_)

plt.plot(lineReg.predict(X_test))
plt.plot(y_test)
plt.show()
from sklearn import linear_model
reg = linear_model.Ridge (alpha = 0)
reg.fit(X_train, y_train)
print('Score: ', reg.score(X_test, y_test))
print('Weights: ', reg.coef_)

plt.plot(reg.predict(X_test))
plt.plot(y_test)
plt.show()
scores = []
coefs = []
for i in range(1000):
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, shuffle= True)
    lineReg = LinearRegression()
    lineReg.fit(X_train, y_train)
    scores.append(lineReg.score(X_test, y_test))
    coefs.append(lineReg.coef_)
print('Linear Regression')
print(np.mean(scores))
print(np.mean(coefs, axis=0))

scores = []
coefs = []
for i in range(500):
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, shuffle= True)
    lineReg = linear_model.Ridge (alpha = .0)
    lineReg.fit(X_train, y_train)
    scores.append(lineReg.score(X_test, y_test))
    coefs.append(lineReg.coef_)
print('\nRidge Regression')
print(np.mean(scores))
print(np.mean(coefs, axis=0))
#Lets test a random array of a vector
array = data.values

#let us try to print the vector for row 0
print(array[0])

#let us try to print the total_cost for row 0
print(array[0][21])

#let us predict the total_cost for row 0
reg.predict([[ 89   ,   2   ,   2   ,   1   ,   1   ,   1  ,    7   ,   0   ,   0  ,    0,
      0   ,   0  ,   0    ,  0     , 0     , 0   ,  12   ,   0   ,   0  ,    0,
      2 ]])



# Import data
testdata = pd.read_csv('test.csv')


# print shape 
print('train data shape :', data.shape)

testdata.head()

#remove id feature 

testdata = testdata.drop('ID', axis=1)

testdata.shape<jupyter_output><empty_output><jupyter_text>Check for missing valuesprint('missing values:', testdata.isnull().sum())testdata.head()<jupyter_code>#We remove rows with missing values on the column 'Travel with'
# drop all rows with NaN values
#data.dropna(axis=0,inplace=True)
testdata['travel_with'].fillna(method='bfill', inplace=True)

#We change missing value in the column   to "No comments"
testdata['most_impressing'].fillna('No comments', inplace=True)

#we keep the value from below the cell of the missing value to the missing value 
testdata['total_male'].fillna(method='bfill', inplace=True)



#we keepo the value from above the cell of the missing value to the missing value 
testdata['total_female'].fillna(method='pad', inplace=True)



# Check for missing values
print('missing values:', testdata.isnull().sum())


testdata.head()

# Convert all columns to numerical Data
le = LabelEncoder()

testdata['purpose'] = le.fit_transform(testdata['purpose'])
testdata['country'] = le.fit_transform(testdata['country'])
testdata['age_group'] = le.fit_transform(testdata['age_group'])
testdata['travel_with'] = le.fit_transform(testdata['travel_with'])
testdata['total_female'] = le.fit_transform(testdata['total_female'])
testdata['total_male'] = le.fit_transform(testdata['total_male'])
testdata['info_source'] = le.fit_transform(testdata['info_source'])
testdata['main_activity'] = le.fit_transform(testdata['main_activity'])
testdata['tour_arrangement'] = le.fit_transform(testdata['tour_arrangement'])
testdata['package_transport_int'] = le.fit_transform(testdata['package_transport_int'])
testdata['package_accomodation'] = le.fit_transform(testdata['package_accomodation'])
testdata['package_food'] = le.fit_transform(testdata['package_food'])
testdata['package_transport_tz'] = le.fit_transform(testdata['package_transport_tz'])
testdata['package_sightseeing'] = le.fit_transform(testdata['package_sightseeing'])
testdata['package_guided_tour'] = le.fit_transform(testdata['package_guided_tour'])
testdata['package_insurance'] = le.fit_transform(testdata['package_insurance'])
testdata['night_mainland'] = le.fit_transform(testdata['night_mainland'])
testdata['night_zanzibar'] = le.fit_transform(testdata['night_zanzibar'])
testdata['payment_mode'] = le.fit_transform(testdata['payment_mode'])
testdata['first_trip_tz'] = le.fit_transform(testdata['first_trip_tz'])
testdata['most_impressing'] = le.fit_transform(testdata['most_impressing'])




#show the first five rows 
testdata.head() 
testdata.shape 
my_array = testdata.values

print(my_array[1598])
reg.predict([my_array[1598]])
reg.predict([[1 , 1 , 3 , 3 , 4 , 4 , 7 , 3 , 0 , 1 , 2 , 0 , 0, 0 , 0,  0, 2 , 0 , 0 , 0 , 6]])
#Lets test a random array of a vector
reg.predict([my_array[0]])
submit_file = pd.read_csv('SampleSubmission.csv')
submit_file.head()

print(submit_file)
i = 0
b = 0

while i < 1600:
    
    #print(my_array[i])
    print(reg.predict([my_array[i]]))
    
    results = reg.predict([my_array[i]])
    submit_file['total_cost'][i] = reg.predict([my_array[i]])
    i = i+1
    b = b + 1
print(b)


print(submit_file)<jupyter_output>ID  total_cost
0        tour_1    18779692
1      tour_100    11720465
2     tour_1001     7017919
3     tour_1006     2152066
4     tour_1009    18105296
...         ...         ...
1596   tour_988     1426813
1597   tour_990    25039715
1598   tour_992     -166385
1599   tour_996    -2388897
1600   tour_998     5241422

[1601 rows x 2 columns]
{'fraction_non_alphanumeric': 0.11653718091009989, 'fraction_numerical': 0.06961961456967006, 'mean_word_length': 4.931777378815081, 'pattern_counts': {'":': 0, '<': 23, '<?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': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '211692', 'license': 'MIT', 'n_tokens_mistral': 3920, 'n_tokens_neox': 3417, 'n_words': 765, 'path': 'Untitled.ipynb', 'repo_name': 'fdagreat/Tanzania_Tourism_Prediction', 'size': '9911'}