I am an undergraduate who is expecting a mediocre grade in my optimization class this semester (roughly B-), which is in significant contrast to my near straight A transcript. Unfortunately, I want to do a PhD in machine learning and so I expect this grade to be viewed in a negative light by graduate admissions committees due to the relevance of the material and the aforementioned contrast. But I will take an advanced graduate level machine learning course this coming semester which has the optimization course as a prerequisite. Will a good performance in this graduate level class negate my mediocre grade in my optimization class? In general, can (and will) good grades in more advanced classes negate bad grades in prerequisite classes in the context of graduate admissions? To what extent? I understand that each admissions committee will view this situation differently. I am looking for a general opinion.
In general, can (and will) good grades in more advanced classes negate bad grades in prerequisite classes in the context of graduate admissions?
Only to a very limited extent. Getting a C (or F) in calculus during Freshman year is likely to be completely ignored if you got straight As after that. Other than in such extreme examples, no, I would not expect to be able to negate past grades. But of course, each grade is only one data point, and additional data points can significantly improve your overall record.
roughly B-, which is in significant contrast to my near straight A transcript.
So, this is an outlier. Like in any other sort of data analysis, a single outlier in an otherwise homogeneous record is unlikely to have much/any effect. Sure, getting a good grade in the more advanced course could make it even more clear that this outlier was not related to the subject. But it seems like you are already on track to get put in the highest "tier" with respect to GPA, so you may want to consider investing time on the other facets of your application.
Actually, grades don't negate other grades. Your record is what it is. But people are reasonable about evaluating your history in most cases. People evaluate your application based on whether it shows indicators that you are very likely to be a success, not whether or not you are perfect in every way. Very few people can claim perfection.
A single bad grade is, in any case, very unlikely to have much if any negative effect. In any grad school application you have to make your own case that you will be successful. There are many ways to do that. GPA is only one and individual course grades matter even less, assuming that your record indicates positive outcomes.
Make sure you get good letters of recommendation, of course.
Your chances can still be good. The key thing to realize is that this happened, and to work on optimizing your other skills and accomplishments, rather than trying to fix or revisit previous events. PhD admittance is based on many characteristics, including overcoming adversity, taking initiative, and learning from mistakes.