Introduction to Python for Data Science 学习笔记
目录
1. 简介
用于数据科学。
1.1 Variables and Types
· 变量名区分大小写;
· 通过变量名调用值。
Python 数据类型
float
- real numbersint
- integer numbersstr
- string, textbool
- True, False
2. Python 列表
序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。
Python有6个序列的内置类型,但最常见的是列表和元组。
列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。
列表的数据项不需要具有相同的类型。
2.1 Python List
float
- real numbersint
- integer numbersstr
- string, textbool
- True, False
Each variable represents single value.
List:
- Name a collection of values;
- Contain any type;
- Contain different types.
创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可,如下图:
2.2 Subsetting Lists
Zero-based indexing:第一个索引是0,第二个索引是1,依此类推。
- 0,1,2,…,N−2,N−10, 1, 2,\dots, N-2, N-10,1,2,…,N−2,N−1
- −(N−1),−(N−2),…,−2,−1-(N-1), -(N-2), \dots , -2, -1−(N−1),−(N−2),…,−2,−1
Python列表截取
[ start (包括) : end (不包括) ]
如:
2.3 Manipulating Lists
- Change list elements
- Add list elements
- Remove list elements
更改列表元素
增加或删除列表元素
复制列表
- Copy by reference
- Copy by value
Copy by reference
y=x #赋值引用, x和y都指向同一个对象。
Copy by value
Python还有深拷贝和浅拷贝。
3. 进阶
3.1 函数 Functions
max()
函数
max() 方法返回给定参数的最大值,参数可以为序列。
语法:
max(x, y, z, ...)
round()
函数
round() 方法返回浮点数x的四舍五入值。
语法:
round( x [, n] ) # x-数值表达式 n-数值表达式(精度),返回浮点数x的四舍五入值。
3.2 Methods: Functions that belong to objects
str
: capitalize(), replace(), etc.float
: bit_length(), conjugate(), etc.list
: intex(), count(), etc.
list
methods
list.append(obj)
:在列表末尾添加新的对象。list.count(obj)
:统计某个元素在列表中出现的次数。list.extend(obj)
:在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。list.index(obj)
:从列表中找出某个值的第一个匹配项的索引位置。list.insert(index, obj)
:将对象插入列表。list.pop([index = -1])
:移除列表中的一个元素(默认最后一个元素),并返回该元素的值。list.remove(obj)
:移除列表中某个值的第一个匹配项。list.reverse()
:反向列表中元素。list.sort(cmp=None, key=None, reverse=False)
:对原列表进行排序。
str
methods
str.capitalize()
:把字符串第一个字符大写。str.center(width)
:返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。- …
总结: - Functions:
type(fam) - Methods: call functions on objects
fam.index(“dad”)
4. Numpy
4.1 Numpy简介
NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
NumPy 是一个运行速度非常快的数学库,主要用于数组计算,包含:
- 一个强大的N维数组对象 ndarray
- 广播功能函数
- 整合 C/C++/Fortran 代码的工具
- 线性代数、傅里叶变换、随机数生成等功能
4.2 Ndarray
NumPy 最重要的一个特点是其 N 维数组对象 ndarray,它是一系列同类型数据的集合,以 0 下标为开始进行集合中元素的索引。
ndarray = N-dimensional array
ndarry可以整个数组进行运算。
!: Numpy arrays: contain only one type; Different types different behavior!
Subsetting
ndarray 数组可以基于 0 - n 的下标进行索引;numpy还可以用boolean索引。
布尔索引:通过布尔运算(如:比较运算符)来获取符合指定条件的元素的数组。
2D Numpy Arrays
4.3 Numpy: Basic Statistics
5. Matplotlib 绘图
5.1 简介
Matplotlib 是 Python 的绘图库。 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案。
直线图
散点图
直方图
5.2 个性化
plt.xlabel('Year') plt.ylabel('Population') plt.title('World Population Projections') plt.yticks([0,2,4,6,8,10],['0','2B','4B','6B','8B','10B']) plt.xlim(1800, 2100) plt.ylim(0, 11)
6. Pandas
CSV file →→ DataFrame
import pandas as pd data = pd.read_csv("路径")
- 【学习笔记】Introduction to Computer Science and Programming
- introduction to python for statistics,analysis笔记2
- introduction to python for statistics,analysis笔记3
- Python学习笔记 --- Pycharm中遇到 no data sources are configured to run thi s sql...
- 【coursera 学习笔记】An Introduction to Interactive Programming in Python--week0
- Swift学习笔记 Introduction to Swift Programming for beginners
- 【coursera 学习笔记】An Introduction to Interactive Programming in Python--week1
- Introduction to Materials Management 学习笔记--编制主计划
- 【学习笔记】Thinking in java (第三版)第一章 对象的介绍(Introduction to objects)
- MIT Introduction to Algorithms 学习笔记(四)
- 《Beginning Python From Novice to Professional》学习笔记三:List
- Udacity cs344-Introduction to Parallel Programming学习笔记-第二单元
- CentOS学习笔记1:< yum提示another app is currently holding the yum lock;waiting for it to exit >
- Java学习笔记(1)Introduction to Computers, Programs, and Java
- MSDN Kernel-Mode Driver Architecture学习笔记(1)——Introduction to Windows Drivers(2)
- <Introduction to discrete event systems> 学习笔记4-3
- python nltk 学习笔记(5) Learning to Classify Text
- 2011斯坦福大学iOS应用开发教程学习笔记(第一课)MVC.and.Introduction.to.Objective-C
- MIT Introduction to Algorithms 学习笔记(五)
- (一)Data Science Specialization学习笔记