Modification in the code to classify it into less than 100 classes - python-2.7

I am dealing with a classification problem. The following code only works when I divide my data into 100 classes. If I divide my data into less than 100 classes it gives me error that the index is out of bound.My training labels are 85000 and test labels are 15000 Can some one please tell me that it is giving this error and how to fix it?
def dense_to_one_hot(labels_dense, num_classes):
num_labels = labels_dense.shape[0]
index_offset = numpy.arange(num_labels) * num_classes
labels_one_hot = numpy.zeros((num_labels, num_classes))
labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
return labels_one_hot
def extract_labels(labels,num_classes, one_hot=False):
if one_hot :
return dense_to_one_hot(labels,num_classes)
return labels

Related

Sorting sizes with two or more numbers in Django

I am trying to sort items that have sizes described by two numbers like the following
10 x 13
100 x 60
7 x 8
The size is saved as a string. I want them sorted like this (first by first dimension, then by second dimension)
7 x 8
10 x 13
100 x 60
how can this be achieved with Django? It would be nice if we could somehow use
Item.objects.sort
I would advice not to store these as a string, but as two IntegerFields, for example, with:
class Item(models.Model):
width = models.IntegerField()
height = models.IntegerField()
#property
def size(self):
return f'{self.width}x{self.height}'
#size.setter
def size(self, value):
self.width, self.height = map(int, value.split('x'))
Then you can easily sort by Item.objects.order_by('width', 'height') for example. We thus have a property .size that can format the item to a size, and even with a setter that can "parse" the value and put the width and height in the corresponding fields.
you could use sorted for this from python math library. Had a similar problem way back this is what I used and it worked just fine.
import math
l = ['10x13', '100x60','7x8']
sorted(l, key=lambda dim: math.hypot(*map(int, dim.split('x'))))
# ['7x8', '10x13', '100x60']

Creating a List of Event Counts

I am trying to loop through my data and for every time a threshold is exceeded, i want to rasie a flag and count it. At the end i want an output of a data frame having those rows that were flagged and their corresponding information
I have gotten this far..
frame_of_reference = frame_of_reference.apply(pd.to_numeric, errors='coerce')
window_size = 10
for i in range(1, len(frame_of_reference['Number_of_frames']), window_size):
events_ttc = [1 if 0.0 < frame_of_reference['TTC_radar'].any() <= 1.5 else 0]
events_ttc
but instead of giving me a dataframe, it only gives me a one or a zero.
Screenshot:

Rule out solutions in pyomo

New to pyomo and python in general and I am trying to implement a simple solution to a binary integer programming problem. However the problem is large but a large percentage of the values of the matrix x are known in advance. I have been trying to figure out how to 'tell' pyomo that some values are known in advance and what they are.
from __future__ import division # converts to float before division
from pyomo.environ import * # Make symbolds used by pyomo known to python
model = AbstractModel() # Declaration of an abstract model, called model
model.users = Set()
model.slots = Set()
model.prices=Param(model.users, model.slots)
model.users_balance=Param(model.users)
model.slot_bounds=Param(model.slots)
model.x = Var(model.users, model.slots, domain=Binary)
# Define the objective function
def obj_expression(model):
return sum(sum(model.prices[i,j] * model.x[i,j] for i in model.users)
for j in model.slots)
model.OBJ = Objective(rule=obj_expression, sense=maximize)
# A user can only be assigned to one slot
def one_slot_rule(model, users):
return sum(model.x[users,n] for n in model.slots) <= 1
model.OneSlotConstraint = Constraint(model.users, rule=one_slot_rule)
# Certain slots have a minimum balance requirement.
def min_balance_rule1(model, slots):
return sum(model.x[n,slots] * model.users_balance[n] for n in
model.users) >= model.slot_bounds[slots]
model.MinBalanceConstraint1 = Constraint(model.slots,
rule=min_balance_rule1)
So I want to be able to benefit from the fact that I know certain values of x[i,j] to be 0. So for example I have a list of extra conditions
x[1,7] = 0
x[3,6] = 0
x[5,8] = 0
How do I include this information in order to benefit from reducing the search space?
Many Thanks.
After the model is constructed you can do the following:
model.x[1,7].fix(0)
model.x[3,6].fix(0)
model.x[5,8].fix(0)
or, assuming that you have a Set, model.Arcs, that contains the following:
model.Arcs = Set(initialize=[(1,7), (3,6), (5,8)])
you can fix x variables in a loop:
for i,j in model.Arcs:
model.x[i,j].fix(0)

Creating standalone butter filter with Matlab coder

i'm trying to compile with Matlab Coder a custom function which includes a butterworth filter.
I've extracted the lines that give me problem with the Matlab coder function.
function [output] = myfilter(input,fs) %#codegen
f1 = 5; % cuttoff low frequency to get rid of baseline wander
Wn = f1.*2./fs; % cutt off based on fs
N = 3; % order of 3 less processing
[a,b] = butter(N,Wn); % bandpass filtering
output = filtfilt(a,b,input); % filtering
output = output/max(abs(output));
I'm getting an error when I launch the command to compile it:
codegen -config:lib -launchreport myfilter -args {zeros(1,100),10}
The output error is: All inputs must be constant.
I tried to use the function coder.const modifying the code as follows, but still have the same problem:
function [output] = myfilter(input,fs) %#codegen
f1 = 5; % cuttoff low frequency to get rid of baseline wander
Wn = f1.*2./fs; % cutt off based on fs
N = 3; % order of 3 less processing
[a,b] = coder.const(#butter,N,Wn); % bandpass filtering
a = coder.const(a);
b = coder.const(b);
output = filtfilt(a,b,input); % filtering
output = output/max(abs(output));
Can somebody help me with this issue? I'm new to Matlab Coder. Thanks in advance!
The function butter needs to have constant inputs. This means that when generating code MATLAB Coder must be able to determine values for all of them.
In this case, the code passes Wn which is computed from the top input fs. Because it depends on a top level input, Wn is not a constant.
Two options to consider:
Pass a literal constant for Wn in the call to butter like you did for N
Pass a constant to the codegen call for fs so that the computation for Wn can be constant folded
codegen -config:lib -launchreport myfilter -args {zeros(1,100),coder.Constant(10)}

Using a percent to find a partial python

I'm having some difficulty trying to figure out a puzzle that I'm working on.
The idea is to find the remaining total when subtracting the percentage.
This should be a fairly simple exercise but for one reason or another I'm having difficulty finding a result that works properly.
Please forgive my formatting, this is my first post.
from math import floor
def compute_active_users(n, b):
x = float(b)/float(n) * 100
x = x * 100
return floor((n - x))
print '-' * 25
print compute_active_users(1000,25) # Expected output: ------- 750
print '-' * 25
print compute_active_users(835,17) # Expected output: ------- 693
print '-' * 25
Results:
-------------------------
750.0
-------------------------
631.0
-------------------------
The following will do it:
def compute_active_users(n, b):
return n * (1 - b / 100.)
Here:
b / 100. converts the percentage into a fraction.
1 - ... computes the remaining fraction.
n * ... computes the remaining number of users.
Note that this will happily return a fractional number of users, so you may want to add rounding as the final step.