Here is a brief visualization that I am working on today..
US Average House Prices and Steel & Lumber Price Index 1982-2022
Here is a brief visualization that I am working on today..
US Average House Prices and Steel & Lumber Price Index 1982-2022
get_dummies is a useful function for running regressions with categorical variables, but sometimes you may want to keep the variable it drops. I looked at its documentation, but couldn't get how I can select the feature to drop. Anyways, here is a 'manual' way of doing it instead of get_dummies. This seems more customizable to me, at least for some cases.
Sometimes we may need to capture the lowest correlations of other columns with our Y column (or maybe another column) to see if there is noise and room for improving our model.
This is how I did it lately:
And this sorts them by absolute value:
Getting a proper list of correlations between columns may be needed, especially when you can't generate a heatmap due to a high number of features. You may need this list to see highly correlated features so that you can drop some columns to improve your regression results and avoid possible multicollinearity problems.
This page was very useful for me to get rid of the duplicate rows.
Here is the code and the screenshot to see correlations above 0.7 (of course you can adjust this threshold):
I wanted to see each unique value with the number of occurrences (or counts of each unique value) in each of the columns of my dataframe, as shown in the picture below. However, I couldn't manage it by groupby or pivot_table functions. I guess there is an easier method -a one line code- that I couldn't devise or find on google (I mean there should be), but I came up with this at the end:
cat_cols=data.select_dtypes(include=['object']).columns for column in cat_cols: print("For column:",column) print(data[column].unique()) print('-'*50)
If we are not using advanced or simple imputation techniques to fill the null values, looking at other columns may also help to conjecture the missing value.
So which column should we look at? Let's take this dataset which has more than 80 features. Let's assume that we have some null values in 'LotArea'. Which other features can we look at to make a conjecture on this feature? We can have a look at the feature with the highest correlation values with LotArea with this code that also sorts the values:
print(pd.DataFrame(df.corr())['LotArea'].sort_values(ascending=False))
So looking at houses with similar 'LotFrontage's, may help the most. Other features do not have high correlation, but we may still add second or third feature into account.
Imputing missing values in a specific cell can be tricky. I had to do this while working on a competition on Kaggle.
I had to impute values for a specific cell which also includes a categorical variable to be imputed (see the last one in the picture: 'GarageFinish').
The cells that should be imputed were about the garage features of the house. Here's what I did in brief:
I thought the neighborhood, home type and house style features could be good predictors to estimate the garage type. So I predicted the garage features by filtering these values and imputing either their mean or median or mode for different features.
As I said above, the interesting part was the 'GarageFinish' feature which is a categorical one. The way I found here was imputing the mean by combining value_counts and index[0] methods.
You can understand it better by checking the code below: