跳槽成功 准备面试的时候记下来的note全面总结关于一些how to 我个人感觉非常详细了 重点在于不能光知道what 还要知道why 更重要的是 you gotta know what you are talking about -- 不要只记住一个表面的词 而是问自己 为什么要这么做 我如果是面试官 我会揪着你说的一些big words 去挖掘你到底知不知道你在说什么 能回答上followup的才是真的懂了希望有帮助 求大米!也attach了word 文档
- How do you handle imbalanced data?
- Up weight minority class
- Oversampling minority class: To be equally distributed—>This can balance the class distribution but does not provide any additional information to the model — artificially reducing variation in the data
- Under-sampling majority class
- SMOTE(**Synthetic Minority Oversampling Technique**) for oversampling imbalanced classification
- *SMOTE first selects a minority class instance at random and finds its k nearest minority class neighbours. The synthetic instance is then created by choosing one of the k nearest neighbours b at random and connecting a and b to form a line segment in the feature space. The synthetic instances are generated as a convex combination of the two chosen instances a and b.*. Χ
- *The combination of SMOTE and under-sampling performs better than plain under-sampling.*
- The approach is effective because new synthetic examples from the minority class are created that are plausible, that is, are relatively close in feature space to existing examples from the minority class.
- A general downside of the approach is that synthetic examples are created without considering the majority class, possibly resulting in ambiguous examples if there is a strong overlap between the classes.
- Evaluate the model using the [ROC area under curve (AUC) metric](https://machinelearningmastery.c ... fication-in-python/). This can be optimistic for severely imbalanced datasets but will still show a relative change with better performing models。. 1point3acres.com
- Loss Function — focal loss
How to select metrics?
- **Are you predicting probabilities?**. 1point3acres
- **Do you need class labels?**. From 1point 3acres bbs
- **Is a positive class more important?**
- Use Precision-Recall AUC
- **Are both classes important?**
- Use ROC AUC. Χ
- **Do you need probabilities?**. 1point 3 acres
- Use Brier Score and Brier Skill Score—> calibrated probability
- **Are you predicting class labels?**. 1point3acres
- **Is a positive class more important?**
- **Are False Negatives and False Positives Equally Important?**
- Use F1-Measure
- **Are False Negatives More Important?**
- Use F2-Measure ..
- **Are False Positives More Important?**
- Use F0.5-Measure
- **Are both classes important?**
- **Do you have < 80%-90% Examples for the Majority Class?**
- Use Accuracy
- **Do you have > 80%-90% Examples for the Majority Class?**
- Use G-Mean. ----
- Should I scale my data— is my data on the same scale . - tree-based model: no effects
- distanced- involved e.g. KNN - huge impacts
- OLS: no impacts on prediction results but on interpretability —> different unit of measure
- How do you handle overfit?
- Improving the data, such as reducing the number of features fed into the model with feature selection, or collecting more data to have more instances than features.
- Alternatively, we can combat overfitting by improving our model. We could make our model simpler by reducing the number of estimators (in a random forest or XGBoost), or reducing the number of parameters in a neural network. We could also introduce a technique known as ***early stopping***, where the training process is stopped early instead of running for a set number of epochs.
- Another way to simplify the model is by adding bias into the model with regularization.
- add ‘information’ to the objective function(something we try to minimize to lower the error)—> L1 or L2. 1point 3 acres
- How do you assess coefficient uncertainty?
Standard error . From 1point 3acres bbs
CI. 1point3acres
The standard error is most useful as a means of calculating a confidence interval. For a large sample, a 95% confidence interval is obtained as the values **1.96×SE on either side of the mean**
.
- How to transform data?
- Normalize: data distribution is gaussian
- Log transform: conforms to a **[power law](https://wikipedia.org/wiki/Power_law)** distribution that clumps data at the low end, log transform creates a smoother distribution
- **Quantiles:** As a rule of thumb, to create n quantiles, you should have at least 10n
examples. If you don't have enough data, stick to normalization.
- How to make the model robust to outliers?
- investigate outliers first
- add regularization to reduce variance
- try diff model: **tree-based**
- cap data or log transform
- change error metric, e.g to MAE instead MSE
- remove outliers only if they are true anomalies
- How to handle categorical features?
Before taking action, clean the data a little bit —> check on **frequency**: if not evenly distributed but has a large gap, e.g only labeled categories with more than 10 entries and put the rest into an "other" group, which increased the metric.
.--If evenly distributed —> PCA
- How do you deal with a categorical variable with high cardinality?. 1point 3 acres
*A categorical feature is said to possess high cardinality when there are too many of these unique values.* One-Hot Encoding becomes a big problem in such a case since we have a separate column for each unique value (indicating its presence or absence) in the categorical variable. This leads to two problems, one is obviously space consumption, but this is not as big a problem as the second problem, the **[curse of dimensionality.](https://www.quora.com/What-is-the-curse-of-dimensionality)**
. 1point3acres
> **As the number of features grows, the amount of data we need to accurately be able to distinguish between these features (in order to give us a prediction) and generalize our model (learned function) grows EXPONENTIALLY**
>
- **One-hot encoders**: if no order inherent, cons: may cause the curse of dimensionality, doesn’t, doesn’t provide any additional info, treat as equal,
- better to drop one of var to avoid collinearity + one-hot and PCA
- **Label encoder**: better for ordinal var —> good for tree-based
- **Target(Mean) Encoding**: each value in the column is replaced with the **mean target value for that category**. This allows for a more direct representation of the relationship between the categorical variable and the target variable.
- Pros: outliers, exposed to the same values, the model may overfit, be careful of target leakage
- How to solve it? - introduce some Gaussian noise in the target statistics.
- - **Leave-one-out encoding:** calculating the average, **excluding** the current row value. This levels off the effect of outliers and create more diverse encoded values.
- ****Entity Embeddings:**** Formally, an embedding is a mapping of a categorical variable into an n-dimensional vector. We map categorical variables in a function approximation problem into Euclidean spaces, which are the entity embeddings of the categorical variables. The mapping is learned by a neural network during the standard supervised training process.-baidu 1point3acres
- Entity embedding not only reduces memory usage and speeds up neural networks but more importantly by **mapping similar values close to each othe**r in the embedding space it **reveals the intrinsic properties** of the categorical variables.
- Entity embedding automatically learns the representation of categorical features in **multi-dimensional spaces** which puts values with **similar** effect to the target output value close to each other helping neural networks to solve the problem
- **CatBoost**: a target-based categorical encoder. It is a supervised encoder that encodes categorical columns according to the target value. It supports binomial and continuous targets. It involves an ordering principle in order to overcome this problem of target leakage. It uses a principle similar to the time series data validation. The values of target statistic rely on the observed history, i.e, **target probability for the current feature is calculated only from the rows (observations) before it.**
- **With plenty of features available, how should we select the most important features?**
Ask how big the sample size is, why a model with features is not preferred, what algorithms/models will be used, what is the goal of the modelling, and understand the context before answering.
- If we have a small sample size, and we have P>>N, we can’t do a classic regression model because (XTX) is not full rank, not invertible, meaning that we can’t solve model coefficients
- There are a few methods to do feature selections. Forward selection, iteratively adding new features that significantly improve model fit. Backward elimination, iteratively dropping existing features that insignificantly decreases model fit. Here model fit metrics can be AIC, BIC, etc.
- If we have a large sample size, but want to reduce overfitting. We can consider regularization methods such as LASSO, which will force coefficients to be zero for those insignificant features.
- We can also use PCA to reduce feature dimensionality.
- Another thing to consider is multicollinearity. Do some features have high covariance? With the presence of multicollinearity, the estimated standard error of coefficient will be larger, leading to a higher False negative error (higher type II error), or equivalently, lower power.
- Do we care about feature interpretability (point estimate and confidence interval)? If we just care about prediction, multicollinearity is fine to be ignored since point estimates are not affected. But the confidence interval of point estimates will be wider and thus interpretation of coefficients may be invalid.
- We can use Ridge regression to shrink the variance of estimated coefficients, at a tradeoff of introducing some bias. Ridge/LASSO regression increases the power of estimates.. check 1point3acres for more.
- Random forest algorithms are more robust to multicollinearity.
- How does XGB assign the label?
- Regression: take mean or median
- Classification: depends on the objective function, sigmoid function, output probability 0 - 1
- How to split tree in RF and decision tree?
**Split** **the process of dividing a node into multiple sub-nodes to create relatively pure nodes.**
Based on the type of target variable:
- **Continuous**, regression problem —> reduce variance —> calculating the homogeneity of a node
Here are the steps to split a decision tree using a reduction in variance:
. 1point3acres.com
1. For each split, individually calculate the variance of each child node
2. Calculate the variance of each split as the weighted average variance of child nodes.
3. Select the split with the lowest variance. 1point3acres
4. Perform steps 1-3 until completely homogeneous nodes are achieved
- **Categorical**
- Information Gain = 1- entropy—> higher IG —> higher purity
- entropy
Steps to split a decision tree using Information Gain:
1. For each split, individually calculate the entropy of each child node
2. Calculate the entropy of each split as the weighted average entropy of child nodes
3. Select the split with the lowest entropy or highest information gain
4. Until you achieve homogeneous nodes, repeat steps 1-3
- Gini Impurity
- Gini: Gini is the probability of correctly labelling a randomly chosen element if it was randomly labelled according to the distribution of labels in the node.
- Preferable than Information Gain as not involved Log computation
- How to select k in k-means?
1. **The Elbow Method**
1. Calculate within-cluster sum of squared error for different k: squared error is by computing the distance(manhattan or Euclidean
. From 1point 3acres bbs Construct Elbow curve using number of K and error, choose one the error decrease sharply. Also, need to align with business understanding
2. **The Silhouette Method:** *The silhouette value measures how similar a point is to its own cluster (cohesion) compared to other clusters (separation).*
1. Plot k v.s Sihouette value —> find out the peak point. 1point 3 acres