简体   繁体   English

类型错误:尝试循环 Python 中的 function 时,不支持 -: 'str' 和 'float' 的操作数类型

[英]TypeError: unsupported operand type(s) for -: 'str' and 'float' when trying to loop over a function in Python

I'm trying to make a Kernel Estimator (as given in the The Article ) predict values given a vector, rather than a single value.我正在尝试使 Kernel 估计器(如文章中给出)预测给定向量的值,而不是单个值。 Here is the code:这是代码:

from scipy.stats import norm
import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
import math

class GKR:
    def __init__(self, x, y, b):
        self.x = x
        self.y = y
        self.b = b

    '''Implement the Gaussian Kernel'''
    def gaussian_kernel(self, z):
        return (1/math.sqrt(2*math.pi))*math.exp(-0.5*z**2)

    '''Calculate weights and return prediction'''
    def predict(self, X):
        kernels = [self.gaussian_kernel((xi-X)/self.b) for xi in self.x]
        weights = [len(self.x) * (kernel/np.sum(kernels)) for kernel in kernels]
        return np.dot(weights, self.y)/len(self.x)


llke = GKR(x, y, 0.2)

y_hat=pd.DataFrame(index=range(x.shape[0]))
for row in range(x.shape[0]):
    y_hat.iloc[row] = llke.predict(float(x.iloc[row]))
y_hat

but I get the error: TypeError: unsupported operand type(s) for -: 'str' and 'float' .但我收到错误: TypeError: unsupported operand type(s) for -: 'str' and 'float' From what I understand the issue is in the self.gaussian_kernel((xi-X) that it sees xi as a string for some reason. I tried to wrap it in float() function, but it doesn't help. What could be the problem? Alternatively, feel free to suggest alternative ways to apply the function predict onto a vector, rather than a single value.据我了解,问题出在self.gaussian_kernel((xi-X)中,它出于某种原因将xi视为字符串。我试图将它包装在 float() function 中,但它没有帮助。可能是什么问题吗?或者,请随意建议将 function 预测应用于向量而不是单个值的替代方法。

The issue was that x and y were given in the wrong format (as lists of lists, rather than lists), which is why the interpreter could deal with it properly.问题是xy以错误的格式给出(作为列表的列表,而不是列表),这就是解释器可以正确处理它的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError: 不支持的操作数类型 -: 'float' 和 'str' - TypeError: unsupported operand type(s) for -: 'float' and 'str' TypeError:+不支持的操作数类型:“ float”和“ str” - TypeError: unsupported operand type(s) for +: 'float' and 'str' TypeError:/:不支持的操作数类型:“ str”和“ float” - TypeError: unsupported operand type(s) for /: 'str' and 'float' Python 错误:类型错误:+ 不支持的操作数类型:'float' 和 'str' - Python error : TypeError: unsupported operand type(s) for +: 'float' and 'str' TypeError:-:'str'和'float'python pandas的不受支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'str' and 'float' python pandas 初学者 python,TypeError:+ 不支持的操作数类型:“float”和“str” - Beginner python, TypeError: unsupported operand type(s) for +: 'float' and 'str' Python 3:类型错误:+ 不支持的操作数类型:'float' 和 'str' - Python 3: TypeError: unsupported operand type(s) for +: 'float' and 'str' 类型错误:+ 不支持的操作数类型:'int' 和 'str' 尝试“打印”时 - TypeError: unsupported operand type(s) for +: 'int' and 'str' when trying to 'print' Python 3:不支持的操作数类型 -:'str' 和 'float' - Python 3: unsupported operand type(s) for -: 'str' and 'float' Python 不支持 + 的操作数类型:'float' 和 'str - Python unsupported operand type(s) for +: 'float' and 'str
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM