import numpy as np
import matplotlib.pyplot as plt x_data = np.random.randn(10)
print(x_data)
y_data = x_data * 0.3 + 0.15
print(y_data) plt.plot(x_data,y_data)
plt.scatter(x_data,y_data,c="r")
plt.show()
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt x_data = np.random.randn(10)
y_data = x_data * 0.3 + 0.15 weight = tf.Variable(0.5)
bias = tf.Variable(0.0)
y_model = weight * x_data + bias loss = tf.pow((y_model - y_data),2)
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss) sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init) for _ in range(200):
sess.run(train_op)
print(weight.eval(sess),bias.eval(sess)) plt.plot(x_data, y_data, 'ro', label='Original data')
plt.plot(x_data, sess.run(weight) * x_data + sess.run(bias), label='Fitted line')
plt.legend()
plt.show()
.........................
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt x_data = np.random.randn(10)#.astype(np.float32)
y_data = x_data * 0.3 + 0.1 weight = tf.Variable(0.5)
bias = tf.Variable(0.0)
x_ = tf.placeholder(tf.float32)
y_ = tf.placeholder(tf.float32)
y_model = weight * x_ + bias loss = tf.pow((y_model - y_),2)
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss) sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init) for _ in range(5):
for (x,y) in zip(x_data,y_data):
sess.run(train_op,feed_dict={x_:x,y_:y})
print("weighe: " ,weight.eval(sess)," | bias: ",bias.eval(sess)) plt.plot(x_data, y_data, 'ro', label='Original data')
plt.plot(x_data, sess.run(weight) * (x_data) + sess.run(bias), label='Fitted line')
plt.legend()
plt.show()
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt threshold = 1.0e-2
x_data = np.random.randn(10).astype(np.float32)
y_data = x_data * 3 + 1 weight = tf.Variable(1.)
bias = tf.Variable(1.)
x_ = tf.placeholder(tf.float32)
y_ = tf.placeholder(tf.float32)
y_model = tf.add(tf.multiply(x_, weight), bias) loss = tf.reduce_mean(tf.pow((y_model - y_),2))
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss) sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)
flag = 1
while(flag):
for (x,y) in zip(x_data,y_data):
sess.run(train_op,feed_dict={x_:x,y_:y})
print(weight.eval(sess), bias.eval(sess))
if(sess.run(loss,feed_dict={x_:x_data,y_:y_data}) <= threshold):
flag = 0 plt.plot(x_data, y_data, 'ro', label='Original data')
plt.plot(x_data, sess.run(weight) * (x_data) + sess.run(bias), label='Fitted line')
plt.legend()
plt.show()
......................................................
#coding:utf-8 import numpy as np a = np.array([1,2,3])
b = np.array([7,8])
res = np.meshgrid(a,b)
for i in res:
print(i)
import numpy as np
import matplotlib.pyplot as plt x = np.array([[0, 1, 2], [0, 1, 2]])
y = np.array([[0, 0, 0], [1, 1, 1]]) plt.plot(x, y,color='red',marker='.', linestyle='--')
plt.grid(True)
plt.show()
plt.plot(x, y, marker='.',markersize=10,linestyle='-.')
plt.grid(True)
plt.show()
import numpy as np
import matplotlib.pyplot as plt x = np.array([[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3]])
y = np.array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]]) plt.plot(x, y,marker='.', markersize=10,linestyle='-.')
plt.grid(True)
plt.show()
import numpy as np
import matplotlib.pyplot as plt x = np.array([0, 1, 2])
y = np.array([0, 1])
X, Y = np.meshgrid(x, y)
print(X)
print(Y) plt.plot(X, Y,color='red', marker='.', linestyle='')
plt.grid(True)
plt.show()
import numpy as np
import matplotlib.pyplot as plt x = np.linspace(0,1000,20)
y = np.linspace(0,500,20) X,Y = np.meshgrid(x, y) plt.plot(X, Y,color='limegreen', marker='.',linestyle='')
plt.grid(True)
plt.show()
import numpy as np
import matplotlib.pyplot as plt from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import LinearLocator, FormatStrFormatter fig = plt.figure(figsize=(16,12))
ax = fig.gca(projection="3d") #准备数据
x = np.arange(-5, 5, 0.25) #生成[-5,5] 间隔0.25的数列,间隔越小,曲面越平滑
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x,y) #格点矩阵,原来的x行向量向下复制len(y)此形成
r = np.sqrt(x ** 2 + y ** 2)
z = np.sin(r) surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm) #cmap指color map #自定义z轴
ax.set_zlim(-1, 1)
ax.zaxis.set_major_locator(LinearLocator(20)) #z轴网格线的疏密,刻度的疏密,20表示刻度的个数
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) #将z的value子符串转为float,保留2位小数 #设置坐标轴的label和标题
ax.set_xlabel('x', size=15)
ax.set_ylabel('y', size=15)
ax.set_zlabel('z', size=15)
ax.set_title("Surface plot", weight='bold', size=20) #添加右侧的色卡条
fig.colorbar(surf, shrink=0.6, aspect=8) #shrink表示整体收缩比例,aspect仅对bar的宽度有影响,
# aspect值越大,bar越窄 plt.show()
import numpy as np
import matplotlib.pyplot as plt n=1024
X=np.random.normal(0,1,n)
Y=np.random.normal(0,1,n)
T=np.arctan2(Y,X)#返回给定的 X 及 Y 坐标值的反正切值。 plt.scatter(X,Y,s=75,c=T,alpha=0.5) plt.xlim(-1.5,1.5)
plt.xticks(())
plt.ylim(-1.5,1.5)
plt.yticks(()) plt.show()
import matplotlib.pyplot as plt
import numpy as np n=12
X=np.arange(n)
Y1=(1 - X / float(n)) * np.random.uniform(0.5,1.0,n)
Y2=(1 - X / float(n)) * np.random.uniform(0.5,1.0,n) plt.bar(X,+Y1,facecolor='#9999ff',edgecolor='white')
plt.bar(X,-Y2,facecolor='#ff9999',edgecolor='white') for x,y in zip(X,Y1):
plt.text(x+0.4,y+0.05,'%.2f' % y,ha='center',va='bottom') for x,y in zip(X,Y2):
plt.text(x+0.4,-y-0.05,'%.2f' % y,ha='center',va='bottom') plt.xlim(-1,n)
plt.xticks(())
plt.ylim(-1.25,1.25)
plt.yticks(()) plt.show()
import numpy as np
import matplotlib.pyplot as plt def f(x,y):
return (1 - x/2 + x**5 + y**3) * np.exp(-x**2,-y**2) n=256
x=np.linspace(-3,3,n)
y=np.linspace(-3,3,n)
X,Y=np.meshgrid(x,y) plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot)
C=plt.contour(X,Y,f(X,Y),8,colors='black',linewidth=0.5) plt.clabel(C,inline=True,fontsize=10)
plt.xticks(())
plt.yticks(())
plt.show()
import numpy as np
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig=plt.figure()
ax=Axes3D(fig) X=np.arange(-4,4,0.25)
Y=np.arange(-4,4,0.25)
X,Y=np.meshgrid(X,Y)
R=np.sqrt(X ** 2 + Y ** 2)
Z=np.sin(R) ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))
ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap=plt.get_cmap('rainbow')) plt.show()
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D threshold = 1.0e-2
x1_data = np.random.randn(100).astype(np.float32)
x2_data = np.random.randn(100).astype(np.float32)
y_data = x1_data * 2 + x2_data * 3 + 1.5 weight1 = tf.Variable(1.)
weight2 = tf.Variable(1.)
bias = tf.Variable(1.)
x1_ = tf.placeholder(tf.float32)
x2_ = tf.placeholder(tf.float32)
y_ = tf.placeholder(tf.float32) y_model = tf.add(tf.add(tf.multiply(x1_, weight1), tf.multiply(x2_, weight2)),bias)
loss = tf.reduce_mean(tf.pow((y_model - y_),2)) train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss) sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)
flag = 1
while(flag):
for (x,y) in zip(zip(x1_data, x2_data),y_data):
sess.run(train_op, feed_dict={x1_:x[0],x2_:x[1], y_:y})
if sess.run(loss, feed_dict={x1_:x[0],x2_:x[1], y_:y}) <= threshold:
flag = 0 fig = plt.figure()
ax = Axes3D(fig)
X, Y = np.meshgrid(x1_data, x2_data)
Z = sess.run(weight1) * (X) + sess.run(weight2) * (Y) + sess.run(bias)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.hot)
ax.contourf(X, Y, Z, zdir='z', offset=-1, cmap=plt.cm.hot)
ax.set_zlim(-1, 1)
plt.show()