计算参数时出错(不支持的操作数类型)

时间:2019-06-30 16:51:45

标签: python numpy linear-regression

我正在尝试为我的小型数据科学项目计算线性回归。

我上课

import numpy as np

# I'm using the idea from https://devarea.com/linear-regression-with-numpy/#.XRfdcegzaUk
class LinearRegression:
    def __init__(self, values):
        self.y = np.array(values)
        self.x = np.array([number for number in range(1, len(values)+1)])
        self.values_to_return = []

    def getlinear(self, x1):
        # Function that returns value
        def inner(x1):
            return self.m * x1 + self.b

        self.m = (len(self.x) * np.sum(self.x*self.y) - np.sum(self.x) * np.sum(self.y)) / (len(self.x)*np.sum(self.x*self.x) - np.sum(self.x) * np.sum(self.x))
        self.b = (np.sum(self.y) - self.m*np.sum(self.x)) / len(self.x)

        return inner

我遇到了错误

  

文件“ c:/ Users /Paweł/ Documents / projects vscode / WorldBankDataKeras / tests.py”,第35行,在       country1 = data.CountryInformations('波兰')      init 中的文件“ c:\ Users \Paweł\ Documents \ projects vscode \ WorldBankDataKeras \ data.py”,第26行       linear.return_values_of_linear_regression())     文件“ c:\ Users \Paweł\ Documents \ projects vscode \ WorldBankDataKeras \ linear_regr.py”,第22行,在return_values_of_linear_regression中       self.values_to_return.append(self.getlinear(x_param))     文件“ c:\ Users \Paweł\ Documents \ projects vscode \ WorldBankDataKeras \ linear_regr.py”,第15行,位于getlinear中       self.m =(np.array(len(self.x))* np.sum(self.x * self.y)-np.sum(self.x)* np.sum(self.y))/( np.array(len(self.x)) np.sum(self.x self.x)-np.sum(self.x)* np.sum(self.x))   TypeError:*:“ int”和“ dict_values”不支持的操作数类型

我该怎么办?

编辑:

将list(dictionary.values())传递到我得到的类中时

  

回溯(最近通话最近):     文件“ c:/ Users /Paweł/ Documents / projects vscode / WorldBankDataKeras / tests.py”,第41行,在       graph.plot_graph_renewable_electricity_status()     文件“ c:\ Users \Paweł\ Documents \ projects vscode \ WorldBankDataKeras \ graph_plotting.py”,第115行,位于plot_graph_renewable_electricity_status中       linestyle ='-')     内部文件1810行中的文件“ C:\ Users \Paweł\ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ matplotlib__init __。py”       return func(ax,* args,** kwargs)     图中的文件“ C:\ Users \Paweł\ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ matplotlib \ axes_axes.py”,行1612       self.add_line(行)     文件“ C:\ Users \Paweł\ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ matplotlib \ axes_base.py”,行1895,在add_line中       self._update_line_limits(行)     文件“ C:\ Users \Paweł\ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ matplotlib \ axes_base.py”,行1917,在_update_line_limits中       路径= line.get_path()     文件“ C:\ Users \Paweł\ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ matplotlib \ lines.py”,第945行,位于get_path中       self.recache()     重新缓存文件“ C:\ Users \Paweł\ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ matplotlib \ lines.py”,行645       y = _to_unmasked_float_array(yconv).ravel()     第1365行的文件“ C:\ Users \Paweł\ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ matplotlib \ cbook__init __。py”在_to_unmasked_float_array中       返回np.asarray(x,float)     文件“ C:\ Users \Paweł\ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ numpy \ core \ numeric.py”,第538行,按格式排列       返回数组(a,dtype,copy = False,order = order)   TypeError:float()参数必须是字符串或数字,而不是'function'

编辑2:

class CountryInformations:

    def __init__(self, name):
        self.name = name

        xls_parsing = xls_parse.XLSParsing(self.name)

        self.population = xls_parsing.import_country_population()
        self.co2_emissions = xls_parsing.import_country_co2_emissions()
        self.renewable_electricity_status = xls_parsing.import_country_renewable_electricity_status()

        # I want to have linear regression values in format [year] : value
        self.population_linear_regression = self.population.copy()
        self.co2_emissions_linear_regression = self.co2_emissions.copy()
        self.renewable_electricity_status_linear_regression = self.renewable_electricity_status.copy()

        linear = linear_regr.LinearRegression(list(self.population_linear_regression.values()))
        # Replacing values in dict by values from linear regression
        self.population_linear_regression = dict.fromkeys(self.population_linear_regression,
                                                          linear.return_values_of_linear_regression())

        linear = linear_regr.LinearRegression(list(self.co2_emissions_linear_regression.values()))
        # Replacing values in dict by values from linear regression
        self.co2_emissions_linear_regression = dict.fromkeys(self.co2_emissions_linear_regression,
                                                             linear.return_values_of_linear_regression())

        linear = linear_regr.LinearRegression(list(self.renewable_electricity_status_linear_regression.values()))
        # Replacing values in dict by values from linear regression
        self.renewable_electricity_status_linear_regression = dict.fromkeys(self.renewable_electricity_status_linear_regression, 
                                                                            linear.return_values_of_linear_regression())

    def __str__(self):
        print_string = 'Country: {} \n \
                        Population: {}M \n \
                        CO2 Emissions: {}KT \n \
                        Renewable Electricity Status: {}%'.format(self.name, \
                                                                  self.population, \
                                                                  self.co2_emissions, \
                                                                  self.renewable_electricity_status)
        return print_string

1 个答案:

答案 0 :(得分:0)

我通过删除内部函数解决了这个问题。我不知道为什么它会起作用

def getlinear(self, x1):

    self.m = (len(self.x) * np.sum(self.x*self.y) - np.sum(self.x) * np.sum(self.y)) / (len(self.x)*np.sum(self.x*self.x) - np.sum(self.x) * np.sum(self.x))
    self.b = (np.sum(self.y) - self.m*np.sum(self.x)) / len(self.x)

    return self.m * x1 + self.b

感谢大家的帮助