sklearn 的數據集有好多個種html
①自帶的數據集git
其中的自帶的小的數據集爲:sklearn.datasets.load_<name>算法
這些數據集均可以在官網上查到,以鳶尾花爲例,能夠在官網上找到demo,http://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html數組
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: 150 print("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]dom
""" 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,3 x_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=0 y_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_digits digits=load_digits() print(digits.data.shape) import matplotlib.pyplot as plt plt.gray() plt.matshow(digits.images[0]) plt.show() from sklearn.datasets import load_digits digits=load_digits() digits.keys() n_samples,n_features=digits.data.shape print((n_samples,n_features)) print(digits.data.shape) print(digits.images.shape) import numpy as np print(np.all(digits.images.reshape((1797,64))==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(64): 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次觀測(引體向上,仰臥起坐,立定跳遠)fetch
svmlight/libsvm的每一行樣本的存放格式:
<label><feature-id>:<feature-value> <feature-id>:<feature-value> ....
這種格式比較適合用來存放稀疏數據,在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_blobs center=[[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()