Home > front end >  Python: TypeError: can only concatenate list (not "int") to list; Problems with datatypes
Python: TypeError: can only concatenate list (not "int") to list; Problems with datatypes

Time:01-17

I know there are a few similar questions in here but none of them helped me.. at least I didn´t find any..

I load Data from my MongoDB and store it in a List of up to 200 integers.. 1 dimensional. In the Example i use just 10.

I already tried to np.array(X) it but that doesn't work either.

I'm just learning things, pls be gentle.

import numpy as np

X =  [538, 561, 500, 559, 545, 559, 579, 549, 542, 524]

# Y     I load the data from a textfile with numpy.loadtext
# Y =  [33. 16. 32. 51. 27. 16. 34. 17. 29. 15.]



Z, Y = np.loadtxt("data/textfile.txt", skiprows=1, unpack=True)


def predict(X):
    return X * 1   2

def loss(X, Y):
    return np.average((predict(X - Y)**2)


def train(X, Y, iterations):
    for i in range (iterations):
        current_loss = loss(X, Y)

test, test2 = train(X, Y, iterations=1000)

Textfile

data1  data2
13            33
2             16
14            32
23            51
13            27
1             16
18            34
10            17
26            29
3             15
Traceback (most recent call last):
  File "C:\..., line 18, in <module>
    w, b = linear_regression.train(X, Y, iterations=1000000, lr=0.001)
  File "C:\...\linear_regression.py", line 16, in train
    current_loss = loss(X, Y, w, b)
  File "C:\...linear_regression.py", line 10, in loss
    return np.average((predict(X, w, b) - Y) ** 2)
  File "C:\...linear_regression.py", line 6, in predict
    return X * w   b
TypeError: can only concatenate list (not "int") to list
X =  [538, 561, 500, 559, 545, 559, 579, 549, 542, 524]
Y =  [33. 16. 32. 51. 27. 16. 34. 17. 29. 15.]

These are the prints of my X & Y ..

How can i convert X in the format of Y?

CodePudding user response:

I had a problem with the array. I solved it myself. I feel dumb now.

I just had to convert the X into a numpy array.

import numpy as np

X =  [538, 561, 500, 559, 545, 559, 579, 549, 542, 524]

# Y     I load the data from a textfile with numpy.loadtext
# Y =  [33. 16. 32. 51. 27. 16. 34. 17. 29. 15.]



Z, Y = np.loadtxt("data/textfile.txt", skiprows=1, unpack=True)


def predict(X):
    return X * 1   2

def loss(X, Y):
    return np.average((predict(X - Y)**2)


def train(X, Y, iterations):
    for i in range (iterations):
        current_loss = loss(X, Y)
X = np.asarray(X)



test, test2 = train(X, Y, iterations=1000)



  •  Tags:  
  • Related