<jupyter_start><jupyter_text>ndarray 对象的内部原理<jupyter_code>import numpy as np
np.ones((10, 5)).shape
np.ones((3, 4, 5), dtype=np.float64).strides     #跨度元组
ints = np.ones(10, dtype=np.uint32)
ints
floats = np.ones(10, dtype=np.float32)
floats
np.issubdtype(ints.dtype, np.integer)
np.issubdtype(floats.dtype, np.floating)
np.float64.mro()       #查看所有超类<jupyter_output><empty_output><jupyter_text>数组重塑<jupyter_code>arr = np.arange(8)
arr
arr.reshape((4,2))
arr.reshape((4, 2)).reshape((2, 4))
arr = np.arange(15)
arr
arr.reshape((3, -1))
arr = arr.reshape((3, -1))
arr
# 扁平化
arr = arr. ravel()
arr
# flatten 永远返回数据的副本
arr = np.arange(15).reshape((5, 3))
arr = arr.flatten()
arr<jupyter_output><empty_output><jupyter_text>C 和 Fortan 顺序<jupyter_code>arr = np.arange(12).reshape((3, 4))
arr = arr.ravel('F')    # Fortan 序 ， 按列排
arr
arr = np.arange(12).reshape((3, 4))
arr
arr = arr.ravel('C')
arr<jupyter_output><empty_output><jupyter_text>数组的合并与拆分<jupyter_code>arr1 = np.array([[1,2,3], [4,5,6]])
arr2 = np.array([[7,8,9],[10,11,12]])
np.concatenate([arr1, arr2], axis=0)
np.concatenate([arr1, arr2], axis=1)
# 连接操作还有 vstack 和  hstack 
np.vstack((arr1, arr2))
np.hstack((arr1, arr2))
arr = np.random.rand(5,2)
arr
fir, sec, thi = np.split(arr, [1,3])   # 1 included,  3 excluded
fir
sec
thi<jupyter_output><empty_output><jupyter_text>数组的重复操作<jupyter_code>arr = np.arange(3)
arr.repeat(3)
arr2 = np.arange(4)
arr2.repeat([1,2,3,4])
from numpy.random import randn
arr = randn(2, 2)
arr
arr.repeat(2, axis=1)
arr.repeat(2, axis=0)
np.tile(arr, 2)
arr
np.tile(arr, (2,1))
arr = np.arange(10) * 100
idxs = [7,1,2,6]
arr[idxs]    # sugar
arr
arr.take(idxs)    # 获取
arr.put(idxs, 360)   # 设置
arr
arr = randn(2, 4)
arr
idxs = [2,0,2,1]
arr.take(idxs, axis=1)<jupyter_output><empty_output><jupyter_text>广播<jupyter_code>arr = np.arange(5)
arr
arr * 4
arr = randn(4,3)
arr
arr.mean(0)      # 参数 axis = 0 指定 投影到 x 轴上那一列的平均数
demeaned = arr - arr.mean(0)
demeaned
arr.mean(1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18973326623049824, 'fraction_numerical': 0.06089582284851535, 'mean_word_length': 5.561056105610561, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '520645', 'license': 'MIT', 'n_tokens_mistral': 1049, 'n_tokens_neox': 1024, 'n_words': 169, 'path': 'pydata-learning/ch12.ipynb', 'repo_name': 'JanGin/pydata-book-learning', 'size': '1987'}