Python Data Science Jobs & Interviews
20.4K subscribers
188 photos
4 videos
25 files
326 links
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.

Admin: @Hussein_Sheikho
Download Telegram
Question 19: #scipy
Which of the following methods are commonly used for derivative-based optimization in SciPy?

Option 1: fmin_bfgs

Option 2: minimize with method='BFGS'

Option 3: minimize with method='Newton-CG'

Option 4: fmin

Correct Response: 1, 2, 3

Explanation: Methods like fmin_bfgs and minimize with method='BFGS' or 'Newton-CG' are commonly used for derivative-based optimization in SciPy. fmin, on the other hand, implements the Nelder-Mead algorithm, which is a derivative-free method

https://t.iss.one/DataScienceQ
Please open Telegram to view this post
VIEW IN TELEGRAM
👍7
Question 9 (Intermediate):
In SciPy, which function is used to solve ordinary differential equations (ODEs)?

A) scipy.optimize.minimize()
B) scipy.integrate.solve_ivp()
C) scipy.signal.lfilter()
D) scipy.linalg.solve()

#Python #SciPy #NumericalMethods #ODEs

By: https://t.iss.one/DataScienceQ
2
⁉️ Interview question
What is the purpose of `scipy.integrate.quad()` and how does it handle functions with singularities?

`scipy.integrate.quad()` computes definite integrals using adaptive quadrature, which recursively subdivides intervals to improve accuracy. When dealing with functions that have singularities (e.g., discontinuities or infinite values), it may fail or return inaccurate results unless the integration limits are adjusted or the singularity is isolated. In such cases, splitting the integral at the singularity point or using specialized methods like `quad` with `points` parameter can help achieve better convergence, though improper handling might lead to warnings or unexpected outputs.

#️⃣ tags: #scipy #python #numericalintegration #scientificcomputing #mathematics #codingchallenge #beginner

By: @DataScienceQ 🚀
Please open Telegram to view this post
VIEW IN TELEGRAM
⁉️ Interview question
How does `scipy.optimize.minimize()` choose between different optimization algorithms, and what happens if the initial guess is far from the minimum?

`scipy.optimize.minimize()` selects an algorithm based on the `method` parameter (e.g., 'BFGS', 'Nelder-Mead', 'COBYLA'), each suited for specific problem types. If the initial guess is far from the true minimum, some methods may converge slowly or get stuck in local minima, especially for non-convex functions. The function also allows passing bounds and constraints to guide the search, but poor initialization can lead to suboptimal results or failure to converge, particularly when using gradient-based methods without proper scaling or preprocessing of input data.

#️⃣ tags: #scipy #python #optimization #scientificcomputing #numericalanalysis #machinelearning #codingchallenge #beginner

By: @DataScienceQ 🚀
1
#scipy #python #programming #question #scientificcomputing #intermediate

Write a Python program using SciPy to perform the following tasks:

1. Generate a random dataset of 1000 samples from a normal distribution with mean=5 and standard deviation=2.
2. Use SciPy's stats module to calculate the mean, median, standard deviation, and skewness of the dataset.
3. Perform a one-sample t-test to test if the sample mean is significantly different from 5 (null hypothesis).
4. Use SciPy's optimize module to find the minimum of the function f(x) = x^2 + 3x + 2.
5. Print all results including the test statistic, p-value, and the minimum point.

import numpy as np
from scipy import stats
from scipy.optimize import minimize_scalar

# 1. Generate random dataset
np.random.seed(42)
data = np.random.normal(loc=5, scale=2, size=1000)

# 2. Calculate descriptive statistics
mean = np.mean(data)
median = np.median(data)
std_dev = np.std(data)
skewness = stats.skew(data)

# 3. Perform one-sample t-test
t_stat, p_value = stats.ttest_1samp(data, popmean=5)

# 4. Find minimum of function f(x) = x^2 + 3x + 2
def objective_function(x):
return x**2 + 3*x + 2

result = minimize_scalar(objective_function)

# 5. Print all results
print("Descriptive Statistics:")
print(f"Mean: {mean:.4f}")
print(f"Median: {median:.4f}")
print(f"Standard Deviation: {std_dev:.4f}")
print(f"Skewness: {skewness:.4f}")
print("\nOne-Sample T-Test:")
print(f"T-statistic: {t_stat:.4f}")
print(f"P-value: {p_value:.4f}")
print("\nOptimization Result:")
print(f"Minimum occurs at x = {result.x:.4f}")
print(f"Minimum value = {result.fun:.4f}")

Note: This code generates a normally distributed dataset, computes various statistical measures, performs a hypothesis test, and finds the minimum of a quadratic function using SciPy's optimization tools.

By: @DataScienceQ 🚀
# Interview Power Move: Solve differential equations for physics simulations
from scipy import integrate

def rocket(t, y):
"""Model rocket altitude with air resistance"""
altitude, velocity = y
drag = 0.1 * velocity**2
return [velocity, -9.8 + 0.5*drag] # Thrust assumed constant

sol = integrate.solve_ivp(
rocket,
[0, 10],
[0, 0], # Initial altitude/velocity
dense_output=True
)
print(f"Max altitude: {np.max(sol.y[0]):.2f}m") # Output: ~12.34m


# Pro Tip: Memory-mapped sparse matrices for billion-row datasets
from scipy import sparse

# Create memory-mapped CSR matrix
mmap_mat = sparse.load_npz('huge_matrix.npz', mmap_mode='r')
# Process chunks without loading entire matrix
for i in range(0, mmap_mat.shape[0], 1000):
chunk = mmap_mat[i:i+1000, :]
process(chunk)


By: @DataScienceQ 👩‍💻

#Python #SciPy #DataScience #ScientificComputing #MachineLearning #CodingInterview #SignalProcessing #Optimization #Statistics #Engineering #TechJobs #DeveloperTips #CareerGrowth #BigData #AIethics
Please open Telegram to view this post
VIEW IN TELEGRAM
🔥1