Skip to content

Instantly share code, notes, and snippets.

View dradecic's full-sized avatar

Dario Radečić dradecic

View GitHub Profile
@dradecic
dradecic / img_dnl_2_download.py
Created September 17, 2019 10:43
img_dnl_2_download
# STEP 1
BASE_DIR = 'images/'
# STEP 2
SUB_DIRS = [topic + '/' for topic in df['Topic'].unique()]
# Print a message to the user
print('Image Download Started...')
start_time = datetime.datetime.now()
@dradecic
dradecic / py-bokeh_12_route_final.py
Created August 25, 2019 09:21
Python-Bokeh - Gist 12: Route final
@app.route('/', methods=['GET', 'POST'])
def chart():
selected_class = request.form.get('dropdown-select')
if selected_class == 0 or selected_class == None:
survived_chart, title_chart, hist_age = redraw(1)
else:
survived_chart, title_chart, hist_age = redraw(selected_class)
script_survived_chart, div_survived_chart = components(survived_chart)
@dradecic
dradecic / rfecv_4_rfecv.py
Created September 1, 2019 16:24
rfecv_4_rfecv
X = data.drop('Survived', axis=1)
target = data['Survived']
rfc = RandomForestClassifier(random_state=101)
rfecv = RFECV(estimator=rfc, step=1, cv=StratifiedKFold(10), scoring='accuracy')
rfecv.fit(X, target)
@dradecic
dradecic / ara_2_woe_iv_function.py
Created September 9, 2019 12:51
ara_2_woe_iv_function
def calculate_woe_iv(dataset, feature, target):
lst = []
for i in range(dataset[feature].nunique()):
val = list(dataset[feature].unique())[i]
lst.append({
'Value': val,
'All': dataset[dataset[feature] == val].count()[feature],
'Good': dataset[(dataset[feature] == val) & (dataset[target] == 0)].count()[feature],
'Bad': dataset[(dataset[feature] == val) & (dataset[target] == 1)].count()[feature]
})
@dradecic
dradecic / rfecv_3_correlations.py
Created September 1, 2019 16:06
rfecv_3_correlations
correlated_features = set()
correlation_matrix = data.drop('Survived', axis=1).corr()
for i in range(len(correlation_matrix.columns)):
for j in range(i):
if abs(correlation_matrix.iloc[i, j]) > 0.8:
colname = correlation_matrix.columns[i]
correlated_features.add(colname)
@dradecic
dradecic / linreg_gradient_descent.py
Created October 13, 2019 07:58
linreg_gradient_descent
b0, b1 = 0.0, 1.0
lr = 0.001
epochs = 10000
error = []
# run 10000 times
for epoch in range(epochs):
# initialize to 0 -> cost of epoch, Jb_0, Jb_1
epoch_cost, cost_b0, cost_b1 = 0, 0, 0
@dradecic
dradecic / scraping-1-script.py
Created September 12, 2019 08:36
scraping-1-script
def get_books(topic_list):
# Generate full URLs from the argument provided by user
all_urls = []
for topic in topic_list:
all_urls.append('http://books.toscrape.com/catalogue/category/books/{}/index.html'.format(topic))
# Instantiate an empty list for holding the dictionary objects
all_books = []
@dradecic
dradecic / recommender1_8_scatter.py
Created September 29, 2019 10:34
recommender1_8_scatter
ratings_df = pd.DataFrame()
ratings_df['Mean_Rating'] = data.groupby('title')['rating'].mean().values
ratings_df['Num_Ratings'] = data.groupby('title')['rating'].count().values
fig, ax = plt.subplots(figsize=(14, 7))
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.set_title('Rating vs. Number of Ratings', fontsize=24, pad=20)
ax.set_xlabel('Rating', fontsize=16, labelpad=20)
@dradecic
dradecic / recommender1_7_top10.py
Created September 29, 2019 10:29
recommender1_7_top10
data.sort_values(by='numRatings', ascending=False).drop_duplicates('movieId')[:10]
@dradecic
dradecic / recommender1_6_num_ratings.py
Created September 29, 2019 10:28
recommender1_6_num_ratings
num_ratings = pd.DataFrame(data.groupby('movieId').count()['rating']).reset_index()
data = pd.merge(left=data, right=num_ratings, on='movieId')
data.rename(columns={'rating_x': 'rating', 'rating_y': 'numRatings'}, inplace=True)