Skip to content

Commit b383424

Browse files
committed
numpy库常用方法
1 parent 1377f34 commit b383424

8 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[codestyle]
2+
indentation = True
3+
4+
[main]
5+
version = 0.1.0
6+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[encoding]
2+
text_encoding = utf-8
3+
4+
[main]
5+
version = 0.1.0
6+

PythonDemo/.spyproject/vcs.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[vcs]
2+
use_version_control = False
3+
version_control_system =
4+
5+
[main]
6+
version = 0.1.0
7+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[workspace]
2+
restore_data_on_startup = True
3+
save_data_on_exit = True
4+
save_history = True
5+
save_non_project_files = False
6+
7+
[main]
8+
version = 0.1.0
9+
recent_files = ['C:\\Users\\Administrator\\.spyder-py3\\temp.py', 'C:\\Users\\Administrator\\Desktop\\PythonDemo\\test.py', 'C:\\Users\\Administrator\\Desktop\\PythonDemo\\test2.py', 'D:\\Anaconda3\\lib\\site-packages\\ncmbot\\core.py', 'D:\\Anaconda3\\lib\\site-packages\\psutil\\__init__.py']
10+

PythonDemo/numpy/arraymethod.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Mon Mar 12 14:51:47 2018
4+
5+
@author: Administrator
6+
@description: numpy库操纵数组的一些方法
7+
"""
8+
import numpy as np
9+
10+
from numpy.matlib import randn
11+
#定义数组类型
12+
arr3 = np.array([1, 2, 3], dtype=np.float64)
13+
arr4 = np.array([1, 2, 3], dtype=np.int32)
14+
print(arr3.dtype)#float64
15+
print(arr4.dtype)#int32
16+
#转换数组类型
17+
arr5 = np.array([1, 2, 3, 4, 5])
18+
print(arr5.dtype) #int32
19+
arr6 = arr5.astype(np.float64) #将数组5的数据类型转换为float64
20+
print(arr6.dtype) #float64
21+
#数组间以及数组与数之间的运算
22+
arr = np.array([[1., 2., 3.], [4., 5., 6.]])
23+
print(arr * arr) #相应元素相乘,输出为[[1. 4. 9.] [16. 25. 36.]]
24+
print(arr - arr) #相应元素相减,输出为[[0. 0. 0.] [0. 0. 0.]]
25+
#创建二维数组,取值,修改值
26+
arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
27+
print(arr2d[2]) #数组标号从0开始,输出第三行元素:[7 8 9]
28+
#创建三维数组
29+
arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) #2个二维数组
30+
old_values = arr3d[0].copy() #将第1个二维数组拷贝至old_values
31+
print(old_values) #输出:[[1 2 3], [4 5 6]]
32+
arr3d[0] = 3 #将第1个二维数组的元素都修改为3
33+
arr3d[1] = 3 #将第2个二维数组的元素都修改为3
34+
print(arr3d) #输出全为3的三维数组
35+

PythonDemo/numpy/creatArray.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Mon Mar 12 14:13:32 2018
4+
5+
@author: Snailclimb
6+
@description: numpy库创建数组的一些方法
7+
"""
8+
import numpy as np
9+
10+
print("创建一维数组:")
11+
data1 = [3, 3.3, 9, 5, 6]
12+
arr1 = np.array(data1)
13+
print(arr1)
14+
15+
print("创建二维数组:")
16+
data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
17+
arr2 = np.array(data2)
18+
print(arr2)
19+
# Type of data in array.
20+
print("输出第一个数组的数据类型:",arr1.dtype)
21+
print("输出第二个数组的维数:",arr2.ndim)
22+
print("输出第三个数组的形状:",arr2.shape)
23+
#用zeros函数创建数组
24+
print("np.zeros(10)创建10个元素都是0的一维数组: ")
25+
print(np.zeros(10))
26+
print("np.zeros((3, 6))创建3行6列都是0的二维数组:")
27+
print(np.zeros((3, 6)))
28+
#用Empty函数创建数组,其初始值为乱值
29+
print("np.empty((3,6))创建3行6列都是0的二维数组:")
30+
print(np.empty((3,6)))
31+
#用arrange函数创建数组
32+
print("np.arange(9)创建一维数组:")
33+
print(np.arange(9))

PythonDemo/test.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# -*- coding: utf-8 -*-
2+
3+
'''
4+
Created on 2018年1月21日
5+
6+
@author: Administrator
7+
'''
8+
# drawtree.py
9+
10+
from turtle import Turtle
11+
12+
13+
def tree(plist, l, a, f):
14+
""" plist is list of pens
15+
l is length of branch
16+
a is half of the angle between 2 branches
17+
f is factor by which branch is shortened
18+
from level to level."""
19+
if l > 5: #
20+
lst = []
21+
for p in plist:
22+
p.forward(l) # 沿着当前的方向画画Move the turtle forward by the specified distance, in the direction the turtle is headed.
23+
q = p.clone() # Create and return a clone of the turtle with same position, heading and turtle properties.
24+
p.left(a) # Turn turtle left by angle units
25+
q.right(a) # turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
26+
lst.append(p) # 将元素增加到列表的最后
27+
lst.append(q)
28+
tree(lst, l * f, a, f)
29+
30+
31+
def main():
32+
p = Turtle()
33+
p.color("green")
34+
p.pensize(5)
35+
# p.setundobuffer(None)
36+
p.hideturtle() # Make the turtle invisible. It’s a good idea to do this while you’re in the middle of doing some complex drawing,
37+
# because hiding the turtle speeds up the drawing observably.
38+
# p.speed(10)
39+
# p.getscreen().tracer(1,0)#Return the TurtleScreen object the turtle is drawing on.
40+
p.speed(10)
41+
# TurtleScreen methods can then be called for that object.
42+
p.left(90) # Turn turtle left by angle units. direction 调整画笔
43+
44+
p.penup() # Pull the pen up – no drawing when moving.
45+
p.goto(0, -200) # Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle’s orientation.
46+
p.pendown() # Pull the pen down – drawing when moving. 这三条语句是一个组合相当于先把笔收起来再移动到指定位置,再把笔放下开始画
47+
# 否则turtle一移动就会自动的把线画出来
48+
49+
# t = tree([p], 200, 65, 0.6375)
50+
t = tree([p], 200, 65, 0.6375)
51+
52+
53+
main()

PythonDemo/test2.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# -*- coding: utf-8 -*-
2+
3+
print("sss")
4+
import psutil
5+
print(psutil.cpu_count()) # CPU逻辑数量
6+
print(psutil.cpu_times()) #统计CPU的用户/系统/空闲时间:

0 commit comments

Comments
 (0)