The Data
这个部分描述的是怎样在Orange上加载数据。我们也将展示如何探索数据,发现一些基本统计特性,怎么数据取样。
Data Input
Orange可以读取本机以制表符分隔的格式的文件,也可以从任何主要的标准电子表格文件类型加载数据,像CSV,Excel。本机以制表符分割的格式的文件有通常第一行是特征名称(或者是列名);第二行是属性类型,可以使连续,离散,时间,或者字符串;第三行包含一些元信息,用于标识依赖特性(类)、无关特性(忽略)或元特性(元)。看下面的数据:
这个数据集有四个属性(患者年龄,眼镜处方,散光概念,泪液生成率信息)和关于病人的三个值的处方信息编码(硬隐形眼镜,软隐形眼镜,无镜片)
import Orangedata = Orange.data.Table('lenses')print(data.domain.attributes)
print(data.domain.class_var)for d in data[:3]:print(d)
高级输出展示:
import Orange
data = Orange.data.Table("lenses")
print("Attributes:", ", ".join(x.name for x in data.domain.attributes))
print("Class:", data.domain.class_var.name)
print("Data instances", len(data))target = "soft"
print("Data instances with %s prescriptions:" % target)
atts = data.domain.attributes
for d in data:if d.get_class() == target:print(" ".join(["%14s" % str(d[a]) for a in atts]))
Saving the Data
import Orange
data = Orange.data.Table("lenses")
myope_subset = [d for d in data if d["prescription"] == "myope"]
new_data = Orange.data.Table(data.domain, myope_subset)
new_data.save("lenses-subset.tab")
Exploration of the Data Domain
探索数据,统计离散特征和连续特征的数量。
import Orangedata = Orange.data.Table("imports-85.tab")
n = len(data.domain.attributes)
n_cont = sum(1 for a in data.domain.attributes if a.is_continuous)
n_disc = sum(1 for a in data.domain.attributes if a.is_discrete)
print("%d attributes: %d continuous, %d discrete" % (n, n_cont, n_disc))print("First three attributes:",", ".join(data.domain.attributes[i].name for i in range(3)))print("Class:", data.domain.class_var.name)
Orange的对象可以像Python的列和字典一样,可以通过特征名进行检索和访问。
print("First attribute:", data.domain[0].name)
name = "fuel-type"
print("Values of attribute '%s': %s" %(name, ", ".join(data.domain[name].values)))
Data Instances
数据Table存储了数据实例。可以像Python列表一样进行索引和遍历,可以将数据实例视为向量,通过元素索引或特性名称进行访问。针对于鸢尾花数据
import Orangedata = Orange.data.Table("iris")
print("First three data instances:")
for d in data[:3]:print(d)print("25-th data instance:")
print(data[24])name = "sepal width"
print("Value of '%s' for the first instance:" % name, data[0][name])
print("The 3rd value of the 25th data instance:", data[24][2])average = lambda x: sum(x)/len(x)print("%-15s %s" % ("Feature", "Mean"))
for x in data.domain.attributes:print("%-15s %.2f" % (x.name, average([d[x] for d in data])))average1 = lambda xs: sum(xs)/float(len(xs))targets = data.domain.class_var.values
print("%-15s %s" % ("Feature", " ".join("%15s" % c for c in targets)))
for a in data.domain.attributes:dist = ["%15.2f" % average1([d[a] for d in data if d.get_class() == c])for c in targets]print("%-15s" % a.name, " ".join(dist))
快速计算类分布的代码:
import Orange
from collections import Counterdata = Orange.data.Table("lenses")
print(Counter(str(d.get_class()) for d in data))
Orange Datasets and NumPy
import orange
import numpy as npdata = Orange.data.Table("iris")print(data.X[:3])
print(data.Y[:3])mean = np.mean(data.X,axis=0)
print(mean)#numpy数组转成dataset
X = np.array([[1,2],[4,5]])
data = Orange.data.Table(X)
print(data.domain)#设置有意义的名字
domain = Orange.data.Domain([Orange.data.ContinuousVariable("lenght"),Orange.data.ContinuousVariable("width")])
data = Orange.data.Table(domain, X)
print(data.domain)
另一个例子:
size = Orange.data.DiscreteVariable("size", ["small", "big"])
height = Orange.data.ContinuousVariable("height")
shape = Orange.data.DiscreteVariable("shape", ["circle", "square", "oval"])
speed = Orange.data.ContinuousVariable("speed")domain = Orange.data.Domain([size, height, shape], speed)X = np.array([[1, 3.4, 0], [0, 2.7, 2], [1, 1.4, 1]])
Y = np.array([42.0, 52.2, 13.4])data = Orange.data.Table(domain, X, Y)
print(data)
Missing Values
import numpy as npdata = Orange.data.Table('voting.tab')print(data[2])#查看是否缺失
print(np.isnan(data[2][0]))
print(np.isnan(data[2][1]))#统计缺失率
for x in data.domain.attributes:n_miss = sum(1 for d in data if np.isnan(d[x]))print("%4.1f%% %s" % (100.*n_miss/len(data), x.name))#至少缺失一个值统计
print(sum(any(np.isnan(d[x]) for x in data.domain.attributes) for d in data))
Data Selection and Sampling
data = Orange.data.Table("iris.tab")
print("Dataset instances:", len(data))
subset = Orange.data.Table(data.domain,[d for d in data if d["petal length"] > 3.0])
print("Subset size:", len(subset))#特征选择
new_domain = Orange.data.Domain(list(data.domain.attributes[:2]),data.domain.class_var)
new_data = Orange.data.Table(new_domain, data)print(data[0])
print(new_data[0])#特征采样
sample = Orange.data.Table(data.domain, random.sample(data, 3))#或者随机采样属性
atts = random.sample(data.domain.attributes, 2)
domain = Orange.data.Domain(atts, data.domain.class_var)
new_data = Orange.data.Table(domain, data)
print(new_data[0])
本文参考:http://docs.orange.biolab.si/3/data-mining-library/tutorial/data.html#orange-datasets-and-numpy
在做关联分析的时候发现了Orange这个库,还有Orange可视化的工具,就大致进行学习一下,数据集获取的话,在安装Orange.exe(缩写)之后有自带的数据集。