# Error message "int() must be a string"

**URL:** <https://discuss.python.org/t/error-message-int-must-be-a-string/19603>\
**Category:** Python Help\
**Created:** [September 30, 2022, 7:57pm UTC](https://discuss.python.org/t/error-message-int-must-be-a-string/19603 "2022-09-30T19:57:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chiara](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@Chiara](https://discuss.python.org/u/Chiara)\
**Post date:** [September 30, 2022, 7:57pm UTC](https://discuss.python.org/t/error-message-int-must-be-a-string/19603/1 "2022-09-30T19:57:21Z")

</div>

Hi, I receive an error message for the code below.  
It says TypeError: int() argument must be a string, a bytes-like object or a number, not ‘NoneType’

I believe that the error could lie in the line  
´´´  
(xx, yy, prediction\_grid) = make\_prediction\_grid(predictors, outcomes, limits, h, k)  
´´´  
or is related to make\_prediction\_grid.

Could anyone help me out by any chance? That’d be great!

```auto
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as ss

def knn_predict(p, points, outcomes, k=5):
    ind = find_nearest_neighbors(p, points, k)
    majority_vote(outcomes)
    return majority_vote(outcomes[ind])

def find_nearest_neighbors(p, points, k=5):
    """Find the k nearest neighbors of p and return their indices."""
    distances = np.zeros(points.shape[0])   
    for i in range(len(distances)):
        distances[i] = distance(p, points[i])
    ind = np.argsort(distances)   
    return ind[0:k]
def majority_vote(votes):
    """Return the most common element in votes."""
    
    vote_counts={}
    for vote in votes:
        if vote in vote_counts:
            vote_counts[vote] += 1
        else:
            vote_counts[vote]=1
   
    winners=[]
    max_count=max(vote_counts.values())

    for vote, count in vote_counts.items():
        if count == max_count:
            winners.append(vote)

def distance(p1, p2): 
    """Find the distance between points p1 and p2."""
    return np.sqrt(np.sum(np.power(p2-p1, 2)))

def generate_synth_data(n=50):
    """Create two sets of points from bivariate normal distributions."""
    points = np.concatenate((ss.norm(0,1).rvs((n,2)), ss.norm(1,1).rvs((n,2))), axis=0)
    outcomes = np.concatenate((np.repeat(0, n), np.repeat(1, n)), axis=0) 
    return (points, outcomes)
h=1

def make_prediction_grid(predictors, outcomes,limits, h, k):
    """Classify each point on the prediction grid."""
    (x_min, x_max, y_min, y_max) = limits
    xs = np.arange(x_min, x_max, h)
    ys = np.arange(y_min, y_max, h)
    xx, yy = np.meshgrid(xs, ys)
    
    prediction_grid = np.zeros(xx.shape, dtype = int)
    for i,x in enumerate(xs):
        for j, y in enumerate(ys):
            p = np.array([x, y])
            prediction_grid[j, i] = knn_predict(p, predictors, outcomes, k)
    return (xx, yy, prediction_grid)           

         

def plot_prediction_grid (xx, yy, prediction_grid, filename):
    """ Plot KNN predictions for every point on the grid."""
    from matplotlib.colors import ListedColormap
    background_colormap = ListedColormap (["hotpink","lightskyblue", "yellowgreen"])
    observation_colormap = ListedColormap (["red","blue","green"])
    plt.figure(figsize =(10,10))
    plt.pcolormesh(xx, yy, prediction_grid, cmap = background_colormap, alpha = 0.5)
    plt.scatter(predictors[:,0], predictors [:,1], c = outcomes, cmap = observation_colormap, s = 50)
    plt.xlabel('Variable 1'); plt.ylabel('Variable 2')
    plt.xticks(()); plt.yticks(())
    plt.xlim (np.min(xx), np.max(xx))
    plt.ylim (np.min(yy), np.max(yy))
    plt.savefig(filename)

        
(predictors, outcomes) = generate_synth_data()
k = 5; filename = "knn_synth_5.pdf"; limits = (-3, 4, -3, 4); h = 0.1
(xx, yy, prediction_grid) = make_prediction_grid(predictors, outcomes, limits, h, k)
plot_prediction_grid(xx, yy, prediction_grid, filename)  

```

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [September 30, 2022, 8:14pm UTC](https://discuss.python.org/t/error-message-int-must-be-a-string/19603/2 "2022-09-30T20:14:06Z")

</div>

Where does the traceback say the exception is being raised?

---

<div class="post-metadata">

**Author:** ![Chiara](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@Chiara](https://discuss.python.org/u/Chiara)\
**Post date:** [September 30, 2022, 8:15pm UTC](https://discuss.python.org/t/error-message-int-must-be-a-string/19603/3 "2022-09-30T20:15:54Z")

</div>

at line 85, in   
(xx, yy, prediction\_grid) = make\_prediction\_grid(predictors, outcomes, limits, h, k)

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [September 30, 2022, 8:21pm UTC](https://discuss.python.org/t/error-message-int-must-be-a-string/19603/4 "2022-09-30T20:21:13Z")

</div>

That line doesn’t contain `int(...)`, so the exception isn’t being raised on that line, but somewhere in `make_prediction_grid` or one of the functions/methods it’s calling.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [October 1, 2022, 1:09am UTC](https://discuss.python.org/t/error-message-int-must-be-a-string/19603/5 "2022-10-01T01:09:59Z")

</div>

The Python interpreter goes to a lot of trouble to give us extensive information we can use for debugging. Please copy and paste the full traceback, starting with the line “Traceback …” all the way to the end, so we can see that information and aren’t working blind.

Aside from the numpy array of zeroes using `dtype=int`, I don’t see that you make a call to int anywhere in your code. So there are three obvious possibilities:

- the error message you are reporting is inaccurate;

- the code you are showing us is inaccurate;

- or maybe you are trying to put None inside a numpy array of type int.

Looking more closely, it looks like `majority_vote` fails to return the majority vote, instead it returns None when execution reaches the end of the function.

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [October 1, 2022, 1:57am UTC](https://discuss.python.org/t/error-message-int-must-be-a-string/19603/6 "2022-10-01T01:57:52Z")

</div>

That’s true. The docstring says it returns the most common element, but it doesn’t.

---

<div class="post-metadata">

**Author:** ![Chiara](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@Chiara](https://discuss.python.org/u/Chiara)\
**Post date:** [October 1, 2022, 7:35am UTC](https://discuss.python.org/t/error-message-int-must-be-a-string/19603/7 "2022-10-01T07:35:36Z")

</div>

Hey,  
I copied the code again:

```auto
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as ss

def knn_predict(p, points, outcomes, k=5):
    ind = find_nearest_neighbors(p, points, k)
    majority_vote(outcomes)
    return majority_vote(outcomes[ind])

def find_nearest_neighbors(p, points, k=5):
    """Find the k nearest neighbors of p and return their indices."""
    distances = np.zeros(points.shape[0])   
    for i in range(len(distances)):
        distances[i] = distance(p, points[i])
    ind = np.argsort(distances)   
    return ind[0:k]
def majority_vote(votes):
    """Return the most common element in votes."""
    
    vote_counts={}
    for vote in votes:
        if vote in vote_counts:
            vote_counts[vote] += 1
        else:
            vote_counts[vote]=1
   
    winners=[]
    max_count=max(vote_counts.values())

    for vote, count in vote_counts.items():
        if count == max_count:
            winners.append(vote)

def distance(p1, p2): 
    """Find the distance between points p1 and p2."""
    return np.sqrt(np.sum(np.power(p2-p1, 2)))

def generate_synth_data(n=50):
    """Create two sets of points from bivariate normal distributions."""
    points = np.concatenate((ss.norm(0,1).rvs((n,2)), ss.norm(1,1).rvs((n,2))), axis=0)
    outcomes = np.concatenate((np.repeat(0, n), np.repeat(1, n)), axis=0) 
    return (points, outcomes)
h=1

def make_prediction_grid(predictors, outcomes,limits, h, k):
    """Classify each point on the prediction grid."""
    (x_min, x_max, y_min, y_max) = limits
    xs = np.arange(x_min, x_max, h)
    ys = np.arange(y_min, y_max, h)
    xx, yy = np.meshgrid(xs, ys)
    
    prediction_grid = np.zeros(xx.shape, dtype = int)
    for i,x in enumerate(xs):
        for j, y in enumerate(ys):
            p = np.array([x, y])
            prediction_grid[j, i] = knn_predict(p, predictors, outcomes, k)
    return (xx, yy, prediction_grid)           

         

def plot_prediction_grid (xx, yy, prediction_grid, filename):
    """ Plot KNN predictions for every point on the grid."""
    from matplotlib.colors import ListedColormap
    background_colormap = ListedColormap (["hotpink","lightskyblue", "yellowgreen"])
    observation_colormap = ListedColormap (["red","blue","green"])
    plt.figure(figsize =(10,10))
    plt.pcolormesh(xx, yy, prediction_grid, cmap = background_colormap, alpha = 0.5)
    plt.scatter(predictors[:,0], predictors [:,1], c = outcomes, cmap = observation_colormap, s = 50)
    plt.xlabel('Variable 1'); plt.ylabel('Variable 2')
    plt.xticks(()); plt.yticks(())
    plt.xlim (np.min(xx), np.max(xx))
    plt.ylim (np.min(yy), np.max(yy))
    plt.savefig(filename)

        
(predictors, outcomes) = generate_synth_data()
k = 5; filename = "knn_synth_5.pdf"; limits = (-3, 4, -3, 4); h = 0.1
(xx, yy, prediction_grid) = make_prediction_grid(predictors, outcomes, limits, h, k)
plot_prediction_grid(xx, yy, prediction_grid, filename)  

```

And the traceback is:

Traceback (most recent call last):

File “C:\Users\Weronika Vogel\Desktop\Weiterbildung\Week 3\Gutenberg\3.3 zusammen.py”, line 85, in   
(xx, yy, prediction\_grid) = make\_prediction\_grid(predictors, outcomes, limits, h, k)

File “C:\Users\Weronika Vogel\Desktop\Weiterbildung\Week 3\Gutenberg\3.3 zusammen.py”, line 63, in make\_prediction\_grid  
prediction\_grid[j, i] = knn\_predict(p, predictors, outcomes, k)

TypeError: int() argument must be a string, a bytes-like object or a number, not ‘NoneType’

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [October 1, 2022, 11:10am UTC](https://discuss.python.org/t/error-message-int-must-be-a-string/19603/8 "2022-10-01T11:10:45Z")

</div>

It looks like `majority_vote` fails to return the majority vote, instead it returns None when execution reaches the end of the function.
