numpy生成随机数据实例

一、总结

一句话总结:

A、用np的随机数函数:有正有负:np.random.randn(3,3)   #三行三列正态分布随机数据
B、y3 = 0.89*x + 1.47 + 0.2*np.random.randn(100):注意x有多少维,随机数就应该是多少,比如x是100维,随机数也是100维

 

 

1、生成 y = 0.89x + 1.47 的随机数据?

x = np.linspace(-1,1,100)
y3 = 0.89*x + 1.47 + 0.2*np.random.randn(100) # x是n为数据,随机数也整成n维数据,这样方便一一相加

 

 

2、如下代码生成y = 0.89x + 1.47随机数据的问题?

|||-begin



x = np.linspace(-1,1,100)
y3 = 0.89*x + 1.47 + 0.2*np.random.randn(1)


|||-end

导致每个y加的随机数一样,没达到随机数的目的,可以变成y3 = 0.89*x + 1.47 + 0.2*np.random.randn(100)

 

 

二、numpy生成随机数据实例

博客对应课程的视频位置:

 







import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-1,1,100)
# print(x)




In [15]:





# 设置散点图y数据
y = []
for i in range(len(x)):
yy = 0.89*x[i] + 1.47 + 0.2*np.random.randn(1)
# print(yy)
y.append(yy)
# print(np.array(y))

# 这样做太笨了

# 这里不行,因为10*np.random.randn(1) 固定了,导致每一个x都加了一样的10*np.random.randn(1)




In [45]:





y3 = 0.89*x + 1.47 + 0.2*np.random.randn(100)
y3





Out[45]:



array([0.43289657, 0.58964628, 0.81537881, 0.71828526, 0.60568243,
0.692671 , 0.42955551, 0.85385516, 0.5424596 , 0.94033892,
0.55702877, 0.93413128, 0.82224491, 0.69749114, 0.83552763,
0.73568433, 1.07242776, 0.96049974, 0.92909749, 1.35320683,
1.18608774, 0.79819867, 1.02294792, 1.09733601, 0.85564769,
0.93066108, 1.05048703, 1.65818226, 1.20421378, 1.02459084,
1.32057286, 1.11948688, 0.97158956, 1.53498399, 1.37696365,
1.44499342, 0.95154101, 1.20488183, 1.27275965, 1.52940271,
1.00922309, 1.14251886, 1.57187289, 1.33537043, 1.01433779,
1.0378432 , 1.26895863, 1.00158081, 1.51106763, 1.45484477,
1.40721077, 1.46390839, 1.29558836, 1.33962227, 1.69144454,
1.58420512, 1.44741885, 1.83872615, 1.2939931 , 1.80151958,
1.5272652 , 1.57054056, 1.49705515, 1.50977317, 1.80238832,
1.55590632, 1.78640755, 1.71394312, 1.95401635, 2.02022234,
1.78535058, 1.9906076 , 1.56922585, 1.86471587, 2.06477333,
2.09956185, 1.86451571, 2.05912673, 1.82340342, 2.26899071,
2.22846647, 2.30529275, 1.97947389, 2.30924186, 1.9573911 ,
2.18977128, 2.08008324, 2.00659632, 1.88401575, 2.10918123,
2.43509661, 2.24262501, 2.35875651, 2.30461968, 2.27090374,
2.39677873, 2.27070173, 2.12591642, 2.33550721, 2.39941424])




In [2]:





y = 0.89*x + 1.47 + 0.2*np.random.randn(100)
y





Out[2]:



array([0.43203256, 0.68600621, 0.79649154, 0.82654637, 0.88750752,
0.77880127, 0.5180807 , 0.38787904, 0.68045607, 0.85127519,
0.91369289, 0.93253317, 0.56517652, 0.88590304, 0.87071518,
0.91701613, 1.02918769, 0.9583387 , 0.74621968, 0.93486923,
1.12963601, 0.76325245, 0.79474722, 0.85073618, 1.02708539,
0.96432465, 1.0094568 , 1.25483073, 0.92819118, 1.18273894,
1.13000989, 1.04694783, 1.15123563, 1.24793398, 1.45207923,
1.27339872, 1.46827757, 1.40333015, 1.44885455, 1.50985441,
1.13544908, 1.36010293, 1.45491916, 1.58749374, 1.1458724 ,
1.42366604, 1.38963717, 1.35006035, 1.68065633, 1.29657017,
1.33734056, 1.57125061, 1.52326738, 1.62895124, 1.64303349,
1.27864606, 1.85576483, 1.3549945 , 1.54020721, 1.86783425,
1.77329294, 1.84536986, 1.24152219, 1.53826381, 1.69798321,
1.87518796, 1.67740074, 2.02599999, 1.78161923, 2.13244278,
1.72025926, 1.96094538, 1.90781702, 1.46633179, 1.67728002,
2.01824635, 2.13588947, 2.09084339, 2.03811436, 1.869235 ,
1.61881703, 1.99257175, 1.83019278, 2.02999904, 2.05118707,
2.05373976, 2.11910644, 2.08584724, 2.3587689 , 2.07748842,
2.44923472, 2.1921131 , 2.09460362, 2.29626203, 2.07462724,
2.19450823, 2.04539763, 2.29703966, 2.60031955, 2.48617413])




In [11]:





# for i in range(100):
# print(np.random.randn(1))




In [3]:





# 设置直线数据
y2 = 0.89*x + 1.47




In [4]:





plt.scatter(x,y)

# yy = 0.89*x[i] + 1.47
plt.plot(x,y2,c='r')

plt.show()








In [5]:





# 随机生成直线的x和y
random_w = np.random.randn(120)
random_b = np.random.randn(120)
#print(random_x)
#print(random_y)




In [7]:





# 让直线变动,动态生成图
from matplotlib import animation
from matplotlib import pylab
%pylab

fig,ax=plt.subplots()
# print(fig,ax)

line,=ax.plot(x,y2)

def animate(i):
# line.set_ydata((0.89+i)*x + 1.47)
# line.set_ydata((0.89+0.5*i)*x + 1.47+0.3*i)
line.set_ydata((random_w[i])*x + random_b[i])
# print(i)
return line,

def init():
line.set_ydata(y2)
return line,

ani=animation.FuncAnimation(fig=fig,func=animate,frames=100,init_func=init,interval=200,blit=False)
ani.save('test.gif', writer='imagemagick', fps=30)
plt.show()






Using matplotlib backend: Qt5Agg
Populating the interactive namespace from numpy and matplotlib




 



 


In [ ]: