# Is it better to replace missing values by mean or mean by class

I am preparing a dataset for a binary classification. Some of the columns contain missing values.

I am thinking of either handling the missing values manually using pandas and replace each missing value by the mean of the column by class or using the Imputer method by scikit.

Two questions, is it better to replace the missing values with the mean of the class rather than the mean of the whole column and if yes why isn't there such an option in scikit's Imputer method?

some data related info

For some features (sentiment analysis features) there a lot of missing values and that comes from the fact that some users (my training samples are pairs of users) don't share common hashtags (it's twitter a analysis task), but most of the features are topological so it doesn't make sense to discard all those lines.

On the other hand someone could say that since the users don't share any hashtags they should have a zero value for those features. But this isn't necessarily true. My crawling includes the latest 500 tweets for those users, not all tweets they have ever produced.

Also from visualizing the data I can see clear correlations between those features and the class values, from the values that are not gathered around zero due to lack of common hashtags.

Here are some visualizations

Here are two sentiment features, not exactly easily linearly separable but the problem can be observed (not exactly seen because of density), the stripe on the crossing of the axes holds close to half the values

Here is another one, the problem is the same, the small stripe on the crossing of the axes holds close to half the values

Here is the same visualization but from weka this time

• I'd like some clarification after your edit. You have samples for two users u1 and u2 where some features are hashtag co-occurrence counts? – achompas Aug 25 '16 at 2:53
• let's say there are two types of features, topological and sentiment. The topological features are based on structural properties of the pair of users. Adamic adar for example or shortest path. The sentiment features use correlations based on the common hashtags (e.g. size of the rarest common hashtag) – LetsPlayYahtzee Aug 25 '16 at 2:57
• In your second edit, what does from the values that are not gathered around zero due to lack of common hashtags mean? Are you observing that missing hashtag values actually seem to correlate with class labels? If so, that's a vote in favor of my first suggestion, and you should consider adding a binary feature indicating whether hashtag co-occurrence data for the two users is actually missing. – achompas Aug 25 '16 at 3:01
• That may be a good suggestion, but that is not what I mean. When visualizing those features you can see some really dense lines around zero (which are all the missing values) but the non-missing values, some times, create nice linearly separable planes which in my mind means that those features have indeed some predictive power. – LetsPlayYahtzee Aug 25 '16 at 3:04
• One problem of imputation by class mean is that when you want to use your model in the future, you won't know the class labels, but you might still have to impute the same feature. – mac Oct 24 '16 at 14:13