0% found this document useful (0 votes)
10 views6 pages

3 Data Science Tips That You Might Have Missed 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

3 Data Science Tips That You Might Have Missed 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Weekend Read: 3 Data

Science Tips that You Might


Have Missed
decorator module: Write Shorter Python Decorators
without Nested Functions
Writing a decorator in Python requires a nested function like below:

from time import time, sleep

def time_func(func):
def wrapper(*args, **kwargs):
start_time = time()
func(*args, **kwargs)
end_time = time()
print(
f"""It takes {round(end_time - start_time, 3)} seconds
to execute the function"""
)
return wrapper

@time_func
def test_func1():
sleep(1)

test_func1()

Output:

It takes 1.004 seconds to execute the function


If you want write a Python decorator with only one function instead of nested
functions, try decorator.

In the code below, adding @decorator to a function turns it into a decorator. As the
result, the code used to create a decorator is simpler and easier to read.

from decorator import decorator

@decorator
def time_func_with_decorator(func, *args, **kwargs):
start_time = time()
func(*args, **kwargs)
end_time = time()
print(
f"""It takes {round(end_time - start_time, 3)} seconds
to execute the function"""
)
@time_func_with_decorator
def test_func2():
sleep(1)

Output:

It takes 1.004 seconds to execute the function

Check out other things the decorator library can do.

Link to the source code.


pytest: Don’t Assume. Test your Python Code
When working on a data science project, don’t assume that the output will be what
you expect. Test your assumption. The easiest way to do that in Python is pytest.

For example, in the code below, I use pytest to test my assumption about the output
of the text. When running pytest file_name.py, pytest will run the functions that
start with test_.

By running the test, I know where can my function goes wrong.

from textblob import TextBlob

def extract_sentiment(text: str):


'''Extract sentiment using textblob.
Polarity is within range [-1, 1]'''

text = TextBlob(text)

return text.sentiment.polarity

def test_extract_sentiment_negative():

text = "I do not think this will turn out well"

sentiment = extract_sentiment(text)

assert sentiment < 0

Output:
________________________________ test_extract_sentiment_negative
________________________________

def test_extract_sentiment_negative():

text = "I do not think this will turn out well"

sentiment = extract_sentiment(text)

> assert sentiment < 0


E assert 0.0 < 0

pytest_example.py:17: AssertionError

Check out other examples in my article about pytest.

Link to the source code.


np.where: Replace Elements of a NumPy Array Based on
a Condition
If you want to replace elements of a NumPy array based on a condition, use
np.where.

>>> import numpy as np

>>> arr = np.array([[1, 4, 10, 15], [2, 3, 8, 9]])

# Multiply values that are less than 5 by 2


>>> print(np.where(arr < 5, arr * 2, arr))
[[ 2 8 10 15]
[ 4 6 8 9]]

Link to the source code.

You might also like