<jupyter_start><jupyter_text>数组创建<jupyter_code>np.array([1,4,5,2,3],dtype = 'float_')
np.array([1,2,5,3,4])
np.array([range(i,i+3) for i in range(3)])
np.zeros(10,dtype=float)
np.ones((3,4))
np.full((3,4),fill_value=3.14)#3*4矩阵，均为3.14
np.arange(10,20,2)# [10,20)区间步长为2
np.linspace(0,1,5)# [0,1]区间5个数
np.random.normal(0,1,(3,3))# 0,1正态分布
np.random.randint(0,10,(3,3))# [0,10)区间的随机整数
np.eye(3) # 3*3单位矩阵
np.empty(3) # 1*3未初始化数组<jupyter_output><empty_output><jupyter_text>数组基础  数组属性<jupyter_code>np.random.seed(43)
ndim
shape
size
dtype
nbytes
itemsize<jupyter_output><empty_output><jupyter_text>数组索引:单个元素<jupyter_code>#1维
x1 = np.array([1,23,4,5,7])
x1[0]
x1[-1]
# 多维
x2 = np.array(np.random.randint(0,10,(3,4)))
x2[0,0]
x2[-1,0]
x2[1,1]=12
x2[1,1]=3.14 ##截断!!<jupyter_output><empty_output><jupyter_text>数组切片：子数组<jupyter_code>x1[:5]
x1[5:]
x1[4:7]
x1[::2]
x1[1::2]
x1[::-1]
x2[:2,:3]
x2[::2,::3]
x2[::-1,::-1]
x2[:,0]
x2[0,:]
x2[0] #第0行！
x2_sub = x2[:2,:3]
x2_sub
# 非副本
x2_sub[0,0]=39
x2
# 创建副本
x2_sub_copy = x2[:2,:3].copy()<jupyter_output><empty_output><jupyter_text>数组变形<jupyter_code>grid = np.arange(1,10).reshape((3,3))
grid
x = np.array([1,2,3,4])
x.reshape((1,4))
x[np.newaxis,:]
x[:,np.newaxis]<jupyter_output><empty_output><jupyter_text>数组拼接分裂<jupyter_code>x = np.array([10,20,30])
y = np.array([40,50,60])
grid = np.arange(1,10).reshape((3,3))
np.concatenate((x,y),axis=0)
np.vstack((x,y,grid))
np.hstack((x,y,x))
A = np.random.randint(0,10,(3,4))
A
df = pd.DataFrame(A,columns=list('QRST'))
df
df.loc[:,'R'].shape
df.subtract(df['R'],axis = 'index')<jupyter_output><empty_output><jupyter_text>手动绘制直方图  关注np.searchsorted,np.add.at绘制方法<jupyter_code>x=np.random.randn(100)
bins = np.linspace(-5,5,20)
counts = np.zeros_like(bins)
counts
i=np.searchsorted(bins,x)
np.add.at(counts,i,1)
plt.plot(bins,counts,linestyle = 'steps')
plt.hist(x)<jupyter_output><empty_output><jupyter_text>排序<jupyter_code>x=np.array([2,1,4,3,5])
np.sort(x)
x.sort()
print(x)
x= np.array([2,1,4,3,5])
np.argsort(x)
x[np.argsort(x)]
rand=np.random.RandomState(42)
X=rand.randint(0,10,(4,6))
X
np.sort(X,axis = 0) ##列排序
np.sort(X,axis = 1)
x = np.array([7,2,3,1,6,5,4])
np.partition(x,3) ## 将前3个最小的值放在前3位
np.partition(X,2,axis = 1)
X = rand.rand(10,2)
X
plt.scatter(X[:,0],X[:,1],s=100)
## 计算上图各点的距离
dist_seq = np.sum((X[:,None,:]-X[None,:,:])**2,axis = -1)
X[:,None,:]
X[None,:,:]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.25774058577405856, 'fraction_numerical': 0.09497907949790795, 'mean_word_length': 8.921161825726141, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '470713', 'license': 'MIT', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1430, 'n_words': 129, 'path': 'Python Data Science Handbook-2 Numpy.ipynb', 'repo_name': '123385918/Python-Science-Handbook', 'size': '2390'}