import numpy as np
data = np.random.rand(10,2)
print(data)
初始数据
[[0.85482143 0.87208392]
[0.15226363 0.52549581]
[0.85199583 0.63840434]
[0.37461595 0.7699527 ]
[0.62885774 0.50949107]
[0.13442721 0.33380331]
[0.72998008 0.01574827]
[0.20302876 0.19834324]
[0.78084008 0.34859342]
[0.1350378 0.55653729]]
python行索引【0,1,2,3】行
print(data[[0,1,2,3]])
[[0.85733026 0.51441212]
[0.9297203 0.32537354]
[0.55053671 0.98026599]
[0.01811309 0.78720568]]
python列索引【0】列
print(data[:,0])
[0.85733026 0.9297203 0.55053671 0.01811309 0.72665786 0.51680252
0.89344144 0.80729948 0.05969606 0.71608088]
import numpy as np
import matplotlib.pyplot as plt
data = np.random.rand(10000,2) #随机生成10000行两列个数据
x = data[:,0]
y = data[:,1]
idx = x**2+y**2 < 1 #索引条件圆形中x和y都大于0的数据
plt.plot(x[idx],y[idx],'go',markersize=1)
plt.show()
效果
import numpy as np
import matplotlib.pyplot as plt
data = 2*np.random.rand(10000,2) - 1 #定义域转为[-1,1]
x = data[:,0]
y = data[:,1]
idx = x**2+y**2 < 1 #圆形
plt.plot(x[idx],y[idx],'go',markersize=1)
plt.show()
效果
import numpy as np
import matplotlib.pyplot as plt
data = 2*np.random.rand(10000,2) - 1
x = data[:,0]
y = data[:,1]
idx = x**2+y**2 < 1
hole = x**2 + y**2 < 0.25
idx = np.logical_and(idx,~hole) #numpy中的逻辑与,大圆和不包含小圆区域取相同的部分
plt.plot(x[idx],y[idx],'go',markersize=1)
plt.show()
效果
import numpy as np import matplotlib.pyplot as plt p = np.random.rand(10000) np.set_printoptions(edgeitems=5000, suppress=True) plt.hist(p,bins=20,color='g') #分成20份 plt.show()
效果
import numpy as np import matplotlib.pyplot as plt p = np.random.rand(10000) np.set_printoptions(edgeitems=5000, suppress=True) plt.hist(p,bins=20, color='g', edgecolor='k') plt.show()
import numpy as np
import matplotlib.pyplot as plt
N = 10000
times = 100
z = np.zeros(N)
for i in range(times):
z += np.random.rand(N)
z /= times
plt.hist(z, bins = 20, color = 'm', edgecolor='k')
plt.show()
效果
import numpy as np
import pandas as pd
d = np.random.rand(3,4)
print(d)
print(type(d))
data = pd.DataFrame(data=d,columns = list('abcd'))
print('='*50)
print(data)
print(type(data))
print(data['b'])
data.to_csv('./data.dsv', index=False, header=True)
print('文件保存成功')
效果
import numpy as np
import matplotlib.pyplot as plt
# [-4,2]
d = np.random.rand(100)*6-4
print(d)
plt.plot(d,'r.')
plt.show()
效果
import numpy as np
if __name__=='__main__':
a = np.arange(1,10000)
print(a)
print(np.sqrt(6*np.sum(1/(a**2))))
效果
[ 1 2 3 ... 9997 9998 9999]
3.141497154397623
import numpy as np
if __name__=='__main__':
x = np.arange(1,20)
print(np.sum(1/x.cumprod()) + 1) #cumprod求阶乘
效果
2.7182818351251554
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0,1,100) print(x) y = x**x plt.plot(x,y,'r-',linewidth=2) plt.show()
效果
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0,1,100) print(x) y = x**x plt.plot(x,y,'r--',linewidth=2) plt.show()
效果
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0,1,100) print(x) y = x**x plt.plot(x,y,'r:',linewidth=2) plt.show()
效果
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0,1,100) print(x) y = x**x plt.plot(x,y,'r.',linewidth=2) plt.show()