Skip to content

Commit dadbeb9

Browse files
committed
commit
1 parent 72a4eae commit dadbeb9

File tree

2 files changed

+92
-45
lines changed

2 files changed

+92
-45
lines changed

.idea/workspace.xml

Lines changed: 29 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python基础代码/科学计算基础学习/Nmupy基础练习.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
11
import numpy as np
22

3+
"""
4+
创建ndarray数组
5+
6+
"""
7+
# Method 1: 基于list或tuple
8+
# 一维数组
9+
#
10+
# 基于list
11+
# arr1 = np.array([1,2,3,4])
12+
# print(arr1)
13+
#
14+
# 基于tuple
15+
# arr_tuple = np.array((1,2,3,4))
16+
# print(arr_tuple)
17+
#
18+
# 二维数组 (2*3)
19+
# arr2 = np.array([[1,2,4], [3,4,5]])
20+
# print(arr2)
21+
#
22+
# Method 2: 基于np.arange
23+
# 一维数组
24+
# arr1 = np.arange(5)
25+
# print(arr1)
26+
#
27+
# 二维数组
28+
# arr2 = np.array([np.arange(3), np.arange(3)])
29+
# print(arr2)
30+
#
31+
# Method 3: 基于arange以及reshape创建多维数组
32+
# 创建三维数组
33+
# arr = np.arange(24).reshape(2,3,4)
34+
# print(arr)
35+
'''请注意:arange的长度与ndarray的维度的乘积要相等,即 24 = 2X3X4'''
36+
37+
'''函数resize()的作用跟reshape()类似,但是会改变所作用的数组,相当于有inplace=True的效果'''
38+
a = np.arange(12).reshape(4, 3)
39+
# print(a.reshape(3,4))
40+
# print(a)
41+
# print(a.resize(6,2))
42+
# print(a)
43+
#
44+
# print(b.ravel())
45+
46+
47+
'''ravel()和flatten(),将多维数组转换成一维数组,如下:
48+
两者的区别在于返回拷贝(copy)还是返回视图(view),flatten()返回一份拷贝,
49+
需要分配新的内存空间,对拷贝所做的修改不会影响原始矩阵,
50+
而ravel()返回的是视图(view),会影响原始矩阵。
51+
'''
52+
# b = np.arange(12).reshape(4,3)
53+
# b.ravel()
54+
# b.ravel()[2]=10
55+
# print(b)
56+
# b.flatten()[2]=15
57+
# print(b)
58+
59+
60+
61+
62+
63+
64+
65+
366
# weight = [65.4, 59.2, 63.6, 88.4, 68.7] #体重列表
467
# height = [1.73, 1.68, 1.71, 1.89, 1.79] #身高列表
568
#

0 commit comments

Comments
 (0)