A. Logarithm cannot be applied to zero or negative numbers
B. np.log() requires integer inputs only
C. Missing import statement for pandas
D. Syntax error in the code
Solution
Step 1: Identify input values for log
Values include -5 and 0, which are invalid for logarithm since log is undefined for zero or negative numbers.
Step 2: Understand error cause
Applying np.log to these values causes a runtime warning or error due to invalid input domain.
Final Answer:
Logarithm cannot be applied to zero or negative numbers -> Option A
Quick Check:
Log undefined for ≤ 0 values = C [OK]
Hint: Log only works on positive numbers, not zero or negatives [OK]
Common Mistakes:
Thinking np.log needs integers only
Confusing missing imports as cause
Assuming syntax error without checking inputs
5. You have a dataset with a column 'sales' that contains positive values but also some zeros. You want to apply a log transformation to reduce skewness. Which approach is best to avoid errors and keep data meaningful?
hard
A. Remove all zero values and then apply log: df = df[df['sales'] > 0]; df['log_sales'] = np.log(df['sales'])
B. Replace zeros with -1 before log: df['log_sales'] = np.log(df['sales'].replace(0, -1))
C. Add 1 to all values before log: df['log_sales'] = np.log(df['sales'] + 1)
D. Apply log directly without changes: df['log_sales'] = np.log(df['sales'])
Solution
Step 1: Understand log domain restrictions
Log cannot be applied to zero or negative values, so zeros cause errors.
Step 2: Choose a method to handle zeros
Adding 1 shifts all values positive, allowing log transform without error and preserving data.
Step 3: Evaluate other options
Replacing zeros with -1 causes errors; removing zeros loses data; applying log directly errors on zeros.
Final Answer:
Add 1 to all values before log: df['log_sales'] = np.log(df['sales'] + 1) -> Option C
Quick Check:
Add 1 to avoid log(0) errors = A [OK]
Hint: Add 1 to data before log to handle zeros safely [OK]
Common Mistakes:
Replacing zeros with negative numbers
Dropping zeros losing important data
Applying log directly on zeros
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.