fetch_lfw_people相关
生活随笔
收集整理的這篇文章主要介紹了
fetch_lfw_people相关
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
from sklearn.datasets import fetch_lfw_people#導入數據集,第一次可能等待時間過長,因為要在網上下載數據集
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
people = fetch_lfw_people(min_faces_per_person=2,resize = 0.7)print(people.target_names)print("圖像個數以及像素:{}".format(people.images.shape))#(9164, 87, 65) 9164為圖像個數,每張像素為87*65print("這些圖像來自于{}個人".format(len(people.target_names)))imga_shape = people.images[0].shapeprint("每張圖像的像素:{}".format(imga_shape))fix,axes = plt.subplots(2,5,figsize = (15,8),subplot_kw ={'xticks':(),'yticks':()}) #展示前10張人臉
for target,image , ax in zip(people.target, people.images, axes.ravel()):ax.imshow(image)ax.set_title(people.target_names[target])counts = np.bincount(people.target)
#print(len(counts))#總共有多少個人
'''
for i,(count,name) in enumerate(zip(counts,people.target_names)):print("{0:25}{1:3}".format(name,count),end=' ')if (i+1)%3 == 0:print()
'''
print(min(counts))mask = np.zeros(people.target.shape,dtype=np.bool)
for target in np.unique(people.target):mask[np.where(people.target == target)[0][:50]] == 1X_people = people.data[mask]
y_people = people.target[mask] X_people = X_people/225 from sklearn.neighbors import KNeighborsClassifierX_train, X_test, y_train, y_test = train_test_split( X_people, y_people, stratify = y_people, random_state = 0)
#參數stratify: 依據標簽y,按原數據y中各類比例,分配給train和test,使得train和test中各類數據的比例與原數據集一樣。knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train,y_train)
print("Test set score of knn:{:.2f}".format(knn.score(X_test,y_test)))print("\n")
print("圖片來源于名為{}的人!".format(people.target_names))
print("\n")
print("這些圖像來自于{}個人".format(len(people.target_names)))
print("\n")
print("圖像個數以及像素:{}".format(people.images.shape))#(9164, 87, 65) 9164為圖像個數,每張像素為87*65
print("\n")imga_shape = people.images[0].shapeprint("每張圖像的像素:{}".format(imga_shape))
print("\n")fix,axes = plt.subplots(2,5,figsize = (15,8),subplot_kw ={'xticks':(),'yticks':()}) #展示前10張人臉
for target,image , ax in zip(people.target, people.images, axes.ravel()):ax.imshow(image)ax.set_title(people.target_names[target])counts = np.bincount(people.target)
print(counts[0:20])
#print(len(counts))#總共有多少個人
print(np.where(counts > 5))
print("我在這里{}".format(people.target_names[5]))
print(len(list(np.where(counts > 5))[0]))lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)
from sklearn.datasets import fetch_lfw_people#導入數據集,第一次可能等待時間過長,因為要在網上下載數據集
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_splitfrom sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import fetch_lfw_people
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import PCA
from sklearn.svm import SVClfw_people = fetch_lfw_people(min_faces_per_person=20, resize=0.7)
print("圖像個數以及像素:{}".format(lfw_people.images.shape))#(9164, 87, 65) 9164為圖像個數,每張像素為87*65
print("\n")counts = np.bincount(lfw_people.target)
print("每個名字下圖像的個數為{}".format(counts)) #每個人圖像的個數分別查出來
print("\n")n_samples, h, w = lfw_people.images.shape X = lfw_people.data #圖像數據
print("圖像數據如下:")
print(X)
print("圖像數據的形狀為{}".format(X.shape))
print("\n")n_features = X.shape[1]
print("圖像數據的特征數為{}".format(n_features))
print("\n")
#print(X[0][0:15])第一條數據前15個數y = lfw_people.target
print("y為{}".format(y)) #3023個圖像數據分別屬于哪些人,人的名字用0到61編了號
#print(y[0:100])
print("圖像數據的個數(y的形狀)為 {}".format(y.shape)) #圖像數據的個數
print("圖像數據的個數(y的形狀)為 {}".format(len(y))) #同上,圖像數據的個數
print("y的數據類型為{}".format(type(y))) #y的數據類型
print("y的最大值(即總共有(最大值+1)個人)為{}".format(max(y)))
print("\n")target_names = lfw_people.target_names
print(target_names)
print("\n")
print("圖像名字類型為{}".format(type(target_names)))
print("\n")
print("圖像名字形狀為{}".format(target_names.shape))
print("\n")
n_classes = target_names.shape[0]#print("這些圖像來自于{}個人".format(len(target_names)))
總結
以上是生活随笔為你收集整理的fetch_lfw_people相关的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java compliance_java
- 下一篇: buidulbs android.jar