Any fast method to cast a numpy array to a custom class? - python

I have a numpy array of shape (1000, 20) where a row is has 19 float values (feature) and one last value is a binary value (label).
The custom class is:
class Point(object):
feature = None
label = None
def __init__(self, feature, label):
self.feature = feature
self.label = label
def __str__(self):
return "{0} {1} labelled {2}".format(self.__class__.__name__, self.feature, self.label)
def __repr__(self):
return "{0}({1}, {2})".format(self.__class__.__name__, self.feature, self.label)
Is there a way can just pass the matrix and all rows can be converted to Point object? Or should I just iterate through each row?
For iterative solution I used this function:
def to_data_point(matrix):
new_data = []
for row in matrix:
feature = row[:-1]
label = row[-1]
new_data.append(Point(feature, label))
return new_data

Related

changing value of an instance of a class from another class

I have training and test dataset as Pytorch Tensor objects and I want to change their values from another class, is this somehow possible? The code below does not change their values:
class BO:
def __init__(self):
self.data = DataCollector().df_subset
self.risk = DataCollector().risk_subset.unique(dim=0)
self.device = torch.device('cpu')
self.X_init = None
self.outs_init = torch.zeros(0)
self.y_init = []
if torch.cuda.is_available():
self.device = torch.device('cuda:7')
def create_init_X_Y(self):
'''
Creates the initial X and y values.
'''
model.TrainDataset().df = self.data
_, loss = model.train()
I need to overwrite the value of model.TrainDataset().df with the value of self.data.

Python classes and types

I think I'm misusing the concept of subclass. I'm working on a hobby project with Grids and Cells.
What I have, is the implementation of a Cell class, and its subclass HexCell which basically redefines many of the attributes/methods like so:
class Cell:
def __init__(self, row_loc, col_loc):
self.row = row_loc
self.col = col_loc
self.links = set()
self.neighbors = 4*[None]
def __repr__(self):
return f'Cell #({self.row},{self.col})'
def link(self, other, bidir = True):
self.links.add(other)
if bidir: other.links.add(self)
Then I have a subclass that is the HexGrid which follows a similar structure with new parameters.
class HexCell(Cell):
def __init__(self, r_out, th_around):
# I'm indexing Hex cells around a center cell
# instead of by rows and columns; Prefixed hex
# as they follow the hexagon, and not regular polar coordinates.
self.hex_r = r_out
self.hex_th = th_around
self.neighbors = 6*[None]
self.links = set()
def __repr__(self):
return f"HexCell #[{self.hex_r}, {self.hex_th}]"
def bind(self, other, to_dir):
to_dir = to_dir % 6
if (self.neighbors[to_dir] is None):
self.neighbors[to_dir] = other
other.neighbors[to_dir - 3] = self
# Hexagonal grids share neighbors.
other_1 = other.neighbors[to_dir - 2]
if (self.neighbors[to_dir - 1] is None) & (other_1 is not None):
self.bind(other_1, to_dir - 1)
other_5 = other.neighbors[to_dir - 4]
if (self.neighbors[to_dir - 5] is None) & (other_5 is not None):
self.bind(other_5, to_dir - 5)
In this case, the method self.link(other) is shared, but other attributes change from rectangular grid to hexagonal like the locaion from (row, col) to (hex_r, hex_th), or neighbors as a 4-list or 6-list. Thus I'd like these attributes to be dependent on a another cell-type attribute and transferred down to the subclass.
Correct use of subclassing needs to obey the following substitution principle:
If there are some objects x_1 of type T_1 and x_2 of type T_2 such that issubclass(T_2, T_1) == True, then any property that applies to x_1 must also apply for x_2.
In other words, you expect subclassing to implement new behaviours, not to change existing behaviours.
In you example, the change of coordinate system itself is a change of behaviour and thus HexCell should not inherit from Cell.
What you can do is create a base class BaseCell that encapsulates the common behaviour between Cell and HexCell and inherit from it.
class BaseCell:
def __init__(self):
self.links = set()
self.neighbors = []
def add_neighbor(self, other):
self.neighbors.append(other)
def link(self, other, bidirectional=True):
self.links.add(other)
if bidirectional:
other.link(self, bidirectional=False)
class Cell(BaseCell):
def __init__(self, row_loc, col_loc):
self.row = row_loc
self.col = col_loc
super().__init__()
def __repr__(self):
return f'Cell #({self.row},{self.col})'
class HexCell(Cell):
def __init__(self, r_out, th_around):
self.hex_r = r_out
self.hex_th = th_around
super().__init__()
def __repr__(self):
return f"HexCell #[{self.hex_r}, {self.hex_th}]"
def bind(self, other, to_dir):
...
Your Cell class is in fact not an abstract "Cell", but a square cell in two-dimensional space (has exactly 4 neighbours, has "row" and "col" position). Such cell may not be subclassed by a hex cell, because hex cell is just a different type of cell : )
As you noticed, the only common things are link() method and links attribute. If you insist on subclassing, you could create something like:
class LinkedObject():
def __init__(self):
self.links = set()
def link(self, other, bidir = True):
self.links.add(other)
if bidir: other.links.add(self)
class SquareCell(LinkedObject):
# "Cell" class here
class HexCell(LinkedObject):
# HexCell here

What do I do if a method in one class uses a method from another?

I have this class (in a file named "occ_grid.py"):
class Grid:
def __init__(self, width, height, occupancy_value):
self.width = width
self.height = height
self.cells = []
# initialize grid to all specified occupancy value
for row in range(0, self.height):
self.cells.append([])
for col in range(0, self.width):
self.cells[row].append(occupancy_value)
def set_cell(self, point, value):
self.cells[point.y][point.x] = value
def get_cell(self, point):
return self.cells[point.y][point.x]
And I have another class/method(s) (in the file "worldmodel.py"):
class WorldModel:
def __init__(self, num_rows, num_cols, background):
self.background = occ_grid.Grid(num_cols, num_rows, background)
self.num_rows = num_rows
self.num_cols = num_cols
self.occupancy = occ_grid.Grid(num_cols, num_rows, None)
self.entities = []
self.action_queue = ordered_list.OrderedList()
def is_occupied(self, pt):
return (self.within_bounds(pt) and
occ_grid.get_cell(self.occupancy, pt) != None)
Notice how "def is_occupied" uses the method "get_cell". The thing is, "get_cell" is in a different file and it's a method in a different class. I thought about creating a new "grid" object, but I'm confused on where in my code I should create this object.
You're pretty close. It's just:
self.occupancy.get_cell(pt)
You could write:
occ_grid.Grid.get_cell(self.occupancy, pt)
and that would be equivalent -- But, that'd be pretty unidiomatic.
In order to use Grid at all from the file worldmodel.py, you need to import the Grid class.
You can do this in two possible ways.
import occ_grid. If you do this, you will have to access Grid with occ_grid.Grid
from occ_grid import Grid. Then you can use it with just Grid.
mgilson's answer is very clear about how to use it after you import it.

Creating Vectors with Python

I'm creating a vector class that has one parameter being the length of a vector. The length is automatically 0 if none is entered by user. If a vector is given a length, however, each number will be set to 0. For example: v(5) would be [0,0,0,0,0] and v() would be [].
This is the code i have thus far, but it's not quite working. Any advice?
class V:
def __init__(self, length = 0):
self.vector = [0]*length
def __str__(self):
print(self.vector)
def __len__(self):
return len(self.vector)
Then i plug in a = V() b = V(5) and when i print(a) and print(b) i get an TypeError. Any advice?
I'd probably cheat and go for sub-classing list:
class V(list):
def __init__(self, length=0):
super(V, self).__init__([0] * length)
This way you get the length, repr and other goodies for free.
class V:
def __init__(self, length = 0):
self.data = [0]*length
def __str__(self):
return '[{}]'.format(', '.join(str(d) for d in self.data))
def __len__(self):
return len(self.data)

python cast object to int

I am using the numpy module to retrieve the position of the maximum value in a 2d array. But this 2d array consists of MyObjects. Now I get the error:
TypeError: unorderable types: int() > MyObject()
I tried to override the int function with this code:
def int(self):
return self.score
But this does not solve my problem.
Do I have to convert my 2d array of MyObjects into a 2d array of integers, do I have to extend the Integer object (if this is possible in python) or can I override this int() function in another way?
[EDIT]
The full object:
class MyObject:
def __init__(self, x, y, score, direction, match):
self.x = x
self.y = y
self.score = score
self.direction = direction
self.match = match
def __str__(self):
return str(self.score)
def int(self):
return self.score
The way I call this object:
def traceBack(self):
self.matrix = np.array(self.matrix)
maxIndex = self.matrix.argmax()
print(self.matrix.unravel_index(maxIndex))
Try to use
...
def __int__(self):
return self.score
...
test = MyObject(0, 0, 10, 0, 0)
print 10+int(test)
# Will output: 20
in your MyObject class definition.
The max function takes a key that is applied on the elements. that's where you put score
Typically :
a = max(my_list, key=score)

Categories

Resources