2

I'm trying to calculate linear regression for my small data science project.

I have class

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 

And I got error

File "c:/Users/Paweł/Documents/projects vscode/WorldBankDataKeras/tests.py", line 35, in country1 = data.CountryInformations('Poland') File "c:\Users\Paweł\Documents\projects vscode\WorldBankDataKeras\data.py", line 26, in init linear.return_values_of_linear_regression()) File "c:\Users\Paweł\Documents\projects vscode\WorldBankDataKeras\linear_regr.py", line 22, in return_values_of_linear_regression self.values_to_return.append(self.getlinear(x_param)) File "c:\Users\Paweł\Documents\projects vscode\WorldBankDataKeras\linear_regr.py", line 15, in 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: unsupported operand type(s) for *: 'int' and 'dict_values'

What do I do wrong?

EDIT:

When passing list(dictionary.values()) into the class I got

Traceback (most recent call last): File "c:/Users/Paweł/Documents/projects vscode/WorldBankDataKeras/tests.py", line 41, in graph.plot_graph_renewable_electricity_status() File "c:\Users\Paweł\Documents\projects vscode\WorldBankDataKeras\graph_plotting.py", line 115, in plot_graph_renewable_electricity_status linestyle='-') File "C:\Users\Paweł\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib__init__.py", line 1810, in inner return func(ax, *args, **kwargs) File "C:\Users\Paweł\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes_axes.py", line 1612, in plot self.add_line(line) File "C:\Users\Paweł\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes_base.py", line 1895, in add_line self._update_line_limits(line) File "C:\Users\Paweł\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes_base.py", line 1917, in _update_line_limits path = line.get_path() File "C:\Users\Paweł\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\lines.py", line 945, in get_path self.recache() File "C:\Users\Paweł\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\lines.py", line 645, in recache y = _to_unmasked_float_array(yconv).ravel() File "C:\Users\Paweł\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\cbook__init__.py", line 1365, in _to_unmasked_float_array return np.asarray(x, float) File "C:\Users\Paweł\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\core\numeric.py", line 538, in asarray return array(a, dtype, copy=False, order=order) TypeError: float() argument must be a string or a number, not 'function'

EDIT 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 
14
  • you are trying to multiply an integer by a dictionary , what is the type of values that are you passing to the class? Commented Jun 30, 2019 at 16:56
  • dictionary.values() Commented Jun 30, 2019 at 16:57
  • try passing list(dictionary.values()) Commented Jun 30, 2019 at 17:00
  • are you passing list(dictionary.values()) or dictionary.values() , you should pass the first one Commented Jun 30, 2019 at 17:01
  • 1
    It may be helpful: I used this website devarea.com/linear-regression-with-numpy/#.XRjkEegzaUl Commented Jun 30, 2019 at 17:16

1 Answer 1

0

I solved this by deleting inner function. I don't know why it works by it does so

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 

Thanks for helping everyone

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.