Skip to content

Instantly share code, notes, and snippets.

@Albert-W
Created February 21, 2020 20:51
Show Gist options
  • Save Albert-W/c9ed3e281cf959612a06c937cd5a0570 to your computer and use it in GitHub Desktop.
Save Albert-W/c9ed3e281cf959612a06c937cd5a0570 to your computer and use it in GitHub Desktop.
Numpy/numpy101noanswer.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# 1 引入numpy并查看版本\n```\nout:1.17.3\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import numpy as np",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.__version__",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "'1.18.1'"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 2 创建一位数组,0-9\n```\nout:array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.arange(10)",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 3 创建boolean数组(3x3全部为True)\n```\nout:array([[ True, True, True],\n [ True, True, True],\n [ True, True, True]])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.full((3,3),True)",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "array([[ True, True, True],\n [ True, True, True],\n [ True, True, True]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 4 提取满足条件的元素(提取奇数项)\n```\nin: arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\nout: array([1, 3, 5, 7, 9])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])",
"execution_count": 8,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr[1::2]",
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "array([1, 3, 5, 7, 9])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr2 = arr*2\narr2",
"execution_count": 32,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 32,
"data": {
"text/plain": "array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr[arr2%2==1] #通过元素计算,而非下标",
"execution_count": 34,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 34,
"data": {
"text/plain": "array([], dtype=int64)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 5 替换满足条件的元素(替换奇数项为-1)\n```\nin: arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\nout:array([0, -1, 2, -1, 4, -1, 6, -1, 8, -1])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])",
"execution_count": 39,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr[1::2]= -1",
"execution_count": 40,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr",
"execution_count": 41,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 41,
"data": {
"text/plain": "array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "index = np.arange(len(arr)) # 通过index计算\narr[index%2 ==1] =-1\narr",
"execution_count": 42,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 42,
"data": {
"text/plain": "array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 6 与5相同,但是不要在原数组上改动,返回个新的数组"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])",
"execution_count": 35,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sub = arr[:]\nsub[1::2]= -1\nsub",
"execution_count": 36,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 36,
"data": {
"text/plain": "array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "index = np.arange(len(arr)) # 通过index计算\nres = np.where(index%2==1,-1,arr) # 新函数!!\nres",
"execution_count": 43,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 43,
"data": {
"text/plain": "array([ 0, -1, 2, -1, 4, -1, 6, -1, 8, -1])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 7 reshape一个数组(0-9这个一维数组->2x5的二维数组)\n```\nin: arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\nout: array([[0, 1, 2, 3, 4],\n [5, 6, 7, 8, 9]])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])",
"execution_count": 18,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr.reshape(2,-1)",
"execution_count": 20,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 20,
"data": {
"text/plain": "array([[0, 1, 2, 3, 4],\n [5, 6, 7, 8, 9]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 8 垂直拼接俩数组\n```\nin: a = np.arange(10).reshape(2,-1)\n b = np.repeat(1, 10).reshape(2,-1)\nout: array([[0, 1, 2, 3, 4],\n [5, 6, 7, 8, 9],\n [1, 1, 1, 1, 1],\n [1, 1, 1, 1, 1]])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.arange(10).reshape(2,-1)\nb = np.repeat(1, 10).reshape(2,-1) # 新函数",
"execution_count": 21,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.vstack([a,b])",
"execution_count": 22,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 22,
"data": {
"text/plain": "array([[0, 1, 2, 3, 4],\n [5, 6, 7, 8, 9],\n [1, 1, 1, 1, 1],\n [1, 1, 1, 1, 1]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 9 水平拼接俩数组\n```\nin: a = np.arange(10).reshape(2,-1)\n b = np.repeat(1, 10).reshape(2,-1)\nout: array([[0, 1, 2, 3, 4, 1, 1, 1, 1, 1],\n [5, 6, 7, 8, 9, 1, 1, 1, 1, 1]])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.arange(10).reshape(2,-1)\nb = np.repeat(1, 10).reshape(2,-1)",
"execution_count": 23,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.hstack([a,b])",
"execution_count": 24,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 24,
"data": {
"text/plain": "array([[0, 1, 2, 3, 4, 1, 1, 1, 1, 1],\n [5, 6, 7, 8, 9, 1, 1, 1, 1, 1]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 10 生成自定义的数组\n```\nin: a = np.array([1,2,3])\nout: array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([1,2,3])",
"execution_count": 44,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.hstack([np.repeat(a,3) ,a,a,a])",
"execution_count": 45,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 45,
"data": {
"text/plain": "array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 11 求两数组交集\n```\nin: a = np.array([1,2,3,2,3,4,3,4,5,6])\n b = np.array([7,2,10,2,7,4,9,4,9,8])\nout:array([2, 4])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([1,2,3,2,3,4,3,4,5,6])\nb = np.array([7,2,10,2,7,4,9,4,9,8])",
"execution_count": 46,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.intersect1d(a,b)",
"execution_count": 47,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 47,
"data": {
"text/plain": "array([2, 4])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 12 从一个数组中删除另一个数组中含有的元素\n```\nin: a = np.array([1,2,3,4,5])\n b = np.array([5,6,7,8,9])\nout:array([1,2,3,4])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([1,2,3,4,5])\nb = np.array([5,6,7,8,9])",
"execution_count": 48,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.setdiff1d(a,b)",
"execution_count": 50,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 50,
"data": {
"text/plain": "array([1, 2, 3, 4])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 13 两等长数组,找出位置相同且值也相同的下标\n```\nin: a = np.array([1,2,3,2,3,4,3,4,5,6])\n b = np.array([7,2,10,2,7,4,9,4,9,8])\nout:(array([1, 3, 5, 7]),)\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([1,2,3,2,3,4,3,4,5,6])\nb = np.array([7,2,10,2,7,4,9,4,9,8])",
"execution_count": 52,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.where(a == b)",
"execution_count": 53,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 53,
"data": {
"text/plain": "(array([1, 3, 5, 7]),)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 14 提取给定范围的数据(提取值在5-10之间的元素)\n```\nin: a = np.array([2, 6, 1, 9, 10, 3, 27])\nout:(array([6, 9, 10]),)\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([2, 6, 1, 9, 10, 3, 27])",
"execution_count": 54,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a[(a >=5 ) & (a <=10)]",
"execution_count": 55,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 55,
"data": {
"text/plain": "array([ 6, 9, 10])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 15 取a,b两等长数组中较大值为新数组\n```\nin: a = [4,6,1,7,9]\nb=[1,6,9,2,7]\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = [4,6,1,7,9]\nb=[1,6,9,2,7]",
"execution_count": 59,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.max(np.vstack([a,b]),axis=0)",
"execution_count": 62,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 62,
"data": {
"text/plain": "array([4, 6, 9, 7, 9])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 16 如何交换二维数组两列(交换3x3数组的第0和第1列)\n```\nin:arr = np.arange(9).reshape(3,3)\nout:array([[1, 0, 2],\n [4, 3, 5],\n [7, 6, 8]])\n ```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr = np.arange(9).reshape(3,3)",
"execution_count": 64,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr[:,[1,0,2]]",
"execution_count": 65,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 65,
"data": {
"text/plain": "array([[1, 0, 2],\n [4, 3, 5],\n [7, 6, 8]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 17 如何交换二维数组两行(交换3x3数组第0和第1行)\n```\nin:arr = np.arange(9).reshape(3,3)\nout:array([[3, 4, 5],\n [0, 1, 2],\n [6, 7, 8]])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr =np.arange(9).reshape(3,3)",
"execution_count": 66,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr[[1,0,2],:]",
"execution_count": 67,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 67,
"data": {
"text/plain": "array([[3, 4, 5],\n [0, 1, 2],\n [6, 7, 8]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 18 如何反转二维数组的行\n```\nin:arr = np.arange(9).reshape(3,3)\nout:array([[6, 7, 8],\n [3, 4, 5],\n [0, 1, 2]])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr =np.arange(9).reshape(3,3)",
"execution_count": 68,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr[[2,1,0],:]",
"execution_count": 69,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 69,
"data": {
"text/plain": "array([[6, 7, 8],\n [3, 4, 5],\n [0, 1, 2]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 19 如何反转二维数组的列\n```\nin:arr = np.arange(9).reshape(3,3)\nout:array([[2, 1, 0],\n [5, 4, 3],\n [8, 7, 6]])\n ```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr =np.arange(9).reshape(3,3)",
"execution_count": 70,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr[:,[2,1,0]]",
"execution_count": 71,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 71,
"data": {
"text/plain": "array([[2, 1, 0],\n [5, 4, 3],\n [8, 7, 6]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 20 创建5x3的float二维数组,值是5-10的随机数"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.random.randint(5,11,15).reshape(5,3)",
"execution_count": 72,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 72,
"data": {
"text/plain": "array([[ 6, 8, 9],\n [10, 10, 7],\n [ 7, 9, 7],\n [ 5, 9, 7],\n [ 5, 8, 7]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.random.uniform(5,10,(5,3))",
"execution_count": 73,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 73,
"data": {
"text/plain": "array([[8.36189438, 8.36356932, 7.62779084],\n [7.52979788, 6.49947576, 5.77024321],\n [6.942666 , 6.78994417, 8.24682838],\n [6.58191915, 9.76396628, 6.44756139],\n [7.10661326, 9.74921338, 8.43203359]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 21 [打印设置相关]float数组每个元素保留小数点后3位打印(只是打印时,本质不变)\n```\nin:rand_arr = np.random.random((5,3))\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "rand_arr = np.random.random((5,3))",
"execution_count": 74,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.set_printoptions(precision=3)\nrand_arr",
"execution_count": 75,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 75,
"data": {
"text/plain": "array([[0.019, 0.926, 0.049],\n [0.544, 0.206, 0.991],\n [0.438, 0.949, 0.733],\n [0.985, 0.071, 0.596],\n [0.557, 0.103, 0.769]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 22 [打印设置相关]避免科学计数法打印数组\n```\nin:np.random.seed(100)\nrand_arr = np.random.random([3,3])/1e3\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.random.seed(100)\nrand_arr = np.random.random([3,3])/1e3",
"execution_count": 76,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "rand_arr",
"execution_count": 77,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 77,
"data": {
"text/plain": "array([[5.434e-04, 2.784e-04, 4.245e-04],\n [8.448e-04, 4.719e-06, 1.216e-04],\n [6.707e-04, 8.259e-04, 1.367e-04]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.set_printoptions(suppress=True, precision=6)\nrand_arr",
"execution_count": 78,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 78,
"data": {
"text/plain": "array([[0.000543, 0.000278, 0.000425],\n [0.000845, 0.000005, 0.000122],\n [0.000671, 0.000826, 0.000137]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 23 [打印设置相关]如何限制数组打印条数\n```\nin:a = np.arange(15)\nout: array([ 0, 1, 2, ..., 12, 13, 14])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.arange(15)",
"execution_count": 79,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.set_printoptions(threshold=6)\na",
"execution_count": 80,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 80,
"data": {
"text/plain": "array([ 0, 1, 2, ..., 12, 13, 14])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 24 [打印设置相关]如何打印全部数据避免被省略"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.set_printoptions(threshold=1000)\nnp.arange(100)",
"execution_count": 81,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 81,
"data": {
"text/plain": "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,\n 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,\n 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,\n 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 25 导入含有数字和文本的数据集,保证数据完整(不要让文本变nan了)\n```\nin:url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'",
"execution_count": 82,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.set_printoptions(threshold=6)\niris = np.genfromtxt(url,delimiter=',',dtype='object')\niris",
"execution_count": 84,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 84,
"data": {
"text/plain": "array([[b'5.1', b'3.5', b'1.4', b'0.2', b'Iris-setosa'],\n [b'4.9', b'3.0', b'1.4', b'0.2', b'Iris-setosa'],\n [b'4.7', b'3.2', b'1.3', b'0.2', b'Iris-setosa'],\n ...,\n [b'6.5', b'3.0', b'5.2', b'2.0', b'Iris-virginica'],\n [b'6.2', b'3.4', b'5.4', b'2.3', b'Iris-virginica'],\n [b'5.9', b'3.0', b'5.1', b'1.8', b'Iris-virginica']], dtype=object)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 26 一维tuple数组,提取特定一列\n```\nin:url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\niris_1d = np.genfromtxt(url, delimiter=',', dtype=None)\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\niris_1d = np.genfromtxt(url, delimiter=',', dtype=None)",
"execution_count": 85,
"outputs": [
{
"output_type": "stream",
"text": "/home/yichang/miniconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: VisibleDeprecationWarning: Reading unicode strings without specifying the encoding argument is deprecated. Set the encoding, use None for the system default.\n \n",
"name": "stderr"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "species = np.array([row[4] for row in iris_1d])\nspecies[:5]",
"execution_count": 86,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 86,
"data": {
"text/plain": "array([b'Iris-setosa', b'Iris-setosa', b'Iris-setosa', b'Iris-setosa',\n b'Iris-setosa'], dtype='|S18')"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 27 如何将一维tuple数组转二维\n```\nin:同26\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\niris_1d = np.genfromtxt(url, delimiter=',', dtype=None)",
"execution_count": 87,
"outputs": [
{
"output_type": "stream",
"text": "/home/yichang/miniconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: VisibleDeprecationWarning: Reading unicode strings without specifying the encoding argument is deprecated. Set the encoding, use None for the system default.\n \n",
"name": "stderr"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "iris_2d = np.array([row.tolist()[:4] for row in iris_1d])\niris_2d[:4]",
"execution_count": 88,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 88,
"data": {
"text/plain": "array([[5.1, 3.5, 1.4, 0.2],\n [4.9, 3. , 1.4, 0.2],\n [4.7, 3.2, 1.3, 0.2],\n [4.6, 3.1, 1.5, 0.2]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 28 计算数组均值、中位数、标准差\n```\nin: np.arange(1000)\nout:499.5 499.5 288.6749902572095\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.arange(1000)\n(np.mean(a), np.median(a), np.std(a))",
"execution_count": 89,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 89,
"data": {
"text/plain": "(499.5, 499.5, 288.6749902572095)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 29 数组归一化\n```\nin:a = np.arange(15)\na = a.reshape(3,5)\nout:array([[0. , 0.071429, 0.142857, 0.214286, 0.285714],\n [0.357143, 0.428571, 0.5 , 0.571429, 0.642857],\n [0.714286, 0.785714, 0.857143, 0.928571, 1. ]])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.arange(15)\na = a.reshape(3,5)",
"execution_count": 90,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "ma = a.max()\nmi = a.min()\n(a - mi)/(ma - mi)",
"execution_count": 91,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 91,
"data": {
"text/plain": "array([[0. , 0.071429, 0.142857, 0.214286, 0.285714],\n [0.357143, 0.428571, 0.5 , 0.571429, 0.642857],\n [0.714286, 0.785714, 0.857143, 0.928571, 1. ]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 30 如何计算softmax分数"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Input\nurl = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nsepallength = np.array([float(row[0]) for row in iris])",
"execution_count": 92,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sepallength[:5]",
"execution_count": 93,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 93,
"data": {
"text/plain": "array([5.1, 4.9, 4.7, 4.6, 5. ])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# 对向量进行归一化,凸显其中最大的值并抑制远低于最大值的其他分量。\n# 重要函数\ndef softmax(x):\n e_x = np.exp(x - np.max(x))\n return e_x / e_x.sum(axis=0)",
"execution_count": 94,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.set_printoptions(threshold=1000)\nsoftmax(sepallength[:10])",
"execution_count": 96,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 96,
"data": {
"text/plain": "array([0.122295, 0.100127, 0.081977, 0.074176, 0.110657, 0.165081,\n 0.074176, 0.110657, 0.06073 , 0.100127])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 31找到第5%和第95%大的值\n\n```\nin:a = np.random.rand(100)\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\nnp.percentile(a,q=[5,95])",
"execution_count": 97,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 97,
"data": {
"text/plain": "array([0.059188, 0.975081])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 32 如何在随机位置改值,在一个数组中随机10个位置改为nan\n```\nin: a = np.random.rand(100)\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)",
"execution_count": 98,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.set_printoptions(threshold=10)\na[np.random.randint(100,size=10)]=np.nan\na",
"execution_count": 99,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 99,
"data": {
"text/plain": "array([0.166694, 0.023178, 0.160745, ..., 0.301752, 0.979624, 0.035627])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 33 找出数组中nan的位置\n```\nin: a = np.random.rand(100)\na[np.random.randint(100, size=20)] = np.nan\nout: 20\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\na[np.random.randint(100, size=20)] = np.nan",
"execution_count": 100,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.where(np.isnan(a))",
"execution_count": 101,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 101,
"data": {
"text/plain": "(array([ 0, 1, 7, ..., 87, 90, 98]),)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 34 使用2个或以上条件来过滤数组元素\n```\nin: a = np.random.rand(100)\nout:在>0.2 且 <0.8\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)",
"execution_count": 102,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a[(a>0.2) & (a < 0.8)] #必须有括号",
"execution_count": 104,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 104,
"data": {
"text/plain": "array([0.699632, 0.271694, 0.599993, ..., 0.266454, 0.657147, 0.748506])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 35 删除含有nan的行\n```\nin:a = np.random.rand(100)\na[np.random.randint(100, size=4)] = np.nan\na = a.reshape(10,10)\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\na[np.random.randint(100, size=4)] = np.nan\na = a.reshape(10,10)",
"execution_count": 105,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "index = [~np.any(np.isnan(row)) for row in a]\nindex",
"execution_count": 107,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 107,
"data": {
"text/plain": "[True, True, True, False, True, True, False, False, True, False]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a[index]",
"execution_count": 108,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 108,
"data": {
"text/plain": "array([[0.934542, 0.896985, 0.343623, ..., 0.563431, 0.455038, 0.341985],\n [0.623383, 0.21236 , 0.133043, ..., 0.025134, 0.270897, 0.958288],\n [0.989244, 0.524229, 0.116979, ..., 0.220498, 0.561125, 0.720267],\n [0.685852, 0.410708, 0.882639, ..., 0.015783, 0.264345, 0.42559 ],\n [0.912524, 0.263556, 0.271085, ..., 0.424459, 0.244018, 0.274413],\n [0.810186, 0.121511, 0.010492, ..., 0.688104, 0.13497 , 0.008719]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 36 求两列相关性(如第0和第2列相关性)\n```\nin:a = np.random.rand(100)\na = a.reshape(10,10)\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\na = a.reshape(10,10)",
"execution_count": 110,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.corrcoef(a[:,0],a[:,2]) # 返回一个相关性矩阵:1与1,1与2,2与1,2与2 的相关性",
"execution_count": 113,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 113,
"data": {
"text/plain": "array([[ 1. , -0.190487],\n [-0.190487, 1. ]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 37 判断数组是否有nan值\n```\nin:a = np.random.rand(100)\na[np.random.randint(100, size=4)] = np.nan\na = a.reshape(10,10)\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\na[np.random.randint(100, size=4)] = np.nan\na = a.reshape(10,10)",
"execution_count": 114,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.isnan(a).any()",
"execution_count": 115,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 115,
"data": {
"text/plain": "True"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 38 将数组中的nan全部替换为0"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\na[np.random.randint(100, size=4)] = np.nan\na = a.reshape(10,10)",
"execution_count": 116,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a[np.isnan(a)]=0",
"execution_count": 117,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 39 求数组不重复值个数\n```\nin: a = np.random.rand(100)\na[np.random.randint(100, size=4)] = np.nan\na = a.reshape(10,10)\na = a*10\na = a.astype('int16')\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100) # [0,1]之前生成100个树\na[np.random.randint(100, size=4)] = np.nan #随机取4个置nan\na = a.reshape(10,10)\na = a*10\na = a.astype('int16')",
"execution_count": 118,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a",
"execution_count": 120,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 120,
"data": {
"text/plain": "array([[5, 7, 0, ..., 0, 2, 7],\n [1, 4, 9, ..., 1, 2, 1],\n [8, 8, 0, ..., 5, 7, 8],\n ...,\n [9, 3, 1, ..., 0, 0, 4],\n [0, 6, 5, ..., 1, 7, 4],\n [1, 0, 1, ..., 1, 0, 4]], dtype=int16)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "len(np.unique(a))",
"execution_count": 119,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 119,
"data": {
"text/plain": "10"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 40 数字转文本(0-3转s,4-6转m,7-9转l)\n```\na = np.random.rand(100)\na = a.reshape(10,10)\na = a*10\na = a.astype('int16')\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\na[np.random.randint(100, size=4)] = np.nan\na = a.reshape(10,10)\na = a*10\na = a.astype('int16')",
"execution_count": 121,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "b = np.full_like(a,\"\",dtype='S10')\nb[a<=3] = 's'\nb[(a<3) & (a<=6)]='m'\nb[(a>6) &(a<=9)]='l'\nb",
"execution_count": 122,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 122,
"data": {
"text/plain": "array([[b'', b'', b'', ..., b'm', b'l', b's'],\n [b'', b'm', b'm', ..., b's', b'm', b''],\n [b'm', b'l', b'l', ..., b'l', b's', b's'],\n ...,\n [b'', b'l', b's', ..., b'l', b'', b'l'],\n [b'l', b'l', b's', ..., b's', b'', b'm'],\n [b'l', b'l', b'l', ..., b'l', b'l', b'l']], dtype='|S10')"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 41 用现有的列,创建新的一列(第0列叫x,第1列叫y,新增 sin(pi*(x/100)) * y^2)\n```\na = np.random.rand(100)\na = a.reshape(10,10)\na = a*10\na = a.astype('int16')\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\na = a.reshape(10,10)\na = a*10\na = a.astype('int16')",
"execution_count": 123,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.sin(np.pi*(a[0]/100)) * (a[1]**2)",
"execution_count": 124,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 124,
"data": {
"text/plain": "array([13.670564, 0.094108, 7.853157, 1.539127, 0.872573, 17.855431,\n 2.544271, 0. , 15.177886, 0. ])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 42 从1d数组中抽样选出10个元素\n```\nin:a = np.random.rand(100)\na = a*10\na = a.astype('int16')\n```\n"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\na = a*10\na = a.astype('int16')",
"execution_count": 125,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.random.choice(a,10)",
"execution_count": 126,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 126,
"data": {
"text/plain": "array([3, 2, 2, 3, 9, 0, 4, 1, 3, 2], dtype=int16)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 43 如何获取1d数组中不重复的第二大的值\n```\nin:a = np.random.rand(100)\na = a*10\na = a.astype('int16')\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\na = a*10\na = a.astype('int16')",
"execution_count": 127,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.unique(np.sort(a))[-2]",
"execution_count": 128,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 128,
"data": {
"text/plain": "8"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 44 依据某一列对2d数组排序(例如根据第0列排序)\n```\nin:a = np.random.rand(100)\na = a*10\na = a.astype('int16')\na = a.reshape(10,10)\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\na = a*10\na = a.astype('int16')\na = a.reshape(10,10)",
"execution_count": 129,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a[ a[:,0].argsort() ]",
"execution_count": 131,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 131,
"data": {
"text/plain": "array([[0, 7, 5, ..., 8, 2, 2],\n [0, 2, 3, ..., 8, 4, 2],\n [0, 2, 1, ..., 7, 4, 7],\n ...,\n [7, 1, 4, ..., 5, 9, 8],\n [8, 6, 5, ..., 6, 5, 1],\n [9, 2, 2, ..., 2, 9, 8]], dtype=int16)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 45 众数\n```\nin:a = np.random.rand(100)\na = a*10\na = a.astype('int16')\na = a.reshape(10,10)\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\na = a*10\na = a.astype('int16')\na = a.reshape(10,10)",
"execution_count": 132,
"outputs": []
},
{
"metadata": {
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "v, c = np.unique(a, return_counts=True)\nv,c",
"execution_count": 133,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 133,
"data": {
"text/plain": "(array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int16),\n array([12, 11, 7, 14, 8, 18, 8, 5, 11, 6]))"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "v[np.argmax(c)]",
"execution_count": 134,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 134,
"data": {
"text/plain": "5"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 46 找出比给定值大的数第一次出现的位置\n```\nin:a = np.random.rand(100)\na = a*10\na = a.astype('int16')\n\nb = 5\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\na = a*10\na = a.astype('int16')\nb = 5",
"execution_count": 135,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a",
"execution_count": 138,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 138,
"data": {
"text/plain": "array([6, 7, 3, ..., 7, 8, 3], dtype=int16)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.argwhere(a>b)",
"execution_count": 136,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 136,
"data": {
"text/plain": "array([[ 0],\n [ 1],\n [ 3],\n ...,\n [95],\n [97],\n [98]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.argwhere(a>b)[0]",
"execution_count": 137,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 137,
"data": {
"text/plain": "array([0])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 47 将所有>7的替换为7,<3的替换为3\n```\nin:a = np.random.rand(100)\na = a*10\na = a.astype('int16')\na = a.reshape(10,10)\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.rand(100)\na = a*10\na = a.astype('int16')\na = a.reshape(10,10)",
"execution_count": 139,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.where( a<3, 3, np.where(a>7, 7, a))",
"execution_count": 140,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 140,
"data": {
"text/plain": "array([[7, 7, 3, ..., 7, 4, 3],\n [7, 5, 3, ..., 3, 6, 7],\n [3, 3, 3, ..., 3, 6, 6],\n ...,\n [5, 7, 3, ..., 7, 7, 3],\n [3, 3, 6, ..., 7, 4, 3],\n [6, 4, 3, ..., 6, 3, 4]], dtype=int16)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 48 获取1d数组topK大的值(例如k=5)\n```\na = np.random.uniform(1,50, 20)\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.random.uniform(1,50, 20) #[1,50) 20个数\na",
"execution_count": 141,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 141,
"data": {
"text/plain": "array([35.151916, 5.19257 , 28.627778, ..., 49.096271, 4.746612,\n 8.141269])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.sort(a)[-5:]",
"execution_count": 142,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 142,
"data": {
"text/plain": "array([35.238293, 41.050229, 42.032028, 45.025876, 49.096271])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 49 如何逐行计算数组中所有值的数量?"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.random.seed(100)\narr = np.random.randint(1, 11, size=(6, 10))",
"execution_count": 150,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def count_rowwise(arr2d):\n results=[]\n for row in arr2d:\n uniqs, counts = np.unique(row, return_counts=True)\n zeros = np.zeros( arr2d.shape[1], dtype=int)\n zeros[uniqs-1] = counts\n results.append(zeros.tolist())\n return results\ncount_rowwise(arr)",
"execution_count": 155,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 155,
"data": {
"text/plain": "[[1, 0, 2, 1, 1, 1, 0, 2, 2, 0],\n [2, 1, 3, 0, 1, 0, 1, 0, 1, 1],\n [0, 3, 0, 2, 3, 1, 0, 1, 0, 0],\n [1, 0, 2, 1, 0, 1, 0, 2, 1, 2],\n [2, 2, 2, 0, 0, 1, 1, 1, 1, 0],\n [1, 1, 1, 1, 1, 2, 0, 0, 2, 1]]"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 50 如何把数组的数组转为1d数组\n```\n# Input:\narr1 = np.arange(3)\narr2 = np.arange(3,7)\narr3 = np.arange(7,10)\n\narray_of_arrays = np.array([arr1, arr2, arr3])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Input:\narr1 = np.arange(3)\narr2 = np.arange(3,7)\narr3 = np.arange(7,10)\n\narray_of_arrays = np.array([arr1, arr2, arr3])\narray_of_arrays",
"execution_count": 143,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 143,
"data": {
"text/plain": "array([array([0, 1, 2]), array([3, 4, 5, 6]), array([7, 8, 9])],\n dtype=object)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.array( a for a in arr for arr in array_of_arrays ) # 先写外圈",
"execution_count": 144,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 144,
"data": {
"text/plain": "array(<generator object <genexpr> at 0x7feb3c09f0c0>, dtype=object)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.array([a for arr in array_of_arrays for a in arr])",
"execution_count": 145,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 145,
"data": {
"text/plain": "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 51 为数组的每个元素生成二进制编码\n```\nin: a = np.array([2, 3, 2, 2, 2, 1])\nout:array([[ 0., 1., 0.],\n [ 0., 0., 1.],\n [ 0., 1., 0.],\n [ 0., 1., 0.],\n [ 0., 1., 0.],\n [ 1., 0., 0.]])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([2, 3, 2, 2, 2, 1])",
"execution_count": 146,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def one_hot_encodings(arr):\n uniqs = np.unique(arr)\n out = np.zeros( (len(arr), len(uniqs)) )\n for i, k in enumerate(arr):\n out[i,k-1] = 1\n return out\none_hot_encodings(a)",
"execution_count": 148,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 148,
"data": {
"text/plain": "array([[0., 1., 0.],\n [0., 0., 1.],\n [0., 1., 0.],\n [0., 1., 0.],\n [0., 1., 0.],\n [1., 0., 0.]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 52 分好组的元素给定组内行号\n```\nin: a = np.array(['a','a','a','b','b','c','c','c','c'])\nout: array([0,1,2,0,1,0,1,2,3])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.array(['a','a','a','b','b','c','c','c','c'])",
"execution_count": 156,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[i for var in np.unique(a) for i, _ in enumerate( a[a==var])]",
"execution_count": 159,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 159,
"data": {
"text/plain": "[0, 1, 2, 0, 1, 0, 1, 2, 3]"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 53 分好组的元素每组给个标号\n```\nin: a = np.array(['a','a','a','b','b','c','c','c','c'])\nout: array([0,0,0,1,1,2,2,2,2])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.array(['a','a','a','b','b','c','c','c','c'])",
"execution_count": 160,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[ np.unique(a) == s for val in np.unique(a) for s in a[a==val] ]",
"execution_count": 163,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 163,
"data": {
"text/plain": "[array([ True, False, False]),\n array([ True, False, False]),\n array([ True, False, False]),\n array([False, True, False]),\n array([False, True, False]),\n array([False, False, True]),\n array([False, False, True]),\n array([False, False, True]),\n array([False, False, True])]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[ np.argwhere( np.unique(a) == s)[0,0] for val in np.unique(a) for s in a[a==val] ]",
"execution_count": 165,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 165,
"data": {
"text/plain": "[0, 0, 0, 1, 1, 2, 2, 2, 2]"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 54 给出每个元素在排序后的位置下标数组\n给出元素的排名\n```\nin: [ 9 4 15 0 17 16 17 8 9 0]\nout: [4 2 6 0 8 7 9 3 5 1]\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.random.seed(10)\na = np.random.randint(20, size=10)",
"execution_count": 166,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a.argsort()",
"execution_count": 167,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 167,
"data": {
"text/plain": "array([3, 9, 1, 7, 0, 8, 2, 5, 4, 6])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a.argsort().argsort()",
"execution_count": 168,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 168,
"data": {
"text/plain": "array([4, 2, 6, 0, 8, 7, 9, 3, 5, 1])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 55 上题改成多维数组\n```\nin:np.random.seed(10)\na = np.random.randint(20, size=[2,5])\nprint(a)\n\nout:#> [[4 2 6 0 8]\n#> [7 9 3 5 1]]\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Input:\nnp.random.seed(10)\na = np.random.randint(20, size=[2,5])\na",
"execution_count": 169,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 169,
"data": {
"text/plain": "array([[ 9, 4, 15, 0, 17],\n [16, 17, 8, 9, 0]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a.ravel()",
"execution_count": 173,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 173,
"data": {
"text/plain": "array([ 9, 4, 15, 0, 17, 16, 17, 8, 9, 0])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a.reshape(-1)",
"execution_count": 175,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 175,
"data": {
"text/plain": "array([ 9, 4, 15, 0, 17, 16, 17, 8, 9, 0])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a.ravel().argsort().argsort().reshape(a.shape)",
"execution_count": 174,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 174,
"data": {
"text/plain": "array([[4, 2, 6, 0, 8],\n [7, 9, 3, 5, 1]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 56 二维数组找出每一行的最大值\n```\n# Input\nnp.random.seed(100)\na = np.random.randint(1,10, [5,3])\na\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.random.seed(100)\na = np.random.randint(1,10, [5,3])\na",
"execution_count": 177,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 177,
"data": {
"text/plain": "array([[9, 9, 4],\n [8, 8, 1],\n [5, 3, 6],\n [3, 3, 3],\n [2, 1, 9]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a.max(axis=1)",
"execution_count": 178,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 178,
"data": {
"text/plain": "array([9, 8, 6, 3, 9])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 57 上题变种,计算每一行max/min的值"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.random.seed(100)\na = np.random.randint(1,10, [5,3])\na",
"execution_count": 181,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 181,
"data": {
"text/plain": "array([[9, 9, 4],\n [8, 8, 1],\n [5, 3, 6],\n [3, 3, 3],\n [2, 1, 9]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "[ max(row)/min(row) for row in a ]",
"execution_count": 180,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 180,
"data": {
"text/plain": "[2.25, 8.0, 2.0, 1.0, 9.0]"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 58 找到数组中重复的值\n第二次出现开始算\n```\nin: [0 0 3 0 2 4 2 2 2 2]\nout:[False True False True False False True True True True]\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Input\nnp.random.seed(100)\na = np.random.randint(0, 5, 10)",
"execution_count": 182,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "out = np.full(len(a), True)\nunique_positions = np.unique(a, return_index=True) # 实际值与 index值 \nunique_positions # u",
"execution_count": 183,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 183,
"data": {
"text/plain": "(array([0, 2, 3, 4]), array([0, 4, 2, 5]))"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "out[unique_positions[1]] = False\nout",
"execution_count": 184,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 184,
"data": {
"text/plain": "array([False, True, False, True, False, False, True, True, True,\n True])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 59 如何找到 NumPy 的分组平均值?"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Input\nurl = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'\niris = np.genfromtxt(url, delimiter=',', dtype='object')\nnames = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')",
"execution_count": 185,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "uniques = np.unique(iris[:, 4]) # 得到第5列的种类\nuniques",
"execution_count": 186,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 186,
"data": {
"text/plain": "array([b'Iris-setosa', b'Iris-versicolor', b'Iris-virginica'],\n dtype=object)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "output = []\nfor v in uniques:\n group = iris[iris[:,4]==v] # 得到某一类的所有数据\n output.append( [v, np.mean(group[:,1].astype(float) )])\noutput",
"execution_count": 187,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 187,
"data": {
"text/plain": "[[b'Iris-setosa', 3.418],\n [b'Iris-versicolor', 2.7700000000000005],\n [b'Iris-virginica', 2.974]]"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 60 将一张jpg图片转换为numpy数组\n```\nin:URL = 'https://upload.wikimedia.org/wikipedia/commons/8/8b/Denali_Mt_McKinley.jpg'\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "URL = 'https://upload.wikimedia.org/wikipedia/commons/8/8b/Denali_Mt_McKinley.jpg'",
"execution_count": 188,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# conda install -c conda-forge scikit-image\nfrom skimage import io\nio.imread(URL)",
"execution_count": 190,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 190,
"data": {
"text/plain": "array([[[ 9, 72, 125],\n [ 9, 72, 125],\n [ 9, 72, 125],\n ...,\n [ 42, 103, 147],\n [ 42, 103, 147],\n [ 43, 104, 148]],\n\n [[ 9, 72, 125],\n [ 9, 72, 125],\n [ 10, 73, 126],\n ...,\n [ 42, 103, 147],\n [ 42, 103, 147],\n [ 43, 104, 148]],\n\n [[ 9, 72, 125],\n [ 10, 73, 126],\n [ 10, 73, 126],\n ...,\n [ 44, 105, 150],\n [ 45, 106, 151],\n [ 45, 106, 151]],\n\n ...,\n\n [[ 21, 41, 50],\n [ 29, 51, 64],\n [ 28, 54, 69],\n ...,\n [ 27, 58, 79],\n [ 22, 53, 74],\n [ 19, 47, 69]],\n\n [[ 27, 51, 63],\n [ 29, 55, 68],\n [ 27, 56, 72],\n ...,\n [ 33, 70, 97],\n [ 27, 64, 91],\n [ 22, 57, 85]],\n\n [[ 18, 45, 56],\n [ 20, 48, 62],\n [ 21, 52, 70],\n ...,\n [ 15, 51, 77],\n [ 12, 48, 74],\n [ 11, 45, 72]]], dtype=uint8)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 61 如何剔除所有nan值\n```\nin:np.array([1,2,3,np.nan,5,6,7,np.nan])\nout:array([ 1., 2., 3., 5., 6., 7.])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([1,2,3,np.nan,5,6,7,np.nan])",
"execution_count": 191,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a[~np.isnan(a)]",
"execution_count": 192,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 192,
"data": {
"text/plain": "array([1., 2., 3., 5., 6., 7.])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 62 如何计算两数组欧式距离\n```\nin:a = np.array([1,2,3,4,5])\nb = np.array([4,5,6,7,8])\nout:6.7082039324993694\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Input\na = np.array([1,2,3,4,5])\nb = np.array([4,5,6,7,8])",
"execution_count": 193,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "dist = np.linalg.norm(a-b)\ndist",
"execution_count": 194,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 194,
"data": {
"text/plain": "6.708203932499369"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 63 一维数组找到所有峰值(比前后都大的值)的下标\n局部极大值\n```\nin:a = np.array([1, 3, 7, 1, 2, 6, 0, 1])\nout:[2,5]\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a = np.array([1, 3, 7, 1, 2, 6, 0, 1])",
"execution_count": 197,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.diff(a)",
"execution_count": 198,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 198,
"data": {
"text/plain": "array([ 2, 4, -6, 1, 4, -6, 1])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.sign(np.diff(a))",
"execution_count": 199,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 199,
"data": {
"text/plain": "array([ 1, 1, -1, 1, 1, -1, 1])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "double_diff = np.diff(np.sign(np.diff(a)))\ndouble_diff",
"execution_count": 201,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 201,
"data": {
"text/plain": "array([ 0, -2, 2, 0, -2, 2])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.where(double_diff == -2)[0] + 1",
"execution_count": 203,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 203,
"data": {
"text/plain": "array([2, 5])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 64 二维数组每一行都减去一个一维数组\n```\nin:a_2d = np.array([[3,3,3],[4,4,4],[5,5,5]])\n b_1d = np.array([1,2,3])\n \n out: [\n [2,1,0],\n [3,2,1],\n [4,3,2]\n ]\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a_2d = np.array([[3,3,3],[4,4,4],[5,5,5]])\nb_1d = np.array([1,2,3])",
"execution_count": 204,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "a_2d - b_1d",
"execution_count": 205,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 205,
"data": {
"text/plain": "array([[2, 1, 0],\n [3, 2, 1],\n [4, 3, 2]])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 65 找到第n个特定数出现的下标\n```\n找第5个1出现的位置\nx = np.array([1, 2, 1, 1, 3, 4, 3, 1, 1, 2, 1, 1, 2])\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = np.array([1, 2, 1, 1, 3, 4, 3, 1, 1, 2, 1, 1, 2])",
"execution_count": 206,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.where(x==1)",
"execution_count": 207,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 207,
"data": {
"text/plain": "(array([ 0, 2, 3, 7, 8, 10, 11]),)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.where(x==1)[0][5-1]",
"execution_count": 208,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 208,
"data": {
"text/plain": "8"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 66 将datetime64类型转datetime类型\n```\nin:dt64 = np.datetime64('2018-02-25 22:10:10')\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "dt64 = np.datetime64('2018-02-25 22:10:10')",
"execution_count": 209,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "dt64.tolist()",
"execution_count": 210,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 210,
"data": {
"text/plain": "datetime.datetime(2018, 2, 25, 22, 10, 10)"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 67 计算滑动窗口均值(例如窗口大小3,就是0-2均值,1-3均值,2-4....)\n```\nin:np.random.seed(100)\nZ = np.random.randint(10, size=10)\n\nout:[ 6.33 6. 5.67 4.67 3.67 2. 3.67 3. ]\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.random.seed(100)\nZ = np.random.randint(10, size=10)",
"execution_count": 211,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.ones(3)/3",
"execution_count": 212,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 212,
"data": {
"text/plain": "array([0.333333, 0.333333, 0.333333])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.convolve(Z, np.ones(3)/3, mode='valid') #逐项相乘",
"execution_count": 213,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 213,
"data": {
"text/plain": "array([6.333333, 6. , 5.666667, 4.666667, 3.666667, 2. ,\n 3.666667, 3. ])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 68 创建一个等差数列初值5增幅3数组总长10"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.arange(5, 35,3)",
"execution_count": 217,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 217,
"data": {
"text/plain": "array([ 5, 8, 11, 14, 17, 20, 23, 26, 29, 32])"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 69 如何在一系列无序日期中,补全缺失的日期\n"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Input\ndates = np.arange(np.datetime64('2018-02-01'), np.datetime64('2018-02-25'), 2)\nprint(dates)",
"execution_count": 218,
"outputs": [
{
"output_type": "stream",
"text": "['2018-02-01' '2018-02-03' '2018-02-05' ... '2018-02-19' '2018-02-21'\n '2018-02-23']\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "np.diff(dates)",
"execution_count": 221,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 221,
"data": {
"text/plain": "array([2, 2, 2, ..., 2, 2, 2], dtype='timedelta64[D]')"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "zip(dates, np.diff(dates))",
"execution_count": 222,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 222,
"data": {
"text/plain": "<zip at 0x7feb257e4788>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "out = []\nfor date, d in zip(dates, np.diff(dates)):\n out.append(np.arange(date, (date+d)))\nout",
"execution_count": 220,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 220,
"data": {
"text/plain": "[array(['2018-02-01', '2018-02-02'], dtype='datetime64[D]'),\n array(['2018-02-03', '2018-02-04'], dtype='datetime64[D]'),\n array(['2018-02-05', '2018-02-06'], dtype='datetime64[D]'),\n array(['2018-02-07', '2018-02-08'], dtype='datetime64[D]'),\n array(['2018-02-09', '2018-02-10'], dtype='datetime64[D]'),\n array(['2018-02-11', '2018-02-12'], dtype='datetime64[D]'),\n array(['2018-02-13', '2018-02-14'], dtype='datetime64[D]'),\n array(['2018-02-15', '2018-02-16'], dtype='datetime64[D]'),\n array(['2018-02-17', '2018-02-18'], dtype='datetime64[D]'),\n array(['2018-02-19', '2018-02-20'], dtype='datetime64[D]'),\n array(['2018-02-21', '2018-02-22'], dtype='datetime64[D]')]"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "filled_in = np.array(out).reshape(-1)\nfilled_in",
"execution_count": 223,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 223,
"data": {
"text/plain": "array(['2018-02-01', '2018-02-02', '2018-02-03', ..., '2018-02-20',\n '2018-02-21', '2018-02-22'], dtype='datetime64[D]')"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "output = np.hstack([filled_in, dates[-1]]) #添加了最后一个\noutput",
"execution_count": 224,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 224,
"data": {
"text/plain": "array(['2018-02-01', '2018-02-02', '2018-02-03', ..., '2018-02-21',\n '2018-02-22', '2018-02-23'], dtype='datetime64[D]')"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# 70 给定一维数组,创建阶梯型二维数组,二维数组列数是4,步幅是2\n```\nin:arr = np.arange(15) \nout:#> [[ 0 1 2 3]\n#> [ 2 3 4 5]\n#> [ 4 5 6 7]\n#> [ 6 7 8 9]\n#> [ 8 9 10 11]\n#> [10 11 12 13]]\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "arr = np.arange(15) ",
"execution_count": 225,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def gen_rows(a, steps= 2, columns = 4):\n rows = (a.size - columns)//steps + 1\n return np.array([a[s: s+columns] for s in np.arange(0, rows * steps, steps)])\n\ngen_rows(arr)",
"execution_count": 227,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 227,
"data": {
"text/plain": "array([[ 0, 1, 2, 3],\n [ 2, 3, 4, 5],\n [ 4, 5, 6, 7],\n [ 6, 7, 8, 9],\n [ 8, 9, 10, 11],\n [10, 11, 12, 13]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"gist": {
"id": "",
"data": {
"description": "Numpy/numpy101noanswer.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment