<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 人生苦短，我用python
python第四课
课程安排
1、numpy
2、pandas
3、matplotlib
numpy
数组跟列表，列表可以存储任意类型的数据，而数组只能存储一种类型数据
Step1:  从原有列表转换为数组
Step2:  生成数组
Step3:  random
Step4:  范围取值
Step5:  | Data type     | Description |
|
Step6:  数组属性
Step7:  运算
Step8:  | Operator      | Equivalent ufunc    | Description                           |
|---------------|---------------------|---------------------------------------|
|+          |np.add           |Addition (e.g., 1 + 1 = 2)         |
|-          |np.subtract      |Subtraction (e.g., 3 - 2 = 1)      |
|-          |np.negative      |Unary negation (e.g., -2)          |
|*          |np.multiply      |Multiplication (e.g., 2 * 3 = 6)   |
|/          |np.divide        |Division (e.g., 3 / 2 = 1.5)       |
|//         |np.floor_divide  |Floor division (e.g., 3 // 2 = 1)  |
|**         |np.power         |Exponentiation (e.g., 2 ** 3 = 8)  |
|%          |np.mod           |Modulus/remainder (e.g., 9 % 4 = 1)|
Step9:  统计类型
Step10:  notebook使用小技巧
%timeit 代码 ; 此方法来判断程序的执行效率
Step11:  由上代码可已看出np.sum的执行效率高，推荐使用
比较
Step12:  | Operator      | Equivalent ufunc    || Operator      | Equivalent ufunc    |
|---------------|---------------------||---------------|---------------------|
|==         |np.equal         ||!=         |np.not_equal     |
|&lt;          |np.less          ||&lt;=         |np.less_equal    |
|&gt;          |np.greater       ||&gt;=         |np.greater_equal |
Step13:  变形
Step14:  排序
Step15:  拼接
<ASSISTANT_TASK:>
Python Code:
import array
a = array.array('i', range(10))
# 数据类型必须统一
a[1] = 's'
a
import numpy as np
Explanation: 人生苦短，我用python
python第四课
课程安排
1、numpy
2、pandas
3、matplotlib
numpy
数组跟列表，列表可以存储任意类型的数据，而数组只能存储一种类型数据
End of explanation
a_list = list(range(10))
b = np.array(a_list)
type(b)
Explanation: 从原有列表转换为数组
End of explanation
a = np.zeros(10, dtype=int)
print(type(a))
# 查看数组类型
a.dtype
a = np.zeros((4,4), dtype=int)
print(type(a))
# 查看数组类型
print(a.dtype)
a
np.ones((4,4), dtype=float)
np.full((3,3), 3.14)
a
np.zeros_like(a)
np.ones_like(a)
np.full_like(a, 4.12, dtype=float)
Explanation: 生成数组
End of explanation
print(random.randint(5,10))
print(random.random())
np.random.random((3,3))
# 经常会用到
np.random.randint(0,10, (5,5))
Explanation: random
End of explanation
list(range(0,10,2))
np.arange(0,3,2)
# 经常用到
np.linspace(0, 3, 10)
# n维的单位矩阵
np.eye(5)
Explanation: 范围取值
End of explanation
# 嵌套列表的元素访问
var = [[1,2,3], [3,4,5], [5,6,7]]
var[0][0]
# 数组中元素的访问
a = np.array(var)
a[-1][0]
a
# 这两种访问方式是等价的
a[2, 0], a[2][0]
# 数组切片
a[:2, :2]
# 同上边的方式是不等价的
a[:2][:2]
Explanation: | Data type     | Description |
|:---------------|:-------------|
| bool_     | Boolean (True or False) stored as a byte |
| int_      | Default integer type (same as C long; normally either int64 or int32)| 
| intc      | Identical to C int (normally int32 or int64)| 
| intp      | Integer used for indexing (same as C ssize_t; normally either int32 or int64)| 
| int8      | Byte (-128 to 127)| 
| int16     | Integer (-32768 to 32767)|
| int32     | Integer (-2147483648 to 2147483647)|
| int64     | Integer (-9223372036854775808 to 9223372036854775807)| 
| uint8     | Unsigned integer (0 to 255)| 
| uint16    | Unsigned integer (0 to 65535)| 
| uint32    | Unsigned integer (0 to 4294967295)| 
| uint64    | Unsigned integer (0 to 18446744073709551615)| 
| float_    | Shorthand for float64.| 
| float16   | Half precision float: sign bit, 5 bits exponent, 10 bits mantissa| 
| float32   | Single precision float: sign bit, 8 bits exponent, 23 bits mantissa| 
| float64   | Double precision float: sign bit, 11 bits exponent, 52 bits mantissa| 
| complex_  | Shorthand for complex128.| 
| complex64 | Complex number, represented by two 32-bit floats| 
| complex128| Complex number, represented by two 64-bit floats|
访问数组中元素
End of explanation
a
# 维度
print(a.ndim)
# shape
print(a.shape)
# size
print(a.size)
# dtype
print(a.dtype)
# a.itemsize
print(a.itemsize)
# nbytes
print(a.nbytes)
Explanation: 数组属性
End of explanation
a = np.array(list(range(10)))
a
print(a + 10)
print(a - 10)
print(a * 100)
a = np.full((3,3), 1.0, dtype=float)
a + 10 # 等价于 np.add(a, 10)
Explanation: 运算
End of explanation
a = np.linspace(0, np.pi, 5)
b = np.sin(a)
print(a)
print(b)
Explanation: | Operator      | Equivalent ufunc    | Description                           |
|---------------|---------------------|---------------------------------------|
|+          |np.add           |Addition (e.g., 1 + 1 = 2)         |
|-          |np.subtract      |Subtraction (e.g., 3 - 2 = 1)      |
|-          |np.negative      |Unary negation (e.g., -2)          |
|*          |np.multiply      |Multiplication (e.g., 2 * 3 = 6)   |
|/          |np.divide        |Division (e.g., 3 / 2 = 1.5)       |
|//         |np.floor_divide  |Floor division (e.g., 3 // 2 = 1)  |
|**         |np.power         |Exponentiation (e.g., 2 ** 3 = 8)  |
|%          |np.mod           |Modulus/remainder (e.g., 9 % 4 = 1)|
End of explanation
# 求和
print(sum([1,2,3,4,5,6]))
# 数组一维求和
a = np.full(10, 2.3)
print(sum(a))
# 数组多维求和
a = np.array([[1,2],[3,4]])
print(sum(a))
# np.sum 求和
np.sum(a)
np.sum(a, axis=1)
np.max(a, axis=1)
n = np.random.rand(10000)
Explanation: 统计类型
End of explanation
%timeit sum(n)
%timeit np.sum(n)
Explanation: notebook使用小技巧
%timeit 代码 ; 此方法来判断程序的执行效率
End of explanation
a = np.array(range(10))
a
a > 3
a != 3
a == a
Explanation: 由上代码可已看出np.sum的执行效率高，推荐使用
比较
End of explanation
np.all(a>-1)
np.any(a>-1)
Explanation: | Operator      | Equivalent ufunc    || Operator      | Equivalent ufunc    |
|---------------|---------------------||---------------|---------------------|
|==         |np.equal         ||!=         |np.not_equal     |
|&lt;          |np.less          ||&lt;=         |np.less_equal    |
|&gt;          |np.greater       ||&gt;=         |np.greater_equal |
End of explanation
a = np.full((2,10), 1, dtype=float)
a
a.reshape(4, 5)
Explanation: 变形
End of explanation
l = [
    [1,2,3],
    [34,12,4],
    [32,2,33]
]
a = np.array(l)
a
np.sort(a)
a.sort(axis=0)
a
Explanation: 排序
End of explanation
a = np.array([1, 2, 3])
b = np.array([[0, 2, 4], [1, 3, 5]])
# 按行去连接
np.concatenate([b,b,b], axis=0)
# 按列去连接
np.concatenate([b,b,b], axis=1)
Explanation: 拼接
End of explanation
<END_TASK>