文章最前: 我是Octopus,这个名字来源于我的中文名–章鱼;我热爱编程、热爱算法、热爱开源。所有源码在我的个人github
;这博客是记录我学习的点点滴滴,如果您对 Python、Java、AI、算法有兴趣,可以关注我的动态,一起学习,共同进步。
首先要导入numpy的包
- score = np.array([[80, 89, 86, 67, 79],
[78, 97, 89, 67, 81],
[90, 94, 78, 67, 74],
[91, 91, 90, 67, 69],
[76, 87, 75, 67, 86],
[70, 79, 84, 67, 84],
[94, 92, 93, 67, 64],
[86, 85, 83, 67, 80]])
- type(score)
import random
import time
# 生成一个大数组
python_list = []
for i in range(100000000):
python_list.append(random.random())
ndarray_list = np.array(python_list)
# 原生pythonlist求和
t1 = time.time()
a = sum(python_list)
t2 = time.time()
d1 = t2 - t1
# ndarray求和
t3 = time.time()
b = np.sum(ndarray_list)
t4 = time.time()
d2 = t4 - t3
d1
d2
- score.shape # (8, 5)
- score.ndim
- score.size
- score.dtype
- score.itemsize
a = np.array([[1,2,3],[4,5,6]])
b = np.array([1,2,3,4])
c = np.array([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])
a.shape
a.type
1 生成0和1的数组 np.zeros(shape=(3, 4), dtype="float32")
2.np.ones(shape=[2, 3], dtype=np.int32)
1.np.array()
2. data1 = np.array(score)
np.linspace(0, 10, 5)
np.arange(0, 11, 5)
stock_change = np.random.normal(loc=0, scale=1, size=(8, 10))
stock_change.reshape((10, 8))
stock_change.T
stock_change.astype("int32")
temp = np.array([[1, 2, 3, 4],[3, 4, 5, 6]])
np.unique(temp)np.
stock_change = np.random.normal(loc=0, scale=1, size=(8, 10))
逻辑判断, 如果涨跌幅大于0.5就标记为True 否则为False
stock_change > 0.5
np.all(stock_change[0:2, 0:5] > 0)
#判断前5只股票这段期间是否有上涨的
np.any(stock_change[:5, :] > 0)
#判断前四个股票前四天的涨跌幅 大于0的置为1,否则为0
temp = stock_change[:4, :4]
np.where(temp > 0, 1, 0)
np.where(np.logical_and(temp > 0.5, temp < 1), 1, 0)
np.logical_or(temp > 0.5, temp < -0.5)
np.where(np.logical_or(temp > 0.5, temp < -0.5), 11, 3)
temp.max(axis=0)
np.max(temp, axis=-1)
np.argmax(temp, axis=-1)
arr = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])
arr = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])
arr / 10
arr1 = np.array([[1, 2, 3, 2, 1, 4], [5, 6, 1, 2, 3, 1]])
arr2 = np.array([[1], [3]])
arr1 + arr2
arr1 * arr2
arr1 / arr2
data = np.array([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
data_mat = np.mat([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
weights = np.array([[0.3], [0.7]])
np.matmul(data, weights)
a = stock_change[:2, 0:4]
b = stock_change[4:6, 0:4]
a.shape
a.reshape((-1, 2))
np.hstack((a, b))
np.concatenate((a, b), axis=1)
np.vstack((a, b))
np.concatenate((a, b), axis=0)
data = np.genfromtxt("test.csv", delimiter=",")