-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
214 lines (154 loc) · 4.62 KB
/
main.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
import pandas as pd
import numpy as np
import pickle
WELCOME_MESSAGE =f"""
{'-'*30}
Welcome to kickstarter prediction project."""
df = pd.read_csv("data/kickstarter_projects.csv")
main_categorys = list(df.Category.unique())
sub_categorys = {cat: list(df.query(f"Category == '{cat}'").Subcategory.unique()) for cat in main_categorys}
# load model
with open("models/decision_tree_4.pkl", "rb") as file:
model = pickle.load(file)
def ask_parameter(name, _type=None, _range=None):
while True:
value = input(">>> ")
if _type is not None:
try:
value = _type(value)
except:
print(f"Your input must be of type {_type.__name__}!")
continue
if _range is not None:
if value not in _range:
print(f"Your input has to be in {_range}!")
continue
return value
def ask_choose(options):
for n, option in enumerate(options):
print(f"{n:>5}: {option}")
while True:
inpt = input(">>> ")
try:
index = int(inpt)
except:
try:
index = options.index(inpt)
except:
print("Not a valid input!")
continue
break
selection = options[index]
return index, selection
# ['Name',--
# 'Country',
# 'Goal',
# 'Pledged',
# 'Backers',
# 'State',--
# 'Duration_days',
# 'Month',
# 'Combined_category',
# 'Name_length']
def get_parameters():
# Name / Name_length
print("What is the name of your project?")
name = ask_parameter("name")
parameters["Name"] = name
parameters["Name_length"] = len(name)
# Country
print("Whats your country?")
idx, country = ask_choose(list(df.Country.unique()))
parameters["Country"] = country
# Goal
print("What is the goal of your project?")
goal = ask_parameter("goal", int, range(1, 100_000_000))
parameters["Goal"] = goal
# Pledged
#parameters["Pledged"] = 0
# Backers
parameters["Backers"] = 0
# State
#parameters["State"] = "Failed"
# Duration_days
print("How many days will your project be online?")
duration = ask_parameter("duration", int, range(1, 100))
parameters["Duration_days"] = duration
# Month
print("On which month will you start?")
month = ask_parameter("starting month", int, range(1, 13))
parameters["Month"] = month
# Combined_category
print("Select your main category:")
main_idx, main_cat = ask_choose(main_categorys)
print(f"Main category: {main_cat}")
print("Select your sub category:")
sub_idx, sub_cat = ask_choose(sub_categorys[main_cat])
print(f"Sub category: {sub_cat}")
combined_category = f"{main_cat} - {sub_cat}"
print(combined_category)
parameters["Combined_category"] = combined_category
return parameters
if __name__ == "__main__":
parameters = {}
print(WELCOME_MESSAGE)
with open("parameters.pkl", "rb") as file:
parameters = pickle.load(file)
# with open("parameters.pkl", "wb") as file:
# pickle.dump(parameters, file)
parameters = {
'Name_length': 14,
'Country': 'Germany',
'Combined_category': 'Games - Playing Cards',
'Goal': 10000,
'Backers': 0,
'Month': 10,
'Duration_days': 30
}
parameters = get_parameters()
# dummy_df = pd.DataFrame(dummy_parameters, index=[0])
print("-"*60)
for key, value in parameters.items():
print(f"{key:>20} - {value:<20}")
# for key, value in dummy_parameters.items():
# print(f"{key:>20} - {value:<20}")
print("-"*60)
print("")
backers = [
5,
10,
25,
50,
75,
100,
125,
150,
175,
200,
250,
300,
500,
1000,
10000,
]
l = []
for backer in backers:
d = parameters.copy()
d["Backers"] = backer
l.append(d)
user_df = pd.DataFrame(l)
predictions = model.predict(user_df)
print(f"You can expect:")
width = 25 + 1 + 3 + 3
#print("-" * width)
print(f"-{' backers ':-<10}-- {'prediction ':-<15}---")
reached = False
for backer, prediction in zip(backers, list(predictions)):
if parameters["Goal"] < prediction and not reached:
print('-'*width)
reached = True
goal_reached_at = backer
print(f"|{backer:>10} | {prediction:>15.2f}$ |")
print("-" * width)
print(f"You need at least {goal_reached_at} backers for your project ;)")
print("")