运维开发网

Python数据分析入门到实战

运维开发网 https://www.qedev.com 2021-03-01 08:31 出处:51CTO 作者:mb6038ac45c5cb8
download:Python数据分析入门到实战{"cells":[{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":["importnumpyas

Python数据分析入门到实战

download:Python数据分析入门到实战

{

"cells": [

{

"cell_type": "code",

"execution_count": 1,

"metadata": {},

"outputs": [],

"source": [

"import numpy as np"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"1. 数组a = np.random.rand(3,2,3)能和b = np.random.rand(3,2,2)进行运算吗?能和c = np.random.rand(3,1,1)进行运算吗?请说明结果的原因。"

]

},

{

"cell_type": "code",

"execution_count": 3,

"metadata": {},

"outputs": [

{

"ename": "ValueError",

"evalue": "operands could not be broadcast together with shapes (3,2,3) (3,2,2) ",

"output_type": "error",

"traceback": [

"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",

"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",

"\u001b[1;32m<ipython-input-3-8c0e41d1882e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrand\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrand\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[1;31m# 答案:a和b不能参与运算,因为不满足广播的机制(对应形状的值不相等,并且没有等于1的值)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",

"\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (3,2,3) (3,2,2) "

]

}

],

"source": [

"a = np.random.rand(3,2,3)\n",

"b = np.random.rand(3,2,2)\n",

"a + b\n",

"# 答案:a和b不能参与运算,因为不满足广播的机制(对应形状的值不相等,并且没有等于1的值)"

]

},

{

"cell_type": "code",

"execution_count": 4,

"metadata": {},

"outputs": [

{

"data": {

"text/plain": [

"array([[[1.1966444 , 1.33968419, 1.21752683],\n",

" [1.87658798, 1.33365452, 1.61317125]],\n",

"\n",

" [[0.84684334, 1.00225309, 0.83019466],\n",

" [0.54902031, 1.14089275, 1.11052695]],\n",

"\n",

" [[1.04556413, 1.46687971, 1.68426195],\n",

" [0.93094155, 1.52321132, 1.33566973]]])"

]

},

"execution_count": 4,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"a = np.random.rand(3,2,3)\n",

"c = np.random.rand(3,1,1)\n",

"a + c\n",

"# 答案:a和c可以参与运算,因为满足广播机制(a和b的形状值虽然不相等,但是不相等的地方都是等于1)"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"2. 想要将数组a = np.arange(15).reshape(3,5)b = np.arange(100,124).reshape(6,4)叠加在一起,其中ab的上面,并且在b的第1列后面(下标从0开始)新增一列,用0来填充。"

]

},

{

"cell_type": "code",

"execution_count": 27,

"metadata": {},

"outputs": [

{

"data": {

"text/plain": [

"array([[ 0., 1., 2., 3., 4.],\n",

" [ 5., 6., 7., 8., 9.],\n",

" [ 10., 11., 12., 13., 14.],\n",

" [100., 101., 0., 102., 103.],\n",

" [104., 105., 0., 106., 107.],\n",

" [108., 109., 0., 110., 111.],\n",

" [112., 113., 0., 114., 115.],\n",

" [116., 117., 0., 118., 119.],\n",

" [120., 121., 0., 122., 123.]])"

]

},

"execution_count": 27,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"# 第二题答案:\n",

"# 1. 准备好数据\n",

"a = np.arange(15).reshape(3,5)\n",

"b = np.arange(100,124).reshape(6,4)\n",

"\n",

"# 2. 因为b只有4列,无法直接和a堆叠,并且题目要求要在第1列后面添加一列\n",

"# 所以先将b数组在下标为1的地方切割,然后添加完0数组后再进行拼接\n",

"bsplits = np.hsplit(b,[2])\n",

"# 3. 创建一个全0的6行1列的数组\n",

"bzero = np.zeros((6,1))\n",

"# 4. 将b的前半部分,0,b的后半部分组合在一起形成一个新的数组\n",

"c = np.hstack([bsplits[0],bzero,bsplits[1]])\n",

"# 5. 将a和新生成的数组进行堆叠得到结果\n",

"result = np.vstack([a,c])\n",

"result"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"3. 将数组a = np.random.rand(4,5)扁平化成一维数组,可以使用flattenravel,对两者的返回值进行操作,哪个会影响到数组a?对会影响到a数组的那个函数,请说明原因。"

]

},

{

"cell_type": "code",

"execution_count": 31,

"metadata": {},

"outputs": [

{

"name": "stdout",

"output_type": "stream",

"text": [

"[[0.88760841 0.18955808 0.50391074 0.66762016 0.44185892]\n",

" [0.74659343 0.4997936 0.12576716 0.16264012 0.82619357]\n",

" [0.35162609 0.27658575 0.32955319 0.17778362 0.49154326]\n",

" [0.63413851 0.0808167 0.3285915 0.98401184 0.41864415]]\n",

"==============================\n",

"[[20. 0.18955808 0.50391074 0.66762016 0.44185892]\n",

" [ 0.74659343 0.4997936 0.12576716 0.16264012 0.82619357]\n",

" [ 0.35162609 0.27658575 0.32955319 0.17778362 0.49154326]\n",

" [ 0.63413851 0.0808167 0.3285915 0.98401184 0.41864415]]\n"

]

}

],

"source": [

"# 第三题答案:\n",

"a = np.random.rand(4,5)\n",

"a1 = a.flatten()\n",

"a1[0] = 10\n",

"print(a)\n",

"print(\"=\"*30)\n",

"a2 = a.ravel()\n",

"a2[0] = 20\n",

"print(a)\n",

"# 结果:ravel会影响原来的数组。原因是因为ravel返回的是一个浅拷贝(视图),虽然在栈中的内存不一样,但是指向的堆\n",

"# 区的内存地址还是一样,所以修改了a2,会影响到原来堆区的值。但是flatten返回的确实一个深拷贝,也即栈区和堆区都\n",

"# 进行了拷贝。"

]

},

{

"cell_type": "markdown",

"metadata": {},

"source": [

"4. 使用numpy自带的csv方法读取出stock.csv文件中preClosePriceopenPricehighestPricelowestPrice的数据(提示:使用skiprows和usecols参数)。"

]

},

{

"cell_type": "code",

"execution_count": 35,

"metadata": {},

"outputs": [

{

"data": {

"text/plain": [

"array([[13.38 , 13.4 , 13.48 , 12.96 ],\n",

" [31.22 , 30.5 , 32.03 , 30.5 ],\n",

" [25.56 , 25.41 , 26.4 , 25.18 ],\n",

" ...,\n",

" [ 0.507, 0.507, 0.508, 0.507],\n",

" [ 1.675, 1.67 , 1.69 , 1.67 ],\n",

" [ 0.976, 0.976, 0.988, 0.976]])"

]

},

"execution_count": 35,

"metadata": {},

"output_type": "execute_result"

}

],

"source": [

"# 第四题答案:\n",

"stocks = np.loadtxt(\"stock.csv\",dtype=np.float,delimiter=\",\",skiprows=1,usecols=[6,7,8,9])\n",

"stocks\n",

"# help(np.loadtxt)"

]

}

],

"metadata": {

"kernelspec": {

"display_name": "Python 3",

"language": "python",

"name": "python3"

},

"language_info": {

"codemirror_mode": {

"name": "ipython",

"version": 3

},

"file_extension": ".py",

"mimetype": "text/x-python",

"name": "python",

"nbconvert_exporter": "python",

"pygments_lexer": "ipython3",

"version": "3.7.1"

}

},

"nbformat": 4,

"nbformat_minor": 2

}

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号