-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkickstarter_models.py
596 lines (433 loc) · 21.2 KB
/
kickstarter_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 5 03:47:33 2023
@author: cara-lifarrell
"""
# Individual Project
## Cara-Li Farrell (261051787)
###############################################################################
###############################################################################
############################ Classification Model #############################
###############################################################################
###############################################################################
###############################################################################
################################ Test Dataset #################################
###############################################################################
################################### Set Up ####################################
# Importing libraries
import pandas as pd
# Importing dataset
kickstarter = pd.read_excel("Kickstarter.xlsx")
############################### Preprocessing #################################
# 0. Moving the y variable as the first column + setting up y variable
## state ##
columns_order = ['state'] + [col for col in kickstarter.columns if col != 'state']
kickstarter = kickstarter[columns_order]
# Dropping 'suspended' and 'canceled' state values
kickstarter = kickstarter.drop(kickstarter[kickstarter['state'] == 'suspended'].index)
kickstarter = kickstarter.drop(kickstarter[kickstarter['state'] == 'canceled'].index)
# Checking the remaining possible values
kickstarter.shape
state_counts = kickstarter['state'].value_counts()
print(state_counts)
# Making it a binary variable, where 0: failed and 1: successful
for index, row in kickstarter.iterrows():
if row['state'] == 'successful':
kickstarter.at[index, 'state'] = 1
else:
kickstarter.at[index, 'state'] = 0
# Check unique state values
unique_states = kickstarter['state'].unique()
print(unique_states)
# 1. Duplicates
kickstarter[kickstarter.duplicated()].shape # there are no duplicates
# Drop duplicates
kickstarter = kickstarter.drop_duplicates()
kickstarter.shape
# 2. Irrelevant columns
# Drop unecessary columns
kickstarter = kickstarter.drop(columns=['id',
'name',
'pledged',
'disable_communication',
'currency',
'deadline',
'state_changed_at',
'created_at',
'launched_at',
'staff_pick',
'backers_count',
'usd_pledged',
'spotlight',
'name_len_clean',
'blurb_len_clean',
'state_changed_at_weekday',
'state_changed_at_month',
'state_changed_at_day',
'state_changed_at_yr',
'state_changed_at_hr',
'launch_to_state_change_days'], axis=1)
# Drop columns with insignificant scores from the feature importance score
kickstarter = kickstarter.drop(columns=['country',
'deadline_weekday',
'created_at_weekday',
'launched_at_weekday'], axis=1)
# 3. Missing values
# Checking missing values
print(kickstarter.isnull().sum()) # category, name_len, and blurb_len have missing values
# Drop missing values
kickstarter = kickstarter.dropna()
kickstarter.shape
print(kickstarter.isnull().sum())
# 4. Miscellaneous
## goal ##
# Create a new column called goal_usd with the USD value of the goal
kickstarter['goal_usd'] = kickstarter['goal']*kickstarter['static_usd_rate']
# Drop the old goal column and static_usd_rate
kickstarter = kickstarter.drop(columns=['goal'], axis=1)
kickstarter = kickstarter.drop(columns=['static_usd_rate'], axis=1)
# 5. Dummify the categorical variables
# Remaining categorical columns
categorical_columns = ['category']
#'country',
#'deadline_weekday',
#'created_at_weekday',
#'launched_at_weekday']
# Create dummy variables for the specified columns
kickstarter = pd.get_dummies(kickstarter, columns=categorical_columns)
# Make sure all columns are numeric
kickstarter.dtypes
for column in kickstarter.columns:
kickstarter[column] = pd.to_numeric(kickstarter[column], errors='coerce')
kickstarter.dtypes
############################ Classification Model #############################
########################## Setting up the Variables ###########################
# Constructing the variables
X = kickstarter.iloc[:,1:]
y = kickstarter['state']
# Split the data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 5)
###################### Gradient Boosting Algorithm Model ######################
# Building the model
from sklearn.ensemble import GradientBoostingClassifier
gbt = GradientBoostingClassifier(random_state = 5)
model_gbt = gbt.fit(X_train, y_train)
# Print feature importance
feature_importance_df = pd.DataFrame(list(zip(X.columns, model_gbt.feature_importances_)), columns = ['predictor','feature importance'])
print(feature_importance_df)
'''
# Finding the optimal combinations of hyperparameters using GridSearchCV
from sklearn.model_selection import GridSearchCV
# Define the hyperparameter grid
param_grid_gbt = {
'n_estimators': [50, 100, 150, 200], # Vary from 50 to 200 with increments of 50
'max_features': [3, 4, 5, 6], # Vary from 3 to 6
'min_samples_leaf': [1, 2, 3, 4] # Vary from 1 to 4
}
# Create the GridSearchCV object
grid_search_gbt = GridSearchCV(estimator=gbt, param_grid=param_grid_gbt, scoring='accuracy', cv=5, verbose=True)
# Fit the GridSearchCV object to your training data
gbt_model = grid_search_gbt.fit(X_train, y_train)
# Retrieve the best hyperparameters and model
best_params_gbt = grid_search_gbt.best_params_
best_model_gbt = grid_search_gbt.best_estimator_
# Print the best combination of hyperparameters
print(best_params_gbt)
# Evaluate the best model on your test data
accuracy_gbt = grid_search_gbt.best_score_
# Print the accuracy score
print(accuracy_gbt) # 0.7629604672829723
'''
# Final model with optimal parameters
gbt = GradientBoostingClassifier(n_estimators=200, max_features=4, min_samples_leaf=1, random_state=5)
model_gbt = gbt.fit(X_train, y_train)
# Calculate accuracy score
y_test_pred = model_gbt.predict(X_test)
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_test_pred)
###############################################################################
############################### Grading Dataset ###############################
###############################################################################
################################### Set Up ####################################
# Importing libraries
import pandas as pd
# Importing dataset
kickstarter_grading = pd.read_excel("Kickstarter-Grading-Sample.xlsx")
############################### Preprocessing #################################
# 0. Moving the y variable as the first column + setting up y variable
## state ##
columns_order2 = ['state'] + [col for col in kickstarter_grading.columns if col != 'state']
kickstarter_grading = kickstarter_grading[columns_order2]
# Dropping 'suspended' and 'canceled' state values
kickstarter_grading = kickstarter_grading.drop(kickstarter_grading[kickstarter_grading['state'] == 'suspended'].index)
kickstarter_grading = kickstarter_grading.drop(kickstarter_grading[kickstarter_grading['state'] == 'canceled'].index)
# Checking the remaining possible values
kickstarter_grading.shape
state_counts2 = kickstarter_grading['state'].value_counts()
print(state_counts2)
# Making it a binary variable, where 0: failed and 1: successful
for index, row in kickstarter_grading.iterrows():
if row['state'] == 'successful':
kickstarter_grading.at[index, 'state'] = 1
else:
kickstarter_grading.at[index, 'state'] = 0
# Check unique state values
unique_states2 = kickstarter_grading['state'].unique()
print(unique_states2)
# 1. Duplicates
kickstarter_grading[kickstarter_grading.duplicated()].shape # there are no duplicates
# Drop duplicates
kickstarter_grading = kickstarter_grading.drop_duplicates()
kickstarter_grading.shape
# 2. Irrelevant columns
# Drop unecessary columns
kickstarter_grading = kickstarter_grading.drop(columns=['id',
'name',
'pledged',
'disable_communication',
'currency',
'deadline',
'state_changed_at',
'created_at',
'launched_at',
'staff_pick',
'backers_count',
'usd_pledged',
'spotlight',
'name_len_clean',
'blurb_len_clean',
'state_changed_at_weekday',
'state_changed_at_month',
'state_changed_at_day',
'state_changed_at_yr',
'state_changed_at_hr',
'launch_to_state_change_days'], axis=1)
# Drop columns with insignificant scores from the feature importance score
kickstarter_grading = kickstarter_grading.drop(columns=['country',
'deadline_weekday',
'created_at_weekday',
'launched_at_weekday'], axis=1)
# 3. Missing values
# Checking missing values
print(kickstarter_grading.isnull().sum()) # category, name_len, and blurb_len have missing values
# Drop missing values
kickstarter_grading = kickstarter_grading.dropna()
kickstarter_grading.shape
print(kickstarter_grading.isnull().sum())
# 4. Miscellaneous
## goal ##
# Create a new column called goal_usd with the USD value of the goal
kickstarter_grading['goal_usd'] = kickstarter_grading['goal']*kickstarter_grading['static_usd_rate']
# Drop the old goal column and static_usd_rate
kickstarter_grading = kickstarter_grading.drop(columns=['goal'], axis=1)
kickstarter_grading = kickstarter_grading.drop(columns=['static_usd_rate'], axis=1)
# 5. Dummify the categorical variables
# Remaining categorical columns
categorical_columns2 = ['category']
#'country',
#'deadline_weekday',
#'created_at_weekday',
#'launched_at_weekday']
# Create dummy variables for the specified columns
kickstarter_grading = pd.get_dummies(kickstarter_grading, columns=categorical_columns2)
# Make sure all columns are numeric
kickstarter_grading.dtypes
for column in kickstarter_grading.columns:
kickstarter_grading[column] = pd.to_numeric(kickstarter_grading[column], errors='coerce')
kickstarter_grading.dtypes
############################ Classification Model #############################
########################## Setting up the Variables ###########################
# Constructing the variables
X_grading = kickstarter_grading.iloc[:,1:]
y_grading = kickstarter_grading['state']
###################### Gradient Boosting Algorithm Model ######################
# Apply the model previously trained to the grading data
y_grading_pred = model_gbt.predict(X_grading)
# Calculate the accuracy score
from sklearn.metrics import accuracy_score
accuracy_grading = accuracy_score(y_grading, y_grading_pred)
###############################################################################
###############################################################################
############################## Clustering Model ###############################
###############################################################################
###############################################################################
################################### Set Up ####################################
# Importing libraries
import pandas as pd
# Importing dataset
kickstarter3 = pd.read_excel("Kickstarter.xlsx")
############################### Preprocessing #################################
# 0. Moving the y variable as the first column + setting up y variable
## state ##
columns_order = ['state'] + [col for col in kickstarter3.columns if col != 'state']
kickstarter3 = kickstarter3[columns_order]
# Dropping 'suspended' and 'canceled' state values
kickstarter3 = kickstarter3.drop(kickstarter3[kickstarter3['state'] == 'suspended'].index)
kickstarter3 = kickstarter3.drop(kickstarter3[kickstarter3['state'] == 'canceled'].index)
# Checking the remaining possible values
kickstarter3.shape
state_counts = kickstarter3['state'].value_counts()
print(state_counts)
# Making it a binary variable, where 0: failed and 1: successful
for index, row in kickstarter3.iterrows():
if row['state'] == 'successful':
kickstarter3.at[index, 'state'] = 1
else:
kickstarter3.at[index, 'state'] = 0
# Check unique state values
unique_states = kickstarter3['state'].unique()
print(unique_states)
# 1. Duplicates
kickstarter3[kickstarter3.duplicated()].shape # there are no duplicates
# Drop duplicates
kickstarter3 = kickstarter3.drop_duplicates()
kickstarter3.shape
# 2. Irrelevant columns
# Drop unecessary columns
kickstarter3 = kickstarter3.drop(columns=['id',
'name',
'pledged',
'disable_communication',
'currency',
'deadline',
'state_changed_at',
'created_at',
'launched_at',
'name_len_clean',
'blurb_len_clean'], axis=1)
# 3. Missing values
# Checking missing values
print(kickstarter3.isnull().sum()) # category have missing values
# Drop missing values
kickstarter3 = kickstarter3.dropna()
kickstarter3.shape
print(kickstarter3.isnull().sum())
# 4. Miscellaneous
## goal ##
# Create a new column called goal_usd with the USD value of the goal
kickstarter3['goal_usd'] = kickstarter3['goal']*kickstarter3['static_usd_rate']
# Drop the old goal column and static_usd_rate
kickstarter3 = kickstarter3.drop(columns=['goal'], axis=1)
kickstarter3 = kickstarter3.drop(columns=['static_usd_rate'], axis=1)
# 5. Dummify the categorical variables
# Remaining categorical columns
categorical_columns = ['country',
'staff_pick',
'category',
'spotlight',
'deadline_weekday',
'state_changed_at_weekday',
'created_at_weekday',
'launched_at_weekday']
# Create dummy variables for the specified columns
kickstarter3 = pd.get_dummies(kickstarter3, columns=categorical_columns)
############################## Clustering Model ###############################
########################## Setting up the Variables ###########################
# Constructing the variable
X3 = kickstarter3.iloc[:,:]
# Standardize the dataset
from sklearn.preprocessing import StandardScaler
scaler3 = StandardScaler()
X_std3 = scaler3.fit_transform(X3)
################################ K-Means Model ################################
# Finding the optimal K
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
for i in range (2,8):
kmeans = KMeans(n_clusters=i, random_state=5)
kmeans_model = kmeans.fit(X_std3)
kmeans_labels = kmeans_model.labels_
print(i,':',silhouette_score(X_std3,kmeans_labels))
# Optimal k is 6 with the highest silhouette score (0.08393927635646167)
# Run kmeans with k=6
kmeans = KMeans(n_clusters=6, random_state=5)
kmeans_model = kmeans.fit(X_std3)
kmeans_labels = kmeans_model.labels_
# Counting the number of observations in each cluster
# Turning into a dataframe
df_kmeans_labels = pd.DataFrame({'Cluster_Labels': kmeans_labels})
# Counting observations
for i in sorted(df_kmeans_labels['Cluster_Labels'].unique()):
cluster_count = (df_kmeans_labels['Cluster_Labels'] == i).sum()
print(f"Cluster {i+1}: {cluster_count} observations")
# Generate insights
kmeans_centers = kmeans_model.cluster_centers_
df_kmeans_centers = pd.DataFrame(kmeans_centers, columns=kickstarter3.columns)
# Create a dataframe with the category and the labels
kickstarter3['Cluster_Labels'] = df_kmeans_labels['Cluster_Labels']
# Moving the cluster label column to the first column
columns_order3 = ['Cluster_Labels'] + [col for col in kickstarter3.columns if col != 'Cluster_Labels']
kickstarter3 = kickstarter3[columns_order3]
# Getting the summary statistics of each cluster
# Unique
unique_clusters = kickstarter3['Cluster_Labels'].unique()
# Get actual cluster numbers (they start at 0)
kickstarter3['Cluster_Labels'] = kickstarter3['Cluster_Labels']+1
# Get the summary statistics of each cluster
# Defining a function that will take in the cluster label number we are looking
# for and getting its summary statistics
def cluster_summary_statistics(data, cluster_label):
cluster_data = data[data['Cluster_Labels'] == cluster_label] # for the rows we want
summary_statistics = cluster_data.describe().transpose()
return summary_statistics
cluster1 = cluster_summary_statistics(kickstarter3, 1)
cluster2 = cluster_summary_statistics(kickstarter3, 2)
cluster3 = cluster_summary_statistics(kickstarter3, 3)
cluster4 = cluster_summary_statistics(kickstarter3, 4)
cluster5 = cluster_summary_statistics(kickstarter3, 5)
cluster6 = cluster_summary_statistics(kickstarter3, 6)
# Counting the number of each status (success or failure) in each cluster
def count_states_in_clusters(dataset):
# Check if required columns are present
required_columns = ["state", "Cluster_Labels"]
if not all(col in dataset.columns for col in required_columns):
raise ValueError("Required columns 'state' and 'Cluster_Labels' are missing.")
# Count the number of each 'state' for each 'Cluster_Labels'
count_table = dataset.groupby(['Cluster_Labels', 'state']).size().unstack(fill_value=0)
return count_table
state_counts = count_states_in_clusters(kickstarter3)
print(state_counts)
# Success ratio
success_ratios = state_counts[1]/(state_counts[0]+state_counts[1])
# Plotting average goal and pledged
import matplotlib.pyplot as plt
# Data averages
pledged = [18575, 15603, 15747, 14979, 17454, 12769]
goal = [68574, 67971, 115635, 73750, 57919, 85465]
cluster_labels = ["Cluster 1", "Cluster 2", "Cluster 3", "Cluster 4", "Cluster 5", "Cluster 6"]
# Plotting
plt.figure(figsize=(10, 6))
for i, (g, p, label) in enumerate(zip(goal, pledged, cluster_labels)):
plt.scatter(g, p, label=label, marker='o')
plt.text(g, p, f'{label}\n({g}, {p})', fontsize=8, ha='right', va='bottom')
plt.title('Pledged vs Goal with Cluster Labels')
plt.xlabel('Goal')
plt.ylabel('Pledged')
plt.grid(True)
plt.legend()
plt.show()
'''
################### Hierarchical Agglomerative Clustering #####################
# Finding the optimal k
from sklearn.cluster import AgglomerativeClustering
for i in range (2,8):
hac = AgglomerativeClustering(n_clusters=i, affinity="euclidean", linkage="complete")
hac_model = hac.fit(X_std3)
hac_labels = hac_model.labels_
print(i,':',silhouette_score(X_std3,hac_labels))
# Optimal K is 2 with the highest silhouette score (0.8803809459407886)
# Run hirearchical with k=2
hac = AgglomerativeClustering(n_clusters=2, affinity='euclidean', linkage='complete')
hac_model = hac.fit_predict(X_std3)
hac_labels = hac.labels_
# Counting the number of observations in each cluster
# Turning into a dataframe
df_hac_labels = pd.DataFrame({'Cluster_Labels': hac_labels})
# Counting the number of observations in each cluster
for i in sorted(df_hac_labels['Cluster_Labels'].unique()):
cluster_count = (df_hac_labels['Cluster_Labels'] == i).sum()
print(f"Cluster {i+1}: {cluster_count} observations")
# There is only 1 observation in cluster 2
'''