def drowPicture(History): pyplot.plot(History.history['loss']) pyplot.plot(History.history['val_loss']) pyplot.plot(History.history['acc']) pyplot.plot(History.history['val_acc']) pyplot.title('model train vs validation loss') pyplot.ylabel('loss') pyplot.xlabel('epoch') pyplot.legend(['train', 'validation'], loc='upper right') pyplot.show()
return (train_metrics, test_metrics) # 不带约束的 # model = Model(FEATURE_NAMES, hidden_units=10) # model.build_train_op(0.01, unconstrained=True) # results = training_helper(model, train_df, test_df, 100, num_iterations_per_loop=44, num_loops=500) # print("Train Error", results[0]["last_error_rate"]) # print("Train Violation", results[0]["last_max_constraint_violation"]) model = Model(FEATURE_NAMES, hidden_units=10) model.build_train_op(0.01, unconstrained=False) results = training_helper(model, train_df, test_df, 100, num_iterations_per_loop=44, num_loops=500) print("Train Error", results[0]["last_error_rate"]) print("Train Violation", results[0]["last_max_constraint_violation"]) """画图""" plt.title("Error Rate vs Epoch") plt.plot(range(100, len(results[0]["all_errors"])), results[0]["all_errors"][100:], color="green") plt.xlabel("Epoch") plt.show() plt.title("Violation vs Epoch") plt.plot(range(100, len(results[0]["all_violations"])), results[0]["all_violations"][100:], color="blue") plt.xlabel("Epoch") plt.show()
from pandas import read_csv from Example_matplotlib import pyplot # load dataset dataset = read_csv('pollution.csv', header=0, index_col=0) values = dataset.values # specify columns to plot groups = [0, 1, 2, 3, 5, 6, 7] i = 1 # plot each column pyplot.figure() for group in groups: pyplot.subplot(len(groups), 1, i) pyplot.plot(values[:, group]) pyplot.title(dataset.columns[group], y=0.5, loc='right') i += 1 pyplot.show()
return X, y # define model model = Sequential() model.add(LSTM(10, input_shape=(1, 1))) # activation是激活函数的选择,linear是线性函数 model.add(Dense(1, activation='linear')) # compile model, loss是损失函数的取值方式,mse是mean_squared_error,代表均方误差, # optimizer是优化控制器的选择,AdamOptimizer通过使用动量(参数的移动平均数)来改善传统梯度下降 model.compile(loss='mse', optimizer='adam') # fit model X, y = get_train() valX, valY = get_val() # validation_data是要验证的测试集,shuffle代表是否混淆打乱数据 # 不合格的原因是epochs=100,训练周期不足 history = model.fit(X, y, epochs=100, validation_data=(valX, valY), shuffle=False) # plot train and validation loss # loss是训练集的损失函数值,val_loss是验证数据集的损失值 pyplot.plot(history.history['loss']) pyplot.plot(history.history['val_loss']) pyplot.title('model train vs validation loss') pyplot.ylabel('loss') pyplot.xlabel('epoch') pyplot.legend(['train', 'validation'], loc='upper right') pyplot.show()
# -*-coding:utf-8-*- # @Time : 2019/6/18 0018 10:58 # @Author :zhuxinquan # @File : matplotlib_01.py import Example_matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap plt.figure(1) # map=Basemap() map = Basemap(llcrnrlon=70, llcrnrlat=3, urcrnrlon=139, urcrnrlat=54) # 画中国 经135度2分30秒-东经73度40分,北纬3度52分-北纬53度33分 map.drawcoastlines() map.drawcountries() # 加海岸线 # map.drawrivers(color='blue', linewidth=0.3) # 加河流 CHN = r"C:\Users\Administrator\Downloads" map.readshapefile(CHN + r'\gadm36_CHN_shp\gadm36_CHN_1', 'states', drawbounds=True) # 加省界 # map.readshapefile(CHN+r'\gadm36_TWN_shp\gadm36_TWN_1', 'taiwan', drawbounds=True) # 加台湾 plt.title(r'$World\ Map$', fontsize=24) plt.show()
def pic_map(): import numpy as np import pandas as pd import Example_matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap from Example_matplotlib.patches import Polygon from Example_matplotlib.colors import rgb2hex from Example_matplotlib.collections import PatchCollection from Example_matplotlib import pylab plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体 plt.rcParams['axes.unicode_minus'] = False plt.figure(figsize=(20, 10)) # 长和宽 # m= Basemap(llcrnrlon=73, llcrnrlat=18, urcrnrlon=135, urcrnrlat=55) #指定中国的经纬度 m = Basemap(llcrnrlon=77, llcrnrlat=14, urcrnrlon=140, urcrnrlat=51, projection='lcc', lat_1=33, lat_2=45, lon_0=100) # ‘lcc'将投影方式设置为兰伯特投影 # projection='ortho' # 投影方式设置为正投影——类似地球仪 CHN = r'C:\Users\Administrator\Desktop\MyProject\Test_module\Test_matplotlib\data_base' m.readshapefile(CHN + r'\gadm36_CHN_shp\gadm36_CHN_1', 'states', drawbounds=True) # 加省界 # 读取数据 df = pd.read_csv('./data_base/data/chnpop.csv') df['省名'] = df.省名.str[:2] df.set_index('省名', inplace=True) # 把每个省的数据映射到colormap上 statenames = [] colors = {} patches = [] cmap = plt.cm.YlOrRd # 国旗色红黄色调 vmax = 10**8 vmin = 3 * 10**6 # 处理地图包里的省名 for shapedict in m.states_info: statename = shapedict['NL_NAME_1'] p = statename.split('|') if len(p) > 1: s = p[1] else: s = p[0] s = s[:2] if s == '黑龍': s = '黑龙' statenames.append(s) pop = df['人口数'][s] colors[s] = cmap(np.sqrt( (pop - vmin) / (vmax - vmin)))[:3] # 根据归一化后的人口数映射颜色 # exit() ax = plt.gca() for nshape, seg in enumerate(m.states): color = rgb2hex(colors[statenames[nshape]]) poly = Polygon(seg, facecolor=color, edgecolor=color) patches.append(poly) ax.add_patch(poly) # 图片绘制加上台湾(台湾不可或缺) m.readshapefile(CHN + '\gadm36_TWN_shp\gadm36_TWN_1', 'taiwan', drawbounds=True) for nshape, seg in enumerate(m.taiwan): poly = Polygon(seg, facecolor='w') patches.append(poly) ax.add_patch(poly) # 添加colorbar 渐变色legend colors1 = [i[1] for i in colors.values()] colorVotes = plt.cm.YlOrRd p = PatchCollection(patches, cmap=colorVotes) p.set_array(np.array(colors1)) pylab.colorbar(p) #4266831.094478747, 1662846.3046657 lon = 4097273.638675578 lat = 4008859.232616643 x, y = m(lon, lat) plt.text(x, y, '国都', fontsize=120, fontweight='bold', ha='left', va='bottom', color='k') m.scatter(x, y, s=200, marker='*', facecolors='r', edgecolors='B') # 绘制首都 plt.title(u'祝鑫泉画的World Map ', fontsize=24) plt.savefig("./data_base/result/Chinese_Map1.png", dpi=100) # 指定分辨率 plt.show()
ax = plt.gca() for nshape, seg in enumerate(m.states): poly = Polygon(seg, facecolor='r') ax.add_patch(poly) # 画上台湾省 m.readshapefile(CHN + '\gadm36_TWN_shp\gadm36_TWN_1', 'taiwan', drawbounds=True) for nshape, seg in enumerate(m.taiwan): poly = Polygon(seg, facecolor='r') ax.add_patch(poly) for shapedict in m.states_info: statename = shapedict['NL_NAME_1'] p = statename.split('|') if len(p) > 1: s = p[1] else: s = p[0] print(s) # for shapedict in m.taiwan_info: # # s = shapedict['NAME_CHINE'] # print(s) plt.title(r'$祝鑫泉画的World\ Map$', fontsize=24) plt.show()