Python Question / Quiz;
What is the output of the following Python code, and why? 🤔🚀 Comment your answers below! 👇
What is the output of the following Python code, and why? 🤔🚀 Comment your answers below! 👇
#python #programming #developer #programmer #coding #coder #softwaredeveloper #computerscience #webdev #webdeveloper #webdevelopment #pythonprogramming #pythonquiz #ai #ml #machinelearning #datascience
https://t.iss.one/DataScienceQ
👍4
Python Question / Quiz;
What is the output of the following Python code, and why? 🤔🚀 Comment your answers below! 👇
What is the output of the following Python code, and why? 🤔🚀 Comment your answers below! 👇
#python #programming #developer #programmer #coding #coder #softwaredeveloper #computerscience #webdev #webdeveloper #webdevelopment #pythonprogramming #pythonquiz #ai #ml #machinelearning #datascience
https://t.iss.one/DataScienceQ
👍2❤1
Python Question / Quiz;
What is the output of the following Python code, and why? 🤔🚀 Comment your answers below! 👇
What is the output of the following Python code, and why? 🤔🚀 Comment your answers below! 👇
#python #programming #developer #programmer #coding #coder #softwaredeveloper #computerscience #webdev #webdeveloper #webdevelopment #pythonprogramming #pythonquiz #ai #ml #machinelearning #datascience
https://t.iss.one/DataScienceQ
👍3
Forwarded from Python | Machine Learning | Coding | R
#DataScience #SQL #Python #MachineLearning #Statistics #BusinessAnalytics #ProductCaseStudies #DataScienceProjects #InterviewPrep #LearnDataScience #YouTubeLearning #CodingInterview #MLInterview #SQLProjects #PythonForDataScience
Please open Telegram to view this post
VIEW IN TELEGRAM
❤2
Question 2 (Intermediate):
What is a common use case for the PCA (Principal Component Analysis) algorithm in machine learning?
A) Hyperparameter tuning
B) Data visualization and dimensionality reduction
C) Gradient descent optimization
D) Model ensembling
#MachineLearning #PCA #DimensionalityReduction #MLQuiz #DataScience
What is a common use case for the PCA (Principal Component Analysis) algorithm in machine learning?
A) Hyperparameter tuning
B) Data visualization and dimensionality reduction
C) Gradient descent optimization
D) Model ensembling
#MachineLearning #PCA #DimensionalityReduction #MLQuiz #DataScience
Question 2 (Advanced):
In machine learning with Python, what does the
A) Controls the shuffling applied to the data before splitting
B) Sets the percentage of data to use for testing
C) Determines the number of CPU cores to use
D) Specifies the type of ML algorithm to apply
#Python #MachineLearning #ScikitLearn #DataScience
In machine learning with Python, what does the
random_state parameter do in scikit-learn's train_test_split() function?A) Controls the shuffling applied to the data before splitting
B) Sets the percentage of data to use for testing
C) Determines the number of CPU cores to use
D) Specifies the type of ML algorithm to apply
#Python #MachineLearning #ScikitLearn #DataScience
Question 13 (Intermediate):
In NumPy, what is the difference between
A) The first is a 1D array, the second is a 2D row vector
B) The first is faster to compute
C) The second automatically transposes the data
D) They are identical in memory usage
#Python #NumPy #Arrays #DataScience
✅ By: https://t.iss.one/DataScienceQ
In NumPy, what is the difference between
np.array([1, 2, 3]) and np.array([[1, 2, 3]])? A) The first is a 1D array, the second is a 2D row vector
B) The first is faster to compute
C) The second automatically transposes the data
D) They are identical in memory usage
#Python #NumPy #Arrays #DataScience
✅ By: https://t.iss.one/DataScienceQ
❤3
🚀 Comprehensive Guide: How to Prepare for a Graph Neural Networks (GNN) Job Interview – 350 Most Common Interview Questions
Read: https://hackmd.io/@husseinsheikho/GNN-interview
#GNN #GraphNeuralNetworks #MachineLearning #DeepLearning #AI #DataScience #PyTorchGeometric #DGL #NodeClassification #LinkPrediction #GraphML
Read: https://hackmd.io/@husseinsheikho/GNN-interview
#GNN #GraphNeuralNetworks #MachineLearning #DeepLearning #AI #DataScience #PyTorchGeometric #DGL #NodeClassification #LinkPrediction #GraphML
✉️ Our Telegram channels: https://t.iss.one/addlist/0f6vfFbEMdAwODBk📱 Our WhatsApp channel: https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
Please open Telegram to view this post
VIEW IN TELEGRAM
❤5
Python.pdf
488 KB
👨🏻💻 An excellent note that teaches everything from basic concepts to building professional projects with Python.
➖➖➖➖➖➖➖➖➖➖➖➖➖
Please open Telegram to view this post
VIEW IN TELEGRAM
❤2🔥1
1. What is the primary data structure in pandas?
2. How do you create a DataFrame from a dictionary?
3. Which method is used to read a CSV file in pandas?
4. What does the
5. How can you check the data types of columns in a DataFrame?
6. Which function drops rows with missing values in pandas?
7. What is the purpose of the
8. How do you filter rows based on a condition in pandas?
9. What does the
10. How can you sort a DataFrame by a specific column?
11. Which method is used to rename columns in pandas?
12. What is the difference between
13. How do you handle duplicate rows in pandas?
14. What function converts a column to datetime format?
15. How do you apply a custom function to a DataFrame?
16. What is the use of the
17. How can you concatenate two DataFrames?
18. What does the
19. How do you calculate summary statistics in pandas?
20. Which method is used to export a DataFrame to a CSV file?
#️⃣ #pandas #dataanalysis #python #dataframe #coding #programming #datascience
By: t.iss.one/DataScienceQ 🚀
2. How do you create a DataFrame from a dictionary?
3. Which method is used to read a CSV file in pandas?
4. What does the
head() function do in pandas? 5. How can you check the data types of columns in a DataFrame?
6. Which function drops rows with missing values in pandas?
7. What is the purpose of the
merge() function in pandas? 8. How do you filter rows based on a condition in pandas?
9. What does the
groupby() method do? 10. How can you sort a DataFrame by a specific column?
11. Which method is used to rename columns in pandas?
12. What is the difference between
loc and iloc in pandas? 13. How do you handle duplicate rows in pandas?
14. What function converts a column to datetime format?
15. How do you apply a custom function to a DataFrame?
16. What is the use of the
apply() method in pandas? 17. How can you concatenate two DataFrames?
18. What does the
pivot_table() function do? 19. How do you calculate summary statistics in pandas?
20. Which method is used to export a DataFrame to a CSV file?
#️⃣ #pandas #dataanalysis #python #dataframe #coding #programming #datascience
By: t.iss.one/DataScienceQ 🚀
Telegram
Python Data Science Jobs & Interviews
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
Admin: @Hussein_Sheikho
#How can I implement the K-Nearest Neighbors (KNN) algorithm for classification using scikit-learn? Provide a Python example, explain how distance metrics affect predictions, and discuss the impact of choosing different values of k.
Answer:
KNN is a non-parametric algorithm that classifies data points based on the majority class among their k nearest neighbors in feature space.
Explanation:
- Distance Metrics: Common choices include Euclidean, Manhattan, and Minkowski. Euclidean is default and suitable for continuous variables.
- Choice of k:
- Small k (e.g., 1 or 3): Sensitive to noise, may overfit.
- Large k: Smoother decision boundaries, but may underfit.
- Optimal k is found via cross-validation.
- Standardization: Crucial because KNN uses distance; unscaled features can dominate results.
Time Complexity: O(nm) per prediction, where n is training samples and m is features.
Space Complexity: O(nm) to store training data.
Use Case: KNN is simple, effective for small-to-medium datasets, and works well when patterns are localized.
#MachineLearning #KNN #Classification #ScikitLearn #DataScience #PythonProgramming #AlgorithmExplained #DimensionalityReduction #SupervisedLearning
By: @DataScienceQ 🚀
Answer:
KNN is a non-parametric algorithm that classifies data points based on the majority class among their k nearest neighbors in feature space.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
import seaborn as sns
# Load dataset
data = datasets.load_iris()
X = data.data
y = data.target
feature_names = data.feature_names
target_names = data.target_names
# Split and scale data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Train KNN model with k=5
knn = KNeighborsClassifier(n_neighbors=5, metric='euclidean')
knn.fit(X_train_scaled, y_train)
# Predict and evaluate
y_pred = knn.predict(X_test_scaled)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(6, 4))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=target_names, yticklabels=target_names)
plt.title('Confusion Matrix')
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.show()
# Visualize decision boundaries (for first two features only)
plt.figure(figsize=(8, 6))
X_plot = X[:, :2] # Use only first two features for visualization
X_plot_scaled = scaler.fit_transform(X_plot)
knn_visual = KNeighborsClassifier(n_neighbors=5)
knn_visual.fit(X_plot_scaled, y)
h = 0.02
x_min, x_max = X_plot_scaled[:, 0].min() - 1, X_plot_scaled[:, 0].max() + 1
y_min, y_max = X_plot_scaled[:, 1].min() - 1, X_plot_scaled[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = knn_visual.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.3, cmap=plt.cm.Paired)
for i, color in enumerate(['red', 'green', 'blue']):
idx = np.where(y == i)
plt.scatter(X_plot_scaled[idx, 0], X_plot_scaled[idx, 1], c=color, label=target_names[i], edgecolors='k')
plt.xlabel(feature_names[0])
plt.ylabel(feature_names[1])
plt.title('KNN Decision Boundaries (First Two Features)')
plt.legend()
plt.show()
Explanation:
- Distance Metrics: Common choices include Euclidean, Manhattan, and Minkowski. Euclidean is default and suitable for continuous variables.
- Choice of k:
- Small k (e.g., 1 or 3): Sensitive to noise, may overfit.
- Large k: Smoother decision boundaries, but may underfit.
- Optimal k is found via cross-validation.
- Standardization: Crucial because KNN uses distance; unscaled features can dominate results.
Time Complexity: O(nm) per prediction, where n is training samples and m is features.
Space Complexity: O(nm) to store training data.
Use Case: KNN is simple, effective for small-to-medium datasets, and works well when patterns are localized.
#MachineLearning #KNN #Classification #ScikitLearn #DataScience #PythonProgramming #AlgorithmExplained #DimensionalityReduction #SupervisedLearning
By: @DataScienceQ 🚀
#How can I use scikit-learn to build a machine learning pipeline for classification? Provide a Python example, explain the steps involved in preprocessing, model training, and evaluation, and demonstrate how to use cross-validation.
Answer:
Scikit-learn is a powerful Python library for machine learning that provides simple and efficient tools for data mining and data analysis. It supports various algorithms, preprocessing techniques, and evaluation metrics.
Explanation:
- Pipeline: Combines preprocessing (StandardScaler) and model (SVC) into one unit for clean workflow and avoiding data leakage.
- StandardScaler: Normalizes features to have zero mean and unit variance.
- SVC: Support Vector Classifier for classification; RBF kernel handles non-linear data.
- Cross-validation: Evaluates model performance on multiple folds to reduce overfitting.
- GridSearchCV: Automates hyperparameter tuning by testing combinations of parameters.
Key Features of scikit-learn:
- Consistent API across models and utilities.
- Built-in support for preprocessing, feature selection, model evaluation, and ensemble methods.
- Extensive documentation and community support.
Use Case: Ideal for beginners and professionals alike to quickly prototype, evaluate, and optimize machine learning models.
#MachineLearning #ScikitLearn #Python #DataScience #MLPipeline #Classification #CrossValidation #HyperparameterTuning #SVM #GridSearchCV #DataPreprocessing
By: @DataScienceQ 🚀
Answer:
Scikit-learn is a powerful Python library for machine learning that provides simple and efficient tools for data mining and data analysis. It supports various algorithms, preprocessing techniques, and evaluation metrics.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns
# Load dataset
data = datasets.load_iris()
X = data.data
y = data.target
feature_names = data.feature_names
target_names = data.target_names
# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Create a pipeline with preprocessing and model
pipeline = Pipeline([
('scaler', StandardScaler()),
('classifier', SVC(kernel='rbf', random_state=42))
])
# Train the model
pipeline.fit(X_train, y_train)
# Make predictions
y_pred = pipeline.predict(X_test)
# Evaluate the model
accuracy = pipeline.score(X_test, y_test)
print(f"Accuracy: {accuracy:.2f}")
# Classification report
print("Classification Report:")
print(classification_report(y_test, y_pred, target_names=target_names))
# Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(6, 4))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=target_names, yticklabels=target_names)
plt.title('Confusion Matrix')
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.show()
# Cross-validation
cv_scores = cross_val_score(pipeline, X_train, y_train, cv=5)
print(f"Cross-validation scores: {cv_scores}")
print(f"Mean CV Score: {cv_scores.mean():.2f} ± {cv_scores.std():.2f}")
# Hyperparameter tuning using GridSearchCV
param_grid = {
'classifier__C': [0.1, 1, 10],
'classifier__gamma': ['scale', 'auto', 0.1, 1]
}
grid_search = GridSearchCV(pipeline, param_grid, cv=5, scoring='accuracy')
grid_search.fit(X_train, y_train)
print("Best parameters:", grid_search.best_params_)
print("Best cross-validation score:", grid_search.best_score_)
# Final model with best parameters
best_model = grid_search.best_estimator_
final_predictions = best_model.predict(X_test)
final_accuracy = accuracy_score(y_test, final_predictions)
print(f"Final Accuracy with tuned model: {final_accuracy:.2f}")
Explanation:
- Pipeline: Combines preprocessing (StandardScaler) and model (SVC) into one unit for clean workflow and avoiding data leakage.
- StandardScaler: Normalizes features to have zero mean and unit variance.
- SVC: Support Vector Classifier for classification; RBF kernel handles non-linear data.
- Cross-validation: Evaluates model performance on multiple folds to reduce overfitting.
- GridSearchCV: Automates hyperparameter tuning by testing combinations of parameters.
Key Features of scikit-learn:
- Consistent API across models and utilities.
- Built-in support for preprocessing, feature selection, model evaluation, and ensemble methods.
- Extensive documentation and community support.
Use Case: Ideal for beginners and professionals alike to quickly prototype, evaluate, and optimize machine learning models.
#MachineLearning #ScikitLearn #Python #DataScience #MLPipeline #Classification #CrossValidation #HyperparameterTuning #SVM #GridSearchCV #DataPreprocessing
By: @DataScienceQ 🚀
Python Data Science Jobs & Interviews
#How can I use SciPy for scientific computing tasks such as numerical integration, optimization, and signal processing? Provide a Python example that demonstrates solving a differential equation, optimizing a function, and filtering a noisy signal. Answer:…
Lesson: Mastering PyTorch – A Roadmap to Mastery
PyTorch is a powerful open-source machine learning framework developed by Facebook’s AI Research lab, widely used for deep learning research and production. To master PyTorch, follow this structured roadmap:
1. Understand Machine Learning Basics
- Learn key concepts: supervised/unsupervised learning, loss functions, gradients, optimization.
- Familiarize yourself with neural networks and backpropagation.
2. Master Python and NumPy
- Be proficient in Python and its scientific computing libraries.
- Understand tensor operations using NumPy.
3. Install and Set Up PyTorch
- Install PyTorch via official website:
- Ensure GPU support if needed (CUDA).
4. Learn Tensors and Autograd
- Work with tensors as the core data structure.
- Understand automatic differentiation using
5. Build Simple Neural Networks
- Create models using
- Implement forward and backward passes manually.
6. Work with Data Loaders and Datasets
- Use
- Apply transformations and preprocessing.
7. Train Models Efficiently
- Implement training loops with optimizers (
- Track loss and metrics during training.
8. Explore Advanced Architectures
- Build CNNs, RNNs, Transformers, and GANs.
- Use pre-trained models from
9. Use GPUs and Distributed Training
- Move tensors and models to GPU using
- Learn multi-GPU training with
10. Deploy and Optimize Models
- Export models using
- Optimize inference speed with quantization and pruning.
Roadmap Summary:
Start with fundamentals → Build basic models → Train and optimize → Scale to advanced architectures → Deploy professionally.
#PyTorch #DeepLearning #MachineLearning #AI #Python #NeuralNetworks #TensorFlowAlternative #DLFramework #AIResearch #DataScience #LearnToCode #MLDeveloper #ArtificialIntelligence
By: @DataScienceQ 🚀
PyTorch is a powerful open-source machine learning framework developed by Facebook’s AI Research lab, widely used for deep learning research and production. To master PyTorch, follow this structured roadmap:
1. Understand Machine Learning Basics
- Learn key concepts: supervised/unsupervised learning, loss functions, gradients, optimization.
- Familiarize yourself with neural networks and backpropagation.
2. Master Python and NumPy
- Be proficient in Python and its scientific computing libraries.
- Understand tensor operations using NumPy.
3. Install and Set Up PyTorch
- Install PyTorch via official website:
pip install torch torchvision - Ensure GPU support if needed (CUDA).
4. Learn Tensors and Autograd
- Work with tensors as the core data structure.
- Understand automatic differentiation using
torch.autograd.5. Build Simple Neural Networks
- Create models using
torch.nn.Module. - Implement forward and backward passes manually.
6. Work with Data Loaders and Datasets
- Use
torch.utils.data.Dataset and DataLoader for efficient data handling. - Apply transformations and preprocessing.
7. Train Models Efficiently
- Implement training loops with optimizers (
SGD, Adam). - Track loss and metrics during training.
8. Explore Advanced Architectures
- Build CNNs, RNNs, Transformers, and GANs.
- Use pre-trained models from
torchvision.models.9. Use GPUs and Distributed Training
- Move tensors and models to GPU using
.to('cuda'). - Learn multi-GPU training with
torch.nn.DataParallel or DistributedDataParallel.10. Deploy and Optimize Models
- Export models using
torch.jit or ONNX. - Optimize inference speed with quantization and pruning.
Roadmap Summary:
Start with fundamentals → Build basic models → Train and optimize → Scale to advanced architectures → Deploy professionally.
#PyTorch #DeepLearning #MachineLearning #AI #Python #NeuralNetworks #TensorFlowAlternative #DLFramework #AIResearch #DataScience #LearnToCode #MLDeveloper #ArtificialIntelligence
By: @DataScienceQ 🚀
1. What is the output of the following code?
2. Which of the following functions creates an array with random values between 0 and 1?
A)
B)
C)
D)
3. Write a function that takes a 2D NumPy array and returns the sum of all elements in each row.
4. What will be printed by this code?
5. Explain the difference between
6. How do you efficiently reshape a 1D array of 100 elements into a 10x10 matrix?
7. What is the result of
8. Write a program to generate a 3D array of shape (2, 3, 4) filled with random integers between 0 and 9.
9. What happens when you use
10. Which method can be used to find the indices of non-zero elements in a NumPy array?
11. What is the output of this code?
12. Describe how broadcasting works in NumPy with an example.
13. Write a function that normalizes each column of a 2D NumPy array using z-score normalization.
14. What is the purpose of
15. What does
16. How would you perform element-wise multiplication of two arrays of different shapes using broadcasting?
17. Write a program to compute the dot product of two large 2D arrays without using loops.
18. What is the difference between
19. How can you efficiently remove duplicate rows from a 2D NumPy array?
20. Explain the use of
#NumPy #AdvancedPython #DataScience #ScientificComputing #PythonLibrary #NumericalComputing #ArrayProgramming #MachineLearning #PythonDeveloper #CodeQuiz #HighLevelNumPy
By: @DataScienceQ 🚀
import numpy as np
a = np.array([1, 2, 3])
b = a + 1
a[0] = 99
print(b[0])
2. Which of the following functions creates an array with random values between 0 and 1?
A)
np.random.randint() B)
np.random.randn() C)
np.random.rand() D)
np.random.choice()3. Write a function that takes a 2D NumPy array and returns the sum of all elements in each row.
4. What will be printed by this code?
import numpy as np
x = np.array([1, 2, 3])
y = x.view()
y[0] = 5
print(x)
5. Explain the difference between
np.copy() and np.view().6. How do you efficiently reshape a 1D array of 100 elements into a 10x10 matrix?
7. What is the result of
np.dot(np.array([1, 2]), np.array([[1], [2]]))?8. Write a program to generate a 3D array of shape (2, 3, 4) filled with random integers between 0 and 9.
9. What happens when you use
np.concatenate() on arrays with incompatible shapes?10. Which method can be used to find the indices of non-zero elements in a NumPy array?
11. What is the output of this code?
import numpy as np
arr = np.arange(10)
result = arr[arr % 2 == 0]
print(result)
12. Describe how broadcasting works in NumPy with an example.
13. Write a function that normalizes each column of a 2D NumPy array using z-score normalization.
14. What is the purpose of
np.fromfunction() and how would you use it to create a 3x3 array where each element is the sum of its indices?15. What does
np.isclose(a, b) return and when is it preferred over ==?16. How would you perform element-wise multiplication of two arrays of different shapes using broadcasting?
17. Write a program to compute the dot product of two large 2D arrays without using loops.
18. What is the difference between
np.array() and np.asarray()?19. How can you efficiently remove duplicate rows from a 2D NumPy array?
20. Explain the use of
np.einsum() and provide an example for computing the trace of a matrix.#NumPy #AdvancedPython #DataScience #ScientificComputing #PythonLibrary #NumericalComputing #ArrayProgramming #MachineLearning #PythonDeveloper #CodeQuiz #HighLevelNumPy
By: @DataScienceQ 🚀
1. What is the output of the following code?
2. Which of the following functions is used to create an array with values spaced at regular intervals?
A)
B)
C)
D) All of the above
3. Write a function that takes a 1D NumPy array and returns a new array where each element is squared, but only if it’s greater than 5.
4. What will be printed by this code?
5. Explain the difference between
6. How would you efficiently compute the outer product of two vectors using NumPy?
7. What is the result of
8. Write a program to generate a 5x5 matrix filled with random integers from 1 to 100, then find the maximum value in each row.
9. What happens when you use
10. Which method can be used to flatten a multi-dimensional array into a 1D array without copying data?
11. What is the output of this code?
12. Describe how
13. Write a function that calculates the Euclidean distance between all pairs of points in a 2D array of coordinates.
14. What is the purpose of
15. How do you perform matrix multiplication using
16. Write a program to filter out all elements in a 2D array that are outside the range [10, 90].
17. What does
18. How can you efficiently transpose a large 3D array of shape (100, 100, 100) using
19. Explain the concept of "views" vs "copies" in NumPy and give an example where a view leads to unexpected behavior.
20. Write a function that computes the covariance matrix of a dataset represented as a 2D NumPy array.
#NumPy #AdvancedPython #DataScience #InterviewPrep #PythonLibrary #ScientificComputing #MachineLearning #CodingChallenge #HighLevelNumPy #PythonDeveloper #TechnicalInterview #DataAnalysis
By: @DataScienceQ 🚀
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = a.T
b[0, 0] = 99
print(a)
2. Which of the following functions is used to create an array with values spaced at regular intervals?
A)
np.linspace() B)
np.arange() C)
np.logspace() D) All of the above
3. Write a function that takes a 1D NumPy array and returns a new array where each element is squared, but only if it’s greater than 5.
4. What will be printed by this code?
import numpy as np
x = np.array([1, 2, 3])
y = x.copy()
y[0] = 5
print(x[0])
5. Explain the difference between
np.meshgrid() and np.mgrid in generating coordinate matrices.6. How would you efficiently compute the outer product of two vectors using NumPy?
7. What is the result of
np.sum(np.eye(3), axis=1)?8. Write a program to generate a 5x5 matrix filled with random integers from 1 to 100, then find the maximum value in each row.
9. What happens when you use
np.resize() on an array with shape (3,) to resize it to (5,)?10. Which method can be used to flatten a multi-dimensional array into a 1D array without copying data?
11. What is the output of this code?
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
result = arr[[0, 1], [1, 2]]
print(result)
12. Describe how
np.take() works and provide an example using a 2D array.13. Write a function that calculates the Euclidean distance between all pairs of points in a 2D array of coordinates.
14. What is the purpose of
np.frombuffer() and when might it be useful?15. How do you perform matrix multiplication using
np.matmul() and @ operator? Are they always equivalent?16. Write a program to filter out all elements in a 2D array that are outside the range [10, 90].
17. What does
np.nan_to_num() do and why is it important in numerical computations?18. How can you efficiently transpose a large 3D array of shape (100, 100, 100) using
np.transpose() or swapaxes()?19. Explain the concept of "views" vs "copies" in NumPy and give an example where a view leads to unexpected behavior.
20. Write a function that computes the covariance matrix of a dataset represented as a 2D NumPy array.
#NumPy #AdvancedPython #DataScience #InterviewPrep #PythonLibrary #ScientificComputing #MachineLearning #CodingChallenge #HighLevelNumPy #PythonDeveloper #TechnicalInterview #DataAnalysis
By: @DataScienceQ 🚀
In Python, NumPy is the cornerstone of scientific computing, offering high-performance multidimensional arrays and tools for working with them—critical for data science interviews and real-world applications! 📊
By: @DataScienceQ 🚀
#Python #NumPy #DataScience #CodingInterview #MachineLearning #ScientificComputing #DataAnalysis #Programming #TechJobs #DeveloperTips
import numpy as np
# Array Creation - The foundation of NumPy
arr = np.array([1, 2, 3])
zeros = np.zeros((2, 3)) # 2x3 matrix of zeros
ones = np.ones((2, 2), dtype=int) # Integer matrix
arange = np.arange(0, 10, 2) # [0 2 4 6 8]
linspace = np.linspace(0, 1, 5) # [0. 0.25 0.5 0.75 1. ]
print(linspace)
# Array Attributes - Master your data's structure
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix.shape) # Output: (2, 3)
print(matrix.ndim) # Output: 2
print(matrix.dtype) # Output: int64
print(matrix.size) # Output: 6
# Indexing & Slicing - Precision data access
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(data[1, 2]) # Output: 6 (row 1, col 2)
print(data[0:2, 1:3]) # Output: [[2 3], [5 6]]
print(data[:, -1]) # Output: [3 6 9] (last column)
# Reshaping Arrays - Transform dimensions effortlessly
flat = np.arange(6)
reshaped = flat.reshape(2, 3)
raveled = reshaped.ravel()
print(reshaped)
# Output: [[0 1 2], [3 4 5]]
print(raveled) # Output: [0 1 2 3 4 5]
# Stacking Arrays - Combine datasets vertically/horizontally
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.vstack((a, b))) # Vertical stack
# Output: [[1 2 3], [4 5 6]]
print(np.hstack((a, b))) # Horizontal stack
# Output: [1 2 3 4 5 6]
# Mathematical Operations - Vectorized calculations
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
print(x + y) # Output: [5 7 9]
print(x * 2) # Output: [2 4 6]
print(np.dot(x, y)) # Output: 32 (1*4 + 2*5 + 3*6)
# Broadcasting Magic - Operate on mismatched shapes
matrix = np.array([[1, 2, 3], [4, 5, 6]])
scalar = 10
print(matrix + scalar)
# Output: [[11 12 13], [14 15 16]]
# Aggregation Functions - Statistical power in one line
values = np.array([1, 5, 3, 9, 7])
print(np.sum(values)) # Output: 25
print(np.mean(values)) # Output: 5.0
print(np.max(values)) # Output: 9
print(np.std(values)) # Output: 2.8284271247461903
# Boolean Masking - Filter data like a pro
temperatures = np.array([18, 25, 12, 30, 22])
hot_days = temperatures > 24
print(temperatures[hot_days]) # Output: [25 30]
# Random Number Generation - Simulate real-world data
print(np.random.rand(2, 2)) # Uniform distribution
print(np.random.randn(3)) # Normal distribution
print(np.random.randint(0, 10, (2, 3))) # Random integers
# Linear Algebra Essentials - Solve equations like a physicist
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
x = np.linalg.solve(A, b)
print(x) # Output: [2. 3.] (Solution to 3x+y=9 and x+2y=8)
# Matrix inverse and determinant
print(np.linalg.inv(A)) # Output: [[ 0.4 -0.2], [-0.2 0.6]]
print(np.linalg.det(A)) # Output: 5.0
# File Operations - Save/load your computational work
data = np.array([[1, 2], [3, 4]])
np.save('array.npy', data)
loaded = np.load('array.npy')
print(np.array_equal(data, loaded)) # Output: True
# Interview Power Move: Vectorization vs Loops
# 10x faster than native Python loops!
def square_sum(n):
arr = np.arange(n)
return np.sum(arr ** 2)
print(square_sum(5)) # Output: 30 (0²+1²+2²+3²+4²)
# Pro Tip: Memory-efficient data processing
# Process 1GB array without loading entire dataset
large_array = np.memmap('large_data.bin', dtype='float32', mode='r', shape=(1000000, 100))
print(large_array[0:5, 0:3]) # Process small slice
By: @DataScienceQ 🚀
#Python #NumPy #DataScience #CodingInterview #MachineLearning #ScientificComputing #DataAnalysis #Programming #TechJobs #DeveloperTips
# 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
Pandas Python Tip: Custom Column Operations with
The
Key Takeaway:
#Pandas #Python #DataScience #DataManipulation #PythonTips
---
By: @DataScienceQ ✨
apply()! 🚀The
df.apply() method is powerful for applying a function along an axis of the DataFrame (rows or columns), especially useful for custom transformations on columns or rows.import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Score': [85, 92, 78]}
df = pd.DataFrame(data)
Example: Create a new column 'Grade' based on 'Score'
def assign_grade(score):
if score >= 90:
return 'A'
elif score >= 80:
return 'B'
else:
return 'C'
df['Grade'] = df['Score'].apply(assign_grade)
print(df)
You can also use lambda functions for simpler operations
df['Score_Double'] = df['Score'].apply(lambda x: x * 2)
print(df)
Key Takeaway:
df.apply() (especially on a Series) is excellent for element-wise custom logic, often more readable than complex vectorized operations for specific tasks.#Pandas #Python #DataScience #DataManipulation #PythonTips
---
By: @DataScienceQ ✨
❤1
🚀 NumPy Tip: Boolean Indexing (Masking) 🚀
Ever need to filter your arrays based on a condition? NumPy's Boolean Indexing, also known as masking, is your go-to! It allows you to select elements that satisfy a specific condition.
Explanation:
A boolean array (the mask) is created by applying a condition to your original array. When this mask is used for indexing, NumPy returns a new array containing only the elements where the mask was
#NumPy #PythonTips #DataScience #ArrayMasking #Python #Programming
---
By: @DataScienceQ ✨
Ever need to filter your arrays based on a condition? NumPy's Boolean Indexing, also known as masking, is your go-to! It allows you to select elements that satisfy a specific condition.
import numpy as np
Create a sample NumPy array
data = np.array([12, 5, 20, 8, 35, 15, 30])
Create a boolean mask: True where value is > 10, False otherwise
mask = data > 10
print("Boolean Mask:", mask)
Apply the mask to the array to filter elements
filtered_data = data[mask]
print("Filtered Data (values > 10):", filtered_data)
You can also combine the condition and indexing directly
even_numbers = data[data % 2 == 0]
print("Even Numbers:", even_numbers)
Explanation:
A boolean array (the mask) is created by applying a condition to your original array. When this mask is used for indexing, NumPy returns a new array containing only the elements where the mask was
True. Simple, powerful, and efficient!#NumPy #PythonTips #DataScience #ArrayMasking #Python #Programming
---
By: @DataScienceQ ✨