【发布时间】:2019-11-11 13:42:52
【问题描述】:
我正在尝试为我的小型数据科学项目计算线性回归。
我有课
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.xself.x) - np.sum(self.x) * np.sum(self.x)) TypeError: *: 'int' 和 'dict_values' 的不支持的操作数类型
我做错了什么?
编辑:
当将 list(dictionary.values()) 传递给我得到的类时
Traceback(最近一次调用最后一次): 文件“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 线型='-') 文件“C:\Users\Paweł\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib__init__.py”,第 1810 行,在内部 返回函数(ax,*args,**kwargs) 文件“C:\Users\Paweł\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes_axes.py”,第 1612 行,在图中 self.add_line(line) 文件“C:\Users\Paweł\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes_base.py”,第 1895 行,在 add_line self._update_line_limits(line) 文件“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() _to_unmasked_float_array 中的文件“C:\Users\Paweł\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\cbook__init__.py”,第 1365 行 返回 np.asarray(x, 浮点数) 文件“C:\Users\Paweł\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\core\numeric.py”,第 538 行,在 asarray 中 返回数组(a,dtype,copy=False,order=order) TypeError: float() 参数必须是字符串或数字,而不是“函数”
编辑 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
【问题讨论】:
-
你正试图将一个整数乘以一个字典,你传递给类的值是什么类型的?
-
dictionary.values()
-
尝试传递
list(dictionary.values()) -
你传递的是 list(dictionary.values()) 还是 dictionary.values() ,你应该传递第一个
标签: python numpy linear-regression