这些数据集都可以在官⽹上查到,以鸢尾花为例,可以在官⽹上找到demo,
from sklearn.datasets import load_iris#加载数据集iris=load_iris()
iris.keys() #dict_keys(['target', 'DESCR', 'data', 'target_names', 'feature_names'])#数据的条数和维数
n_samples,n_features=iris.data.shape
print(\"Number of sample:\",n_samples) #Number of sample: 150print(\"Number of feature\",n_features) #Number of feature 4#第⼀个样例
print(iris.data[0]) #[ 5.1 3.5 1.4 0.2]print(iris.data.shape) #(150, 4)print(iris.target.shape) #(150,)print(iris.target)\"\"\"
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]
\"\"\"
import numpy as np
print(iris.target_names) #['setosa' 'versicolor' 'virginica']np.bincount(iris.target) #[50 50 50]
import matplotlib.pyplot as plt
#以第3个索引为划分依据,x_index的值可以为0,1,2,3x_index=3
color=['blue','red','green']
for label,color in zip(range(len(iris.target_names)),color):
plt.hist(iris.data[iris.target==label,x_index],label=iris.target_names[label],color=color)plt.xlabel(iris.feature_names[x_index])
plt.legend(loc=\"Upper right\")plt.show()
#画散点图,第⼀维的数据作为x轴和第⼆维的数据作为y轴x_index=0y_index=1
colors=['blue','red','green']
for label,color in zip(range(len(iris.target_names)),colors): plt.scatter(iris.data[iris.target==label,x_index], iris.data[iris.target==label,y_index], label=iris.target_names[label], c=color)
plt.xlabel(iris.feature_names[x_index])plt.ylabel(iris.feature_names[y_index])plt.legend(loc='upper left')plt.show()
⼿写数字数据集load_digits():⽤于多分类任务的数据集
from sklearn.datasets import load_digitsdigits=load_digits()print(digits.data.shape)
import matplotlib.pyplot as pltplt.gray()
plt.matshow(digits.images[0])plt.show()
from sklearn.datasets import load_digitsdigits=load_digits()digits.keys()
n_samples,n_features=digits.data.shapeprint((n_samples,n_features))print(digits.data.shape)print(digits.images.shape)
import numpy as np
print(np.all(digits.images.reshape((1797,))==digits.data))
fig=plt.figure(figsize=(6,6))
fig.subplots_adjust(left=0,right=1,bottom=0,top=1,hspace=0.05,wspace=0.05)#绘制数字:每张图像8*8像素点for i in range():
ax=fig.add_subplot(8,8,i+1,xticks=[],yticks=[])
ax.imshow(digits.images[i],cmap=plt.cm.binary,interpolation='nearest') #⽤⽬标值标记图像
ax.text(0,7,str(digits.target[i]))plt.show()
乳腺癌数据集load-barest-cancer():简单经典的⽤于⼆分类任务的数据集
糖尿病数据集:load-diabetes():经典的⽤于回归认为的数据集,值得注意的是,这10个特征中的每个特征都已经被处理成0均值,⽅差归⼀化的特征值,
波⼠顿房价数据集:load-boston():经典的⽤于回归任务的数据集
体能训练数据集:load-linnerud():经典的⽤于多变量回归任务的数据集,其内部包含两个⼩数据集:Excise是对3个训练变量的20次观测(体重,腰围,脉搏),physiological是对3个⽣理学变量的20次观测(引体向上,仰卧起坐,⽴定跳远)svmlight/libsvm的每⼀⾏样本的存放格式:
: : ....这种格式⽐较适合⽤来存放稀疏数据,在sklearn中,⽤scipy sparse CSR矩阵来存放X,⽤numpy数组来存放Y
from sklearn.datasets import load_svmlight_file
x_train,y_train=load_svmlight_file(\"/path/to/train_dataset.txt\",\"\")#如果要加在多个数据的时候,可以⽤逗号隔开
②⽣成数据集
⽣成数据集:可以⽤来分类任务,可以⽤来回归任务,可以⽤来聚类任务,⽤于流形学习的,⽤于因⼦分解任务的⽤于分类任务和聚类任务的:这些函数产⽣样本特征向量矩阵以及对应的类别标签集合make_blobs:多类单标签数据集,为每个类分配⼀个或多个正太分布的点集
make_classification:多类单标签数据集,为每个类分配⼀个或多个正太分布的点集,提供了为数据添加噪声的⽅式,包括维度相关性,⽆效特征以及冗余特征等
make_gaussian-quantiles:将⼀个单⾼斯分布的点集划分为两个数量均等的点集,作为两类make_hastie-10-2:产⽣⼀个相似的⼆元分类数据集,有10个维度
make_circle和make_moom产⽣⼆维⼆元分类数据集来测试某些算法的性能,可以为数据集添加噪声,可以为⼆元分类器产⽣⼀些球形判决界⾯的数据
#⽣成多类单标签数据集import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_blobscenter=[[1,1],[-1,-1],[1,-1]]cluster_std=0.3
X,labels=make_blobs(n_samples=200,centers=center,n_features=2, cluster_std=cluster_std,random_state=0)print('X.shape',X.shape)print(\"labels\",set(labels))
unique_lables=set(labels)
colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables)))for k,col in zip(unique_lables,colors): x_k=X[labels==k]
plt.plot(x_k[:,0],x_k[:,1],'o',markerfacecolor=col,markeredgecolor=\"k\", markersize=14)
plt.title('data by make_blob()')plt.show()
#⽣成⽤于分类的数据集
from sklearn.datasets.samples_generator import make_classification
X,labels=make_classification(n_samples=200,n_features=2,n_redundant=0,n_informative=2, random_state=1,n_clusters_per_class=2)
rng=np.random.RandomState(2)X+=2*rng.uniform(size=X.shape)
unique_lables=set(labels)
colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables)))for k,col in zip(unique_lables,colors): x_k=X[labels==k]
plt.plot(x_k[:,0],x_k[:,1],'o',markerfacecolor=col,markeredgecolor=\"k\", markersize=14)
plt.title('data by make_classification()')plt.show()
#⽣成球形判决界⾯的数据
from sklearn.datasets.samples_generator import make_circles
X,labels=make_circles(n_samples=200,noise=0.2,factor=0.2,random_state=1)print(\"X.shape:\",X.shape)print(\"labels:\",set(labels))
unique_lables=set(labels)
colors=plt.cm.Spectral(np.linspace(0,1,len(unique_lables)))for k,col in zip(unique_lables,colors): x_k=X[labels==k]
plt.plot(x_k[:,0],x_k[:,1],'o',markerfacecolor=col,markeredgecolor=\"k\", markersize=14)
plt.title('data by make_moons()')plt.show()