Numpy数组组合
运行 :Jupyter
import numpy
world_alcohol = numpy.genfromtxt('world_alcohol.txt',delimiter",",dtype= str)
print(type(world_alcohol))
print(world_alcohol)
print(help(numpy.genfromtxt))//查询函数使用方法或者校核函数
import numpy
vector = numpy.array([1,2,3,4])
print(vector.shape)//.shape查询矩阵的行列元素,
vector.dtype//读取类型
print(vector[0,3])//左闭右开,结果为123
创建3*3数组后进行数组组合
水平组合、垂直组合、深度组合、列组合、行组合
创建3*3数组
from numpy import *
a = arange(9).reshape(3,3)
print(a)
数组运算
b = 2 * a
print(b)
print(a.shape)
print(b.shape)
//水平组合, 将ndarray对象构成的元组作为参数,传给hstack函数
ch = hstack((a,b))
print(ch)
//或者用concatenate函数实现同样的效果
d = concatenate((a,b), axis=1)
print(d)
//垂直组合,同样需要构造一个元组作为参数,只不过是传给vstack函数
v = vstack((a,b))
print(v)
//用concatenate函数实现同样的效果,此时axis参数设置为0,这也是axis参数的默认值
cv = concatenate((a,b), axis=0)
print(cv)
深度组合
import numpy as np
print('\na:')
a = np.arange(9).reshape(3,3)
print(a)
print('\nb:')
b=np.array([[11,22,33],[44,55,66],[77,88,99]])
print(b)
c = np.dstack((a,b))
print('\nc:')
print(np.dstack((a,b)))
列组合、行组合
import numpy as np
oned = arange(2)
print('\noned:')
print(oned)
twice_oned = 2 * oned
print('\ntwice_oned:')
print(twice_oned)
c = column_stack((oned, twice_oned))
print('\nc:')
print(c)
d = row_stack((oned, twice_oned))
print('\nd:')
print(d)
结果
oned:
[0 1]
twice_oned:
[0 2]
c:
[[0 0]
[1 2]]
d:
[[0 1]
[0 2]]