Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def sendSignals(self):
if self.graph != None:
self.optimization = orngNetwork.NetworkOptimization(self.graph)
self.optimization.vertexDistance = self.matrix
self.netCanvas.addVisualizer(self.optimization)
self.netCanvas.setLabelText(["attributes"])
self.sizeAttributeCombo.clear()
self.colorCombo.clear()
self.sizeAttributeCombo.addItem("(same size)")
self.colorCombo.addItem("(same color)")
vars = self.optimization.getVars()
for var in vars:
if var.varType in [orange.VarTypes.Discrete, orange.VarTypes.Continuous]:
self.colorCombo.addItem(self.icons[var.varType], unicode(var.name))
if var.varType in [orange.VarTypes.Continuous]:
self.sizeAttributeCombo.addItem(self.icons[var.varType], unicode(var.name))
index = self.sizeAttributeCombo.findText(self.lastSizeAttribute)
if index > -1:
self.sizeAttributeCombo.setCurrentIndex(index)
self.vertexSizeAttribute = index
index = self.colorCombo.findText(self.lastColorAttribute)
if index > -1:
self.colorCombo.setCurrentIndex(index)
self.colorAttribute = index
self.setMaxVertexSize()
'''Scatterplot 3D
'''
from OWWidget import *
from plot.owplot3d import *
import orange
Discrete = orange.VarTypes.Discrete
Continuous = orange.VarTypes.Continuous
import OWGUI
import OWToolbars
import orngVizRank
from OWkNNOptimization import *
from orngScaleScatterPlotData import *
import numpy
TooltipKind = enum('NONE', 'VISIBLE', 'ALL') # Which attributes should be displayed in tooltips?
class ScatterPlotTheme(PlotTheme):
def __init__(self):
super(ScatterPlotTheme, self).__init__()
self.grid_color = [0.8, 0.8, 0.8, 1.]
# Description: Read data, output class values and attribute names, and show class distribution (in proportion of instances belonging to a class)
# Category: description
# Uses: adult_sample.tab
# Referenced: basic_exploration.htm
import orange
data = orange.ExampleTable("adult_sample.tab")
print "Classes:", len(data.domain.classVar.values)
print "Attributes:", len(data.domain.attributes), ",",
# count number of continuous and discrete attributes
ncont = 0; ndisc = 0
for a in data.domain.attributes:
if a.varType == orange.VarTypes.Discrete:
ndisc = ndisc + 1
else:
ncont = ncont + 1
print ncont, "continuous,", ndisc, "discrete"
# obtain class distribution
c = [0] * len(data.domain.classVar.values)
for e in data:
c[int(e.getclass())] += 1
print "Instances: ", len(data), "total",
r = [0.] * len(c)
for i in range(len(c)):
r[i] = c[i] * 100. / len(data)
for i in range(len(data.domain.classVar.values)):
print ", %d(%4.1f%s) with class %s" % (c[i], r[i], '%', data.domain.classVar.values[i]),
print
self.data = data
domain = data.domain
y_i, x_i, c_i, s_i = [int(random.random() * len(domain)) for i in range(4)]
self.plot.set_axis_title(xBottom, domain[x_i].name)
self.plot.set_show_axis_title(xBottom, True)
self.plot.set_axis_title(yLeft, domain[y_i].name)
self.plot.set_show_axis_title(yLeft, True)
if data.domain[x_i].varType == orange.VarTypes.Discrete:
self.plot.set_axis_labels(xBottom, get_variable_values_sorted(domain[x_i]))
else:
self.plot.set_axis_autoscale(xBottom)
if data.domain[y_i].varType == orange.VarTypes.Discrete:
self.plot.set_axis_labels(yLeft, get_variable_values_sorted(domain[y_i]))
else:
self.plot.set_axis_autoscale(yLeft)
x_data = []
y_data = []
c_data = []
s_data = []
color_cont = (domain[c_i].varType == orange.VarTypes.Continuous)
legend_sizes = set()
for e in data:
x_data.append(e[x_i])
y_data.append(e[y_i])
# Referenced: basic_exploration.htm
import orange
data = orange.ExampleTable("adult_sample.tab")
dist = orange.DomainDistributions(data)
print "Average values and mean square errors:"
for i in range(len(data.domain.attributes)):
if data.domain.attributes[i].varType == orange.VarTypes.Continuous:
print "%s, mean=%5.2f +- %5.2f" % \
(data.domain.attributes[i].name, dist[i].average(), dist[i].error())
print "\nFrequencies for values of discrete attributes:"
for i in range(len(data.domain.attributes)):
a = data.domain.attributes[i]
if a.varType == orange.VarTypes.Discrete:
print "%s:" % a.name
for j in range(len(a.values)):
print " %s: %d" % (a.values[j], int(dist[i][j]))
print "\nNumber of instances where attribute is not defined:"
for i in range(len(data.domain.attributes)):
a = data.domain.attributes[i]
print " %2d %s" % (dist[i].unknowns, a.name)
def kNNClassifyData(self, table):
if len(table) == 0:
return [], []
# check if we have a discrete class
if not table.domain.classVar or not table.domain.classVar.varType == orange.VarTypes.Discrete:
return [], []
if self.externalLearner: learner = self.externalLearner
else: learner = self.createkNNLearner()
results = apply(testingMethods[self.testingMethod], [[learner], table])
returnTable = []
if table.domain.classVar.varType == orange.VarTypes.Discrete:
probabilities = numpy.zeros((len(table), len(table.domain.classVar.values)), numpy.float)
lenClassValues = len(list(table.domain.classVar.values))
if self.qualityMeasure in [AVERAGE_CORRECT, AUC]: # for AUC we have no way of computing the prediction accuracy for each example
for i in range(len(results.results)):
res = results.results[i]
returnTable.append(res.probabilities[0][res.actualClass])
probabilities[i] = res.probabilities[0]
elif self.qualityMeasure == BRIER_SCORE:
for i in range(len(results.results)):
res = results.results[i]
s = sum([val*val for val in res.probabilities[0]])
returnTable.append((s + 1 - 2*res.probabilities[0][res.actualClass])/float(lenClassValues))
probabilities[i] = res.probabilities[0]
elif self.qualityMeasure == CLASS_ACCURACY:
for i in range(len(results.results)):
res = results.results[i]
def sendpredictions(self):
if not self.data or not self.outvar:
self.send("Predictions", None)
return
# predictions, data set with class predictions
classification = self.outvar.varType == orange.VarTypes.Discrete
metas = []
if classification:
if len(self.selectedClasses):
for c in self.predictors.values():
m = [orange.FloatVariable(name=str("%s(%s)" % (c.name, str(self.outvar.values[i]))),
getValueFrom = lambda ex, rw, cindx=i, c=c: orange.Value(c(ex, c.GetProbabilities)[cindx])) \
for i in self.selectedClasses]
metas.extend(m)
if self.showClass:
mc = [orange.EnumVariable(name=str(c.name), values = self.outvar.values,
getValueFrom = lambda ex, rw, c=c: orange.Value(c(ex)))
for c in self.predictors.values()]
metas.extend(mc)
else:
# regression
candidates=filter(lambda v:v.varType==orange.VarTypes.Discrete or v.varType==orange.VarTypes.String, candidates)
if len(data)>20:
# Referenced: basic_exploration.htm
import orange
data = orange.ExampleTable("../../datasets/adult_sample")
dist = orange.DomainDistributions(data)
print "Average values and mean square errors:"
for i in range(len(data.domain.attributes)):
if data.domain.attributes[i].varType == orange.VarTypes.Continuous:
print "%s, mean=%5.2f +- %5.2f" % \
(data.domain.attributes[i].name, dist[i].average(), dist[i].error())
print "\nFrequencies for values of discrete attributes:"
for i in range(len(data.domain.attributes)):
a = data.domain.attributes[i]
if a.varType == orange.VarTypes.Discrete:
print "%s:" % a.name
for j in range(len(a.values)):
print " %s: %d" % (a.values[j], int(dist[i][j]))
print "\nNumber of instances where attribute is not defined:"
for i in range(len(data.domain.attributes)):
a = data.domain.attributes[i]
print " %2d %s" % (dist[i].unknowns, a.name)
def nbClassifier(self, cl):
classVal = cl.domain.classVar
att = cl.domain.attributes
prior = cl.distribution[classVal[0]]/cl.distribution[classVal[1]]
bnomogram = BasicNomogram(AttValue("Constant", Numeric.log(prior)))
for at in range(len(att)):
a = AttrLine(att[at].name, at+1)
if att[at].varType == orange.VarTypes.Discrete:
for cd in cl.conditionalDistributions[at].keys():
a.addAttValue(AttValue(str(cd), Numeric.log(cl.conditionalDistributions[at][cd][classVal[0]]/cl.conditionalDistributions[at][cd][classVal[1]]/prior)))
else:
d = cl.conditionalDistributions[at].keys()[len(cl.conditionalDistributions[at].keys())-1]-cl.conditionalDistributions[at].keys()[0]
d = getDiff(d/50)
if cl.conditionalDistributions[at].keys()[0]<0:
curr_num = arange(-cl.conditionalDistributions[at].keys()[0]+d)
curr_num = curr_num[len(curr_num)-1]
curr_num = -curr_num
elif cl.conditionalDistributions[at].keys()[0] == 0:
curr_num = 0
else:
print d, cl.conditionalDistributions[at].keys()
curr_num = arange(cl.conditionalDistributions[at].keys()[0]-d)
print curr_num
curr_num = curr_num[len(curr_num)-1]