优点:
缺点:
优点:
缺点:
优点:
缺点:
优点:
缺点:
优点:
缺点:
作用:
# In[]
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.family']='SimHei'
data=pd.read_csv('mlp_task1_data.csv')
# In[]
X=data.drop(['y'],axis=1)
y=data.loc[:,'y']
# In[]
#数据可视化
fig1=plt.figure(figsize=(5,5))
plt.scatter(X.loc[:,'x1'][y==1],X.loc[:,'x2'][y==1],label='label1')
plt.scatter(X.loc[:,'x1'][y==0],X.loc[:,'x2'][y==0],label='lable0')
plt.xlabel('x1')
plt.ylabel('x2')
plt.title('raw data')
plt.legend()
plt.show()
# In[]
#数据分离
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)
print(X_train.shape,X_test.shape,y_train.shape,y_test.shape)
#模型建立
from keras.models import Sequential
from keras.layers import Dense,Activation
#dense:层,Activation:要使用的激活函数
#创建框架
mlp=Sequential()
#创建一个有25个神经元的隐藏层(inputs_dim输入的维度)
mlp.add(Dense(units=25,input_dim=2,activation='sigmoid'))
#创建一个神经元的输出层
mlp.add(Dense(units=1,activation='sigmoid'))
mlp.summary()
#模型求解参数配置(optimiizer:求解器,loss损失函数,binary_crossentropy二分类的损失函数)
mlp.compile(optimizer='adam',loss='binary_crossentropy')
#模型训练
mlp.fit(X_train,y_train,epochs=1000)
#训练数据预测与评估
#predict_class:根据概率来给类别
y_train_predict=mlp.predict_classes(X_train)
from sklearn.metrics import accuracy_score
accuracy_train=accuracy_score(y_train,y_train_predict)
print(accuracy_train)
#测试数据预测与评估
y_test_predict=mlp.predict_classes(X_test)
accuracy_test=accuracy_score(y_test,y_test_predict)
print(accuracy_test)
#生成2D数据集
XX,yy=np.meshgrid(np.arange(0,100,1),np.arange(0,100,1))
X_range=np.c_[XX.ravel(),yy.ravel()]
print(X_range)
#生成数据结果预测
y_range_predict=mlp.predict_classes(X_range)
print(type(y_range_predict))
print(y_range_predict.shape)
#格式转化
y_range_predict_form=pd.Series(i[0] for i in y_range_predict)
print(type(y_range_predict_form))
print(y_range_predict_form)
#结果可视化
fig2=plt.figure(figsize=(5,5))
plt.scatter(X_range[:,0][y_range_predict_form==1],X_range[:,1][y_range_predict_form==1],label='label1_predict')
plt.scatter(X_range[:,0][y_range_predict_form==0],X_range[:,1][y_range_predict_form==0],label='label0_predict')
plt.scatter(X.loc[:,'x1'][y==1],X.loc[:,'x2'][y==1],label='label1')
plt.scatter(X.loc[:,'x1'][y==0],X.loc[:,'x2'][y==0],label='label0')
plt.title('predict_data')
plt.xlabel('x1')
plt.ylabel('x2')
plt.legend()
plt.show()
#逐步迭代及结果可视化
accuracy_train=[]
accuracy_test=[]
for i in range(0,21):
if i ==0:
mlp.fit(X_train,y_train,epochs=1)
else:
mlp.fit(X_train,y_train,epochs=500)
y_train_predict=mlp.predict_classes(X_train)
accuracy_train_i=accuracy_score(y_train,y_train_predict)
y_test_predict=mlp.predict_classes(X_test)
accuracy_test_i=accuracy_score(y_test,y_test_predict)
accuracy_train.append(accuracy_train_i)
accuracy_test.append(accuracy_test_i)
#生成2D数据集
xx, yy = np.meshgrid(np.arange(0,100,1),np.arange(0,100,1))
x_range = np.c_[xx.ravel(),yy.ravel()]
y_range_predict = mlp.predict_classes(x_range)
#format the output
y_range_predict_form = pd.Series(i[0] for i in y_range_predict)
fig_i = plt.figure(figsize=(5,5))
label1_predict=plt.scatter(x_range[:,0][y_range_predict_form==1],x_range[:,1][y_range_predict_form==1])
label0_predict=plt.scatter(x_range[:,0][y_range_predict_form==0],x_range[:,1][y_range_predict_form==0])
label1=plt.scatter(X.loc[:,'x1'][y==1],X.loc[:,'x2'][y==1])
label0=plt.scatter(X.loc[:,'x1'][y==0],X.loc[:,'x2'][y==0])
plt.legend((label1,label0,label1_predict,label0_predict),('label1','label0','label1_predict','label0_predict'))
plt.xlabel('x1')
plt.ylabel('x2')
plt.title('epochs={}'.format(1+i*500))
plt.show()
fig_i.savefig('{}.png'.format(i),dpi=500,bbox_inches = 'tight')
# In[]
n = [1+i*500 for i in range(0,21)]
#K值变化对准确率影响的结果可视化
fig10=plt.figure(figsize=(12,5))
fig10_1=plt.subplot(121)
plt.plot(n,accuracy_train,marker='o')
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.title('training data accuracy')
fig10_2=plt.subplot(122)
plt.plot(n,accuracy_test,marker='o')
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.title('testing data accuracy')
plt.show()
# In[]
import imageio
def create_gif(image_list, gif_name, duration=0.35):
frames = []
for image_name in image_list:
frames.append(imageio.imread(image_name))
imageio.mimsave(gif_name, frames, 'GIF', duration=duration)
return
def main():
image_list = ['0.png','1.png','2.png','3.png','4.png','5.png','6.png','7.png','8.png','9.png','10.png','11.png','12.png','13.png','14.png','15.png','16.png','17.png','18.png','19.png','20.png']
gif_name = 'mlp.gif'
duration = 0.35
create_gif(image_list, gif_name, duration)
if __name__ == '__main__':
main()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# In[]
#图像加载与展示
from keras.datasets import fashion_mnist
(X_train,y_train),(X_test,y_test)=fashion_mnist.load_data()
print(type(X_train),'\n',X_train.shape)
# In[]
#样本可视化
img1=X_train[0]
fig1=plt.figure(figsize=(3,3))
plt.imshow(img1)
plt.title('raw img 1')
#输入数据维度转化
feature_size=img1.shape[0]*img1.shape[1]
X_train_format=X_train.reshape(X_train.shape[0],feature_size)
X_test_format=X_test.reshape(X_test.shape[0],feature_size)
print(X_train.shape)
print(X_train_format.shape)
# In[]
#数据的归一化处理
X_train_normal=X_train_format/255
X_test_normal=X_test_format/255
print(X_train_normal[0])
# In[]
#格式化输出结果(labels)
from keras.utils import to_categorical
y_train_format=to_categorical(y_train)
y_test_format=to_categorical(y_test)
print(y_train[0])
print(y_train_format[0])
print(y_train.shape,y_train_format.shape)
#模型建立
from keras.models import Sequential
from keras.layers import Dense,Activation
mlp=Sequential()
mlp.add(Dense(units=392,activation='relu',input_dim=784))
mlp.add(Dense(units=196,activation='relu'))
mlp.add(Dense(units=10,activation='softmax'))
mlp.summary()
#参数配置
mlp.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['categorical_accuracy'])
#多类值categorical_crossentropy,categorical_accuracy类别准确率
#模型训练
mlp.fit(X_train_normal,y_train_format,epochs=10)
#结果预测
y_train_predict=mlp.predict_classes(X_train_normal)
print(type(y_train_predict))
print(y_train_predict[0:10])
# In[]
#训练数据集预测准确率
from sklearn.metrics import accuracy_score
accuracy_train=accuracy_score(y_train,y_train_predict)
print(accuracy_train)
#测试数据集预测准确率
y_test_predict=mlp.predict_classes(X_test_normal)
accuracy_test=accuracy_score(y_test,y_test_predict)
print(accuracy_test)
#创建结果对应标签字典
label_dic={0:'T恤',1:'裤子',2:'套头衫',3:'裙子',4:'外套',5:'凉鞋',6:'衬衫',7:'运动鞋',8:'包',9:'裸靴'}
print(label_dic)
#可视化前九张图的预测结果
a=[i for i in range(1,10)]
fig4=plt.figure(figsize=(5,5))
for i in a:
plt.subplot(3,3,i)
plt.tight_layout()
plt.imshow(X_test[i])
plt.title('predict:{}'.format(label_dic[y_test_predict[i]]))
plt.xticks([])
plt.yticks([])