Python for ML 学习笔记 (Not finished)

Python 函数

DataFrame

DataFrame 是一个类型,每行每列都可以添加标签,也可以利用标签来提取行或列,比nparray更容易组织和修改数据。要index的时候要用 to_numpy() 转换为numpy array类型。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Create DataFrame
data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [24, 27, 22],
    'Score': [85, 90, 88]
}
df = pd.DataFrame(data, index=['A', 'B', 'C'])
print(df)

# Select the column with index 'Name'
column_1 = df['Name']
print(column_1)

row_1 = df.loc['A']
print(row_1)

array = df.to_numpy()
print(array)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
      Name  Age  Score
A    Alice   24     85
B      Bob   27     90
C  Charlie   22     88
A      Alice
B        Bob
C    Charlie
Name: Name, dtype: object
Name     Alice
Age         24
Score       85
Name: A, dtype: object
[['Alice' 24 85]
 ['Bob' 27 90]
 ['Charlie' 22 88]]

The : Technique

  • : 表示所有
  • 2: 表示从右往左取2个
  • :5 表示从左向右取5个

For example:

1
2
3
4
5
6
import numpy as np

a = np.array([[1, 2, 3], [0, 0, 0], [2, 3, 4]])

b = a[2:, :3] # Works only for numpy array
print(b)
1
[[2 3 4]]

The column_stack()row_stack() 函数

仅仅是将两个nparray按照行或列拼在一起

1
2
3
4
5
6
7
import numpy as np

# Demonstrate column_stack() function
x = np.array([1, 2, 3, 4, 5])
y = np.array([6, 7, 8, 9, 10])
z = np.row_stack((x, y))
print(z)
1
2
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]]
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy