更新包
# 完整写法(推荐,清晰)
pip install --upgrade 包名
# 简写(更高效)
pip install -U 包名
查看数据类型
type函数
type(10) # int
type(0.1) # float
type('hello') # str
type(True) # bool
type(None) # NoneType
type([1, 2, 3]) # list
type((1, 2, 3)) # tuple
type({'ID':1, 'gender':'female'}) # dict
type({1, 2, 3}) # set
使用jupyter lab
- 安装
pip install jupyterlab
- 启动
jupyter lab
类
class 类名:
def __init__(self, 参数, ...): # 构造函数
...
def 方法1(self, 参数, ...): # 方法1
...
def 方法2(self, 参数, ...): # 方法2
...
创建一个类测试
class Man:
def __init__(self, name):
self.name = name
print("Initialized!")
def hello(self):
print(f"Hello {self.name}")
def goodbye(self):
print(f"Good-bye {self.name}!")
m = Man("Kate")
# Initialized!
print(type(m))
# <class '__main__.Man'>
m.hello()
# Hello Kate!
m.goodbye()
# Good-bye Kate!
numpy的基本操作
- import:
import numpy as np - 查看形状:
A.shape - 查看元素的数据类型:
A.dtype - element-wise运算
- 广播运算
- 访问元素:
A[0]表示第0行,A[0][1]表示(0,1)的元素 -
for遍历:
for row in A:
print(row)
- 转为一维数组:
A.flatten() - 索引多个元素:
A[np.array([0, 2, 4])]获取索引为0、2、4的元素 - 获取所有大于15的元素:
A[A>15]