-
Notifications
You must be signed in to change notification settings - Fork 2
Sourcery Starbot ⭐ refactored jobindj/data-science-from-scratch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| if years_experience < 3.0: return "paid" | ||
| elif years_experience < 8.5: return "unpaid" | ||
| else: return "paid" | ||
| if years_experience < 3.0 or years_experience >= 8.5: return "paid" | ||
| else: return "unpaid" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function predict_paid_or_unpaid refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if)
| document_lengths = map(len, documents) | ||
|
|
||
| distinct_words = set(word for document in documents for word in document) | ||
| distinct_words = {word for document in documents for word in document} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 181-181 refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension)
|
|
||
| if n % 2 == 1: | ||
| # if odd, return the middle value | ||
| return sorted_v[midpoint] | ||
| else: | ||
| # if even, return the average of the middle values | ||
| lo = midpoint - 1 | ||
| hi = midpoint | ||
| return (sorted_v[lo] + sorted_v[hi]) / 2 | ||
| # if even, return the average of the middle values | ||
| lo = midpoint - 1 | ||
| hi = midpoint | ||
| return (sorted_v[lo] + sorted_v[hi]) / 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function median refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else)
| c1, c2 = min([(cluster1, cluster2) | ||
| for i, cluster1 in enumerate(clusters) | ||
| for cluster2 in clusters[:i]], | ||
| key=lambda p: cluster_distance(p[0], p[1], distance_agg)) | ||
| c1, c2 = min( | ||
| ( | ||
| (cluster1, cluster2) | ||
| for i, cluster1 in enumerate(clusters) | ||
| for cluster2 in clusters[:i] | ||
| ), | ||
| key=lambda p: cluster_distance(p[0], p[1], distance_agg), | ||
| ) | ||
|
|
||
|
|
||
| # remove them from the list of clusters | ||
| clusters = [c for c in clusters if c != c1 and c != c2] | ||
| clusters = [c for c in clusters if c not in [c1, c2]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function bottom_up_cluster refactored with the following changes:
- Replace multiple comparisons of same variable with
inoperator (merge-comparisons) - Replace unneeded comprehension with generator (
comprehension-to-generator)
| if years_experience < 3.0: return "paid" | ||
| elif years_experience < 8.5: return "unpaid" | ||
| else: return "paid" | ||
| if years_experience < 3.0 or years_experience >= 8.5: return "paid" | ||
| else: return "unpaid" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function predict_paid_or_unpaid refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if)
| for i in range(10): | ||
| for _ in range(10): | ||
| binary.append(x % 2) | ||
| x = x // 2 | ||
| x //= 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function binary_encode refactored with the following changes:
- Replace assignment with augmented assignment (
aug-assign) - Replace unused for index with underscore (
for-index-underscore)
|
|
||
| # training data | ||
| xs = [[0., 0], [0., 1], [1., 0], [1., 1]] | ||
| ys = [[0.], [1.], [1.], [0.]] | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function main refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
| document_lengths = [len(document) for document in documents] | ||
|
|
||
| distinct_words = set(word for document in documents for word in document) | ||
| distinct_words = {word for document in documents for word in document} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 208-208 refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension)
| import tqdm | ||
|
|
||
| for iter in tqdm.trange(1000): | ||
| for _ in tqdm.trange(1000): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 252-252 refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore)
|
|
||
| num_epochs = 10000 | ||
| random.seed(0) | ||
|
|
||
| guess = [random.random(), random.random()] # choose random value to start | ||
|
|
||
| learning_rate = 0.00001 | ||
|
|
||
|
|
||
| with tqdm.trange(num_epochs) as t: | ||
| learning_rate = 0.00001 | ||
|
|
||
| for _ in t: | ||
| alpha, beta = guess | ||
|
|
||
| # Partial derivative of loss with respect to alpha | ||
| grad_a = sum(2 * error(alpha, beta, x_i, y_i) | ||
| for x_i, y_i in zip(num_friends_good, | ||
| daily_minutes_good)) | ||
|
|
||
| # Partial derivative of loss with respect to beta | ||
| grad_b = sum(2 * error(alpha, beta, x_i, y_i) * x_i | ||
| for x_i, y_i in zip(num_friends_good, | ||
| daily_minutes_good)) | ||
|
|
||
| # Compute loss to stick in the tqdm description | ||
| loss = sum_of_sqerrors(alpha, beta, | ||
| num_friends_good, daily_minutes_good) | ||
| t.set_description(f"loss: {loss:.3f}") | ||
|
|
||
| # Finally, update the guess | ||
| guess = gradient_step(guess, [grad_a, grad_b], -learning_rate) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function main refactored with the following changes:
- Move assignments closer to their usage (
move-assign)
| for i in t: | ||
| # i is prime if no smaller prime divides it. | ||
| i_is_prime = not any(i % p == 0 for p in primes) | ||
| i_is_prime = all(i % p != 0 for p in primes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function remove_projection_from_vector.main.primes_up_to refactored with the following changes:
- Invert any/all to simplify comparisons (
invert-any-all)
Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨
Here's your pull request refactoring your most popular Python repo.
If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.
Review changes via command line
To manually merge these changes, make sure you're on the
masterbranch, then run: