Whenever i see back and forth zigzag lines, it tells to me sort the x-axis values.
Or, you can set marker to "" empty string and increase markersize which turns the default line plot into a scatter plot

Could you share your code behind this plot?
What does “didn’t work” mean?
Also is my interpretation of “weird” being the zigzag lines correct or something else?

def knn_train_test(df, train_col, target_col): #shuffling the data np.random.seed(1) shuffled_index = np.random.permutation(df.index) shuffled_df = df.reindex(shuffled_index) #shuffled_df = shuffled_df.drop([0], axis=0)

`#spliting the dataset into train and test set`
`split_value = int(len(shuffled_df)/2)`
`train_set = shuffled_df.iloc[0:split_value]`
`test_set = shuffled_df.iloc[split_value:]`
`k_values = [1, 3, 5, 7, 9]`
`k_rmses = {}`
`#training and predicting`
`for k in k_values:`
` knn = KNeighborsRegressor(n_neighbors=k)`
` knn.fit(train_set[[train_col]], train_set[target_col])`
` predictions = knn.predict(test_set[[train_col]])`
`mse = mean_squared_error(test_set[target_col], predictions)`
`rmse = np.sqrt(mse)`
`k_rmses[k] = rmse`
`return k_rmses`

# training all the numeric columns in normalized_cars using knn_train_test function target_cols = normalized_cars.columns.drop('price') k_rmses_target = {} for cols in target_cols: rmse_vals = knn_train_test(normalized_cars, cols, 'price') k_rmses_target[cols]=rmse_vals

import matplotlib.pyplot as plt %matplotlib inline

for k,v in sorted(k_rmses_target.items()): x = list(v.keys()) y = list(v.values())

This chunk tells me that x-axis values are a list, since you did list() over the keys.
How do you sort a list then?

I made a mistake with what i said about marker. What i mean’t was change the linestyle so it’s not visible anymore. Then there won’t be zigzag. (if you don’t want to sort and still show lines)

I don’t have many of the variables in the pasted code above.

Are you able to produce a minimal reproducible example to replicate the zigzag here?
(Like directly defining the x and y values right before plotting, with the plotting code) so I can try to fix that and return similar but edited code.

I see x: [1 3 9 5 7].
Do you think something is wrong with this?
Can you guess how does this relate to the zigzag on the graph?
How can you verify the effect of x on how the graph looks? (Set up a small example with your own x, y and plot it, change the x and see results.).