各种库和定义变量
| 单 词 | 意思 | 音 标 |
|---|---|---|
| Variables | 变量 | /ˈverɪəbl/ |
| assign | 赋值 | /əˈsaɪn/ |
| declare the type | 声明类型 | |
| dynamically typed | 动态类型 | |
| Arithmetic Operations | 算术运算 | |
| Arithmetic | n.算术;演算,计算;数据统计 adj.算术的 | /əˈrɪθmətɪk/ |
| comment | 注释 | |
| Addition | 加 x+y | |
| Substraction | 减法 x-y | |
| Multiplication | 乘法 x*y | /ˌmʌltɪplɪˈkeɪʃ(ə)n/ |
| Division | 除法 x/y | |
| Exponentiating | 取幂 x**2 | /ekspəʊˈnenʃɪeɪt/ |
| Division without Remainder | 无余数除法 x//y | |
| less than | 小于 | x<8 |
| more than | 大于 | x>9 |
| equal to | 等于 | x==9 |
| set | 赋值,Sets the variables x equal to 5. | x=9 |
| an ordered collection | 有序字符集合 | |
| characters | 字符 | |
| letters and numbers | 字母和数字 | |
| single or double quotation | 单引号或双引号 | / kwəʊˈteɪʃ(ə)n / |
| operations | 操作 | |
| concatenation (combining strings) | 串联(组合字符串) | /kənˌkætəˈneɪʃn/ |
| slicing (extracting sections) | 切片(提取切片) | name="kaili" name[2] |
| Indexing (fetching by offset) | 索引(按偏移量抓取) | |
| slide | 幻灯片 | /slaɪd/ |
| contiguous | 连续的 | /kənˈtɪɡjuəs/ |
| Spaces | 空格 | |
| punctuation | 标点符号 | /ˌpʌŋktʃuˈeɪʃn/ |
| polymorphism | 多态性 | /ˌpɒlɪˈmɔːfɪz(ə)m/ |
| contained | 包含 |
Packages 常用的库
Packages: NumPy, pandas, matplotlib, SciPy, scikit-learn, statsmodels
NumPy
-
作用:数值计算的基础库。提供高效的多维数组(
ndarray)结构和各种数学函数。 -
常用场景:矩阵运算、线性代数、统计分析、随机数生成等。
-
例子:
import numpy as np a = np.array([1, 2, 3]) print(a.mean()) # 求平均值
📊 pandas
-
作用:数据分析与清洗的核心工具。提供类似 Excel 表格的
DataFrame数据结构。 -
常用场景:数据读取(CSV、Excel 等)、筛选、统计汇总、缺失值处理。
-
例子:
import pandas as pd df = pd.read_csv("data.csv") print(df.describe()) # 查看数据概况
📈 matplotlib
-
作用:最常用的绘图库,用于绘制各种图表(折线图、柱状图、散点图等)。
-
常用场景:数据可视化。
-
例子:
import matplotlib.pyplot as plt plt.plot([1, 2, 3], [4, 5, 6]) plt.show()
🔬 SciPy
-
作用:科学计算库,基于 NumPy,提供更高级的数学函数。
-
常用场景:数值积分、最优化、信号处理、线性代数。
-
例子:
from scipy import integrate result, _ = integrate.quad(lambda x: x**2, 0, 1) # ∫x² dx from 0 to 1 print(result)
🤖 scikit-learn
-
作用:机器学习库。提供丰富的算法和工具用于建模和预测。
-
常用场景:分类、回归、聚类、降维、模型评估。
-
例子:
from sklearn.linear_model import LinearRegression model = LinearRegression().fit([[1], [2], [3]], [2, 4, 6]) print(model.predict([[4]])) # 预测结果
📉 statsmodels
-
作用:统计建模库。提供回归分析、假设检验、时间序列分析等功能。
-
常用场景:统计推断、经济计量分析。
-
例子:
import statsmodels.api as sm X = sm.add_constant([1, 2, 3]) y = [2, 4, 6] model = sm.OLS(y, X).fit() print(model.summary())
定义变量
| 变量类型 | 怎么定义 |
|---|---|
| integers | x=1,y=3 |
| boolean | boolean_Var=True boolean_Var * 3,True和False必须首字母大写 ,•boolean does behave exactly like a 1: |
| string | name="kaili", or name=’kaili’ |
string的运算
| 运算 | 例子 |
|---|---|
| Slicing Strings | name[2] |
| name[0:2]截取0和1号位置的字符串 | |
| name[2:] 截取位置2到最后一个字符 | |
| name[:2] 等同于name[0:2] | |
| len(name) | Spaces and punctuation count in the indexing of a string! |
| String Concatenation | full_name=name+" "+nickname |
| Using in or not in | "ang" in nickname |
| "ang" not in nickname |