I created a SARIMAX wrapper to use and run my data with, but I got an error. I think I wrote the wrapper correctly, but I get an error. Can I get some help on this?
class SARIMAX_wrapper(object):
def __init__(self, data: pd.DataFrame, p_val: int, d_val: int, q_val: int, P_val: int, D_val: int, Q_val: int, s_val: int) -> None:
self.p_val = p_val
self.q_val = q_val
self.d_val = d_val
self.P_val = P_val
self.Q_val = Q_val
self.D_val = D_val
self.s_val = s_val
self.data = data
#self.model_ = self.fit_model_()
def fit_model_(self) -> None:
try:
temp_model = SARIMAX(self.data, order=(self.p_val, self.d_val, self.q_val), seasonal_order=(self.P_val, self.D_val, self.Q_val, self.s_val))
temp_fitted_model__ = temp_model.fit()
#return temp_fitted_model__
self.model_ = temp_fitted_model__
except:
print('Something is not right!')
pass
#return temp_fitted_model_
def forecast_(self) -> list:
forecast_vals = self.model_.forecast(steps=12)
return forecast_vals
if __name__ == '__main__':
temp1 = SARIMAX_wrapper(data, 2, 2, 2, 0, 0, 0, 0)
temp2 = temp1.fit_model_()
results = temp2.forecast_()
It fails on the results line. Is there a better way to write this wrapper? Thanks!
Related
I have a class __init__ that accepts variable length arguments. I am trying to figure out how to separate the *args into str, and floats/ints.
So, for example, in my mind that might look like:
class Example():
def __init__(self, *legs, *vals, named_input: float, named_input_2: str):
*legs are a string. *vals are floats & ints.
My goal is that the user can do:
a = Example('1y', '5y', named_input = 100, named_input_2 = 'setting_1')
a.legs = ['1y', '5y']
a.vals = []
a = Example('1y', '5y', 15, named_input = 100, named_input_2 = 'setting_1')
a.legs = ['1y', '5y']
a.vals = [15]
a = Example('1y', 0, 15, 30, named_input = 100, named_input_2 = 'setting_1')
a.legs = ['1y']
a.vals = [ 0, 15, 30,]
At least 1 *leg must always be supplied. vals can be None though.
Overcomplicated it.. just had to do this:
class test():
def __init__(self, *args):
self.legs = []
self.vals = []
for x in args:
if type(x) in [str]:
self.legs.append(x)
elif type(x) in [float, int]:
self.vals.append(x)
I'm working on project and got a problem. When I tried to get the element of dictionary I got key error. Although when I doesn't use variable numer the program works. Even when variable = 2 The program doesn't work. Code:
from typing import Sequence
class Lot:
def __init__(self, numer_lotu, id_samolotu,
czas_lotu, trasa, bramka,
cenaek, cenabiz, cenapr,
nrwolek = None, nrwolbiz= None, nrwolpr = None):
self.numer_lotu = numer_lotu
self.id_samolotu = id_samolotu
self.czas_lotu = czas_lotu
self.trasa = trasa
self.bramka = bramka
self.cenaek = cenaek
self.cenabiz = cenabiz
self.cenapr = cenapr
self.nrwolek = nrwolek
self.nrwolbiz = nrwolbiz
self.nrwolpr = nrwolpr
def get_numer_lotu(self):
return self.numer_lotu
def get_id_samolotu(self):
return self.id_samolotu
def get_czas_lotu(self):
return self.czas_lotu
def get_trasa(self):
return self.trasa
def get_bramka(self):
return self.bramka
def get_cenaek(self):
return self.cenaek
def get_cenabiz(self):
return self.cenabiz
def get_cenapr(self):
return self.cenapr
def get_wolnemiejscaek(self):
return self.nrwolek
def get_wolnemiejscabiz(self):
return self.nrwolbiz
def get_wolnemiejscapr(self):
return self.nrwolpr
class DatabaseofLoty():
def __init__(self, list_of_samolot : Sequence[Lot] = ()):
self.list_of_samoloty = {lot.get_numer_lotu() : lot
for lot in list_of_samolot}
print(self.list_of_samoloty)
self.list_of_samolot = list_of_samolot
def get_list(self):
return self.list_of_samolot
def get_dictionary(self):
return self.list_of_samoloty
def get_loty_by_numer_lotu(self, numer):
print(self.list_of_samoloty[2])
print(numer)
return self.list_of_samoloty[numer]
When I tried to run this I got this as an output:
{1: <lociki.Lot object at 0x7f4443f9ec70>}
{1: <lociki.Lot object at 0x7f4443f9ec70>, 2: <lociki.Lot object at 0x7f4443f9edc0>}
{1: <lociki.Lot object at 0x7f4443fa2c10>}
{1: <lociki.Lot object at 0x7f4443fa2c10>, 2: <lociki.Lot object at 0x7f4443f42670>}
<lociki.Lot object at 0x7f4443f42670>
2
KeyError: '2'
2 (int), which is your key is not the same as '2' (str), which is what numer contains
I've noticed that the code prints the date twice to the constructor and am having trouble understanding why since I believe I only instantiate the object once within my code.
This is the constructor
def __init__(self):
self.today = date.today()
print(self.today)
Here is where I instantiate it
self.data = database()
self.schedule_today = self.data.get_bulletin()
Full code for this section of the program with some unfinished functions
class database:
sch_today = ['apt: 8:00', "breakfast", "music", 'apt: 9:00', "therapy", 'apt: 12:00', "lunch"]
test_schedule = []
def __init__(self):
self.today = date.today()
print(self.today)
def get_parse_bulletin_list(self):
temp = []
index = 0
for i in self.sch_today:
if i[0:3] == 'apt':
while index%3 != 0:
temp.append('')
index+=1
temp.append(i)
else:
temp.append(i)
index += 1
return temp
def get_bulletin(self):
n_count = 1
temp = []
ref = self.get_parse_bulletin_list()
for i in ref:
if i[0:3] == 'apt':
temp.append(paper_scrap().get_layout())
n_count = 1
elif not i:
temp.append(Notecard().blank_layout())
#elif i[0:5] == '[hlf]':
#temp.append(Notecard())
elif n_count >= 3: #allign left
temp.append(Notecard())
else:
temp.append(Notecard())
n_count += 1
return temp
def update_schedule(self):
with open('calendar.txt') as r:
pass
class BulletinInterface(RelativeLayout):
def __init__(self, **kwargs):
super(BulletinInterface, self).__init__(**kwargs)
self.data = database()
self.schedule_today = self.data.get_bulletin()
self.l1 = BulletinArea(size_hint=(1,1),
padding=(38, 135, 37, 34),
orientation=('tb-lr'))
self.add_widget(self.l1)
self.b1 = Button(text="test",
background_color=(1, 1, 1, 1),
size_hint=(0.1, 0.1)
)
self.b1.bind(on_press=self.bulletin_init)
self.add_widget(self.b1)
# bulletin board initialize
self.bulletin_init()
def bulletin_init(self, touch=None):
self.init_bulletin(self.schedule_today)
def init_bulletin(self, element_list):
for i in element_list:
self.l1.add_widget(i)
Found the problem after reviewing the construction of the GUI. The KV language and regular python code were both instantiating the GUI, leading to duplicate function calls for everything.
I am implementing a custom ExtensionDtype and seeing that the order of the results becomes out of sync with the original index when using reindex operation or appending/concat on dataframes. I understand the result can be reordered but I expect the index/column names to be aligned with the data.
Here is my implementation.
#register_extension_dtype
class CategoryListWithHashFeatureDtype(ExtensionDtype):
name = 'CategoryListWithHashFeature'
type = CategoryListWithHashFeature
kind = 'O'
na_value = DEAL_NAN
#classmethod
def construct_array_type(cls):
return CategoryListWithHashFeatureArray
#classmethod
def construct_from_string(cls, string):
if string == cls.name:
return cls()
else:
raise TypeError(f'Cannot construct a {cls} from {string}')
class CategoryListWithHashFeatureArray(ExtensionArray):
def __init__(self, values: List[CategoryListWithHashFeature] = None):
self._dtype = CategoryListWithHashFeatureDtype()
self.raw_names: List[str] = []
self.raw_values: List[Union[List[str], List[bytes]]] = []
self.hash_names: List[str] = []
self.hash_values: List[List[int]] = []
if values:
for feature in values:
if isinstance(feature, CategoryListWithHashFeature):
self.raw_names.append(feature.name)
self.raw_values.append(feature.values)
self.hash_names.append(feature.hash_name)
self.hash_values.append(feature.hash_values)
else:
self.raw_names.append(NAN_FEATURE_NAME)
self.raw_values.append([])
self.hash_names.append(NAN_FEATURE_NAME)
self.hash_values.append([])
#classmethod
def _from_values(cls,
raw_names: List[str],
raw_values: List[Union[List[str], List[bytes]]],
hash_names: List[str],
hash_values: List[List[int]]) -> CategoryListWithHashFeatureArray:
inst = CategoryListWithHashFeatureArray()
inst.raw_names = raw_names
inst.raw_values = raw_values
inst.hash_names = hash_names
inst.hash_values = hash_values
return inst
#classmethod
def _from_sequence(cls, scalars, dtype=None, copy=False):
if isinstance(scalars, ndarray):
scalars = list(scalars)
return cls(scalars)
#classmethod
def _from_factorized(cls, values, original):
return cls(values)
def __getitem__(self, index):
try:
if isinstance(index, int) or isinstance(index, np.int64):
if len(self) == 0:
return CategoryListWithHashFeatureDtype.na_value
return CategoryListWithHashFeature(
raw_name=self.raw_names[index],
raw_values=self.raw_values[index],
hash_name=self.hash_names[index],
hash_values=self.hash_values[index]
)
elif isinstance(index, slice):
raw_names: List[str] = self.raw_names[index]
return CategoryListWithHashFeatureArray([
CategoryListWithHashFeature(
raw_name=self.raw_names[idx],
raw_values=self.raw_values[idx],
hash_name=self.hash_names[idx],
hash_values=self.hash_values[idx])
for idx in range(len(raw_names))
])
elif isinstance(index, ndarray):
return CategoryListWithHashFeatureArray([
CategoryListWithHashFeature(
raw_name=self.raw_names[idx],
raw_values=self.raw_values[idx],
hash_name=self.hash_names[idx],
hash_values=self.hash_values[idx])
for idx in list(index)
])
else:
raise RuntimeError(f'Unsupported index type {str(type(index))}')
except Exception:
logging.exception(f'index: {index}. Val: {self.raw_names}')
return self._dtype.na_value
def __len__(self):
return len(self.raw_names)
#property
def dtype(self):
return self._dtype
def __eq__(self, other) -> bool:
if not isinstance(other, CategoryListWithHashFeatureArray):
return False
if len(self) != len(other):
return False
return all(self.__getitem__(idx) == other.__getitem__(idx) for idx in range(len(self)))
def __hash__(self):
h = 0
for idx in range(len(self)):
h += self.__getitem__(idx).__hash__()
return h
#property
def nbytes(self):
return sys.getsizeof(self.raw_names) + sys.getsizeof(self.raw_values) \
+ sys.getsizeof(self.hash_names) + sys.getsizeof(self.hash_values)
def isna(self):
missing_values = [val == CategoryListWithHashFeatureDtype.na_value or len(val) == 0 for val in self.raw_values]
return np.array(missing_values)
def take(self, indices, allow_fill=False, fill_value=None):
# During reindex we get a -1 index if the item was not found in the original data
data = [self.__getitem__(idx) for idx in indices if idx >= 0]
if allow_fill and fill_value is None:
fill_value = self._dtype.na_value
result = pd.api.extensions.take(data, indices, fill_value=fill_value, allow_fill=allow_fill)
return self._from_sequence(result)
def copy(self, deep=False):
copy = [self._copy_feature(idx) for idx in range(len(self.raw_names))]
return type(self)(copy)
def _copy_feature(self, idx: int) -> CategoryListWithHashFeature:
return CategoryListWithHashFeature(
raw_name=self.raw_names[idx],
raw_values=self.raw_values[idx].copy(),
hash_name=self.hash_names[idx],
hash_values=self.hash_values[idx].copy()
)
#classmethod
def _concat_same_type(cls, to_concat):
raw_names: List[str] = []
raw_values: List[Union[List[str], List[bytes]]] = []
hash_names: List[str] = []
hash_values: List[List[int]] = []
for feature_array in to_concat: # type: CategoryListWithHashFeatureArray
raw_names.extend(feature_array.raw_names)
raw_values.extend(feature_array.raw_values)
hash_names.extend(feature_array.hash_names)
hash_values.extend(feature_array.hash_values)
return cls._from_values(raw_names=raw_names,
raw_values=raw_values,
hash_names=hash_names,
hash_values=hash_values)
Tests:
category_list_features = [
CategoryListWithHashFeature('size2', ['s', 'm'], 'size2_hash', [2]),
CategoryListWithHashFeature('color', ['R'], 'color_hash', [1])
]
s1 = Series({f.name: f for f in category_list_features}, dtype='CategoryListWithHashFeature')
s2 = s1.reindex(index=['color', 'size', 'size2'])
print(s2)
color size2: ['s', 'm'] / Hashed size2: [2]
size :NAN_F: [] / Hashed :NAN_F: []
size2 color: ['R'] / Hashed color_hash: [1]
dtype: CategoryListWithHashFeature
My initial guess was there was something incorrect about my handling of the negative indices in the take method so I added the negative index check. Index values I get in my take method: [1, -1, 0] ==> the -1 corresponds to the missing size index/value in the series. I know this incorrect because the take function can actually get legit negative index and i need to support it. If I remove the negative index check I get the color value duplicated:
color color: ['R'] / Hashed color_hash: [1]
size :NAN_F: [] / Hashed :NAN_F: []
size2 color: ['R'] / Hashed color_hash: [1]
dtype: CategoryListWithHashFeature
I did some debugging and this seems to happen in the pandas.algorithms.take_nd function:
Relevant snippet:
func = _get_take_nd_function(
arr.ndim, arr.dtype, out.dtype, axis=axis, mask_info=mask_info
)
func(arr, indexer, out, fill_value)
I am using pandas version 1.2.1
I would appreciate some help on this. Did I miss something?
Thanks!
This was probably a silly question. If I have my own take method, I don't need to call pd.api.extensions.take again. I can just return my calculated results.
I would still like to know the reason for the internal take returning the unexpected result.
'int' Object Not Callable Heap Sort Function
Im trying to create a function that return a sorted list but im getting the following error: "new_value = sort_heap.deleteMin() 'int' object is not callable"
This is the code:
class MinHeap:
def __init__(self):
self.heap=[]
def __str__(self):
return f'{self.heap}'
__repr__=__str__
def parent(self,index):
# -- YOUR CODE STARTS HERE
if index>len(self) or index<=1:
return None
else:
return self.heap[index//2-1]
def leftChild(self,index):
# -- YOUR CODE STARTS HERE
if index<1 or 2*index>len(self):
return None
else:
return self.heap[2*index-1]
def rightChild(self,index):
if index<1 or 2*index>len(self):
return None
else:
return self.heap[2*index-1]
def __len__(self):
return len(self.heap)
def insert(self,x):
self.heap.append(x)
current = len(self)
while self.parent(current) is not None and self.parent(current)>x:
self.heap[current-1], self.heap[current//2-1] = self.heap[current//2-1], self.heap[current-1]
current = current//2
#property
def deleteMin(self):
if len(self)==0:
return None
elif len(self)==1:
out=self.heap[0]
self.heap=[]
return out
deleted = self.heap[0]
current = 1
self.heap[0] = self.heap[len(self)-1]
x = self.heap.pop()
moved_value = self.heap[0]
while self.leftChild(current) is not None:
left=self.leftChild(current)
right=self.rightChild(current)
if right is not None:
if left<=right and left<moved_value:
self.heap[current-1], self.heap[current*2] = self.heap[current*2], self.heap[current-1]
current = current *2
elif left>right and right<moved_value:
self.heap[current-1], self.heap[current*2] = self.heap[current*2], self.heap[current-1]
current = (current*2) + 1
else:
break
else:
if left<moved_value:
self.heap[current-1], self.heap[(current*2)-1] = self.heap[(current*2)-1], self.heap[current-1]
current = current*2
else:
break
return deleted
def heapSort(numList):
'''
>>> heapSort([9,7,4,1,2,4,8,7,0,-1])
[-1, 0, 1, 2, 4, 4, 7, 7, 8, 9]
'''
sort_heap = MinHeap()
for i in range (len(numList)):
sort_heap.insert(numList[i])
sortedList= []
lenght=len(numList)
while lenght >0:
new_value = sort_heap.deleteMin()
sortedList.append(new_value)
lenght -= 1
return sortedList
The MinHeap class is a given but I'm allow to modify it. Could Someone please help? Thanks
Seeing your (btw wrong formatted code, please redo the formatting) code I can see, that deleteMin is a #property and not a class method. Therefore you shuld write:
new_value = sort_heap.deleteMin
# ^ You see, no brackets here