机器学习之LinearRegression与Logistic Regression逻辑斯蒂回归(三) (4)

拆分测试训练集

from sklearn.linear_model import LinearRegression,Ridge,Lasso from sklearn.neighbors import KNeighborsRegressor # 拆分训练集和测试集 from sklearn.model_selection import train_test_split # 每个人拿出9张照片做训练数据,拿出1张照片做测试数据 # 样本特征采用上半边脸,样本标签采用下半边脸 def train_test_split_face(data,test_size): X_train = [] X_test = [] y_train = [] y_test = [] for i in range(40): for j in range(10): face = data[i*10+j] up_face = face[:2048] bottom_face = face[2048:] if j < (1-test_size)*10: # 保存为训练数据 X_train.append(up_face) y_train.append(bottom_face) else: # 保存为测试数据 X_test.append(up_face) y_test.append(bottom_face) return np.array(X_train),np.array(X_test),np.array(y_train),np.array(y_test) #训练集与测试集 X_train,X_test,y_train,y_test = train_test_split_face(data,test_size=0.1) #拆分出的上下半边脸 plt.imshow(X_train[0].reshape((32,64)),cmap='gray') plt.imshow(y_train[0].reshape((32,64)),cmap='gray')

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpsdzy.html