这里的代码是截取的我的代码片段,或许难以阅读,有不理解的地方欢迎交流
x=[]
x.append(img_path[j])
img_ndarray=numpy.asarray(img,dtype='float64')/256 #将图像转化为数组并将像素转化到0-1之间
data[d-1]=numpy.ndarray.flatten(img_ndarray) #将图像的矩阵形式转化为一维数组保存到data中
data_label=data_label.astype(numpy.int) #将标签转化为int类型
print random.randint(12, 20) #生成的随机数n: 12 <= n <= 20
y = 2.5
print type(y) # 输出 "<type 'float'>"
print y ** 2 # 输出 "6.25"
#Python中没有 x++ 和 x-- 的操作符。
#布尔型:Python用英语实现了所有的布尔逻辑,而不是操作符(&&和||等)。
t = True
f = False
print type(t) # 输出 "<type 'bool'>"
print t and f # 逻辑与 AND; prints "False"
print t or f # 逻辑或 OR; prints "True"
print not t # 逻辑非 NOT; prints "False"
print t != f # 逻辑异或 XOR; prints "True"
#字符串:Python对字符串的支持非常棒。
hello = 'hello' # 字符串可以用单引号,也可以用双引号
world = "world"
print hello # Prints "hello"
print len(hello) # 字符串长度 prints "5"
hw = hello + ' ' + world # 字符串连接
print hw # prints "hello world"
hw12 = '%s %s %d' % (hello, world, 12) # 格式化输出
print hw12 # prints "hello world 12"
#字符串对象有一系列有用的方法,比如:
s = "hello"
print s.capitalize() # 首字母大写; prints "Hello"
print s.upper() # 全部大写; prints "HELLO"
print s.rjust(7) # 右端对齐; prints " hello"
print s.center(7) # 中间对齐; prints " hello "
print s.replace('l', '(ell)') # 字符串中字符替换; prints "he(ell)(ell)o"
print ' world '.strip() # 去除首尾空格; prints "world"
import numpy as np
#一维数组
a = np.array([1, 2, 3])
print type(a) # Prints "<type 'numpy.ndarray'>"
print a.shape # Prints "(3,)"
print a[0], a[1], a[2] # Prints "1 2 3"
a[0] = 5 # 改变数组中元素
print a # Prints "[5, 2, 3]"
#二维数组
b = np.array([[1,2,3],[4,5,6]])
print b.shape # Prints "(2, 3)"
print b[0, 0], b[0, 1], b[1, 0] # Prints "1 2 4"
#其他一些建立数组方法
a = np.zeros((2,2)) # Create an array of all zeros
print a # Prints "[[ 0. 0.]
# [ 0. 0.]]"
b = np.ones((1,2)) # Create an array of all ones
print b # Prints "[[ 1. 1.]]"
c = np.full((2,2), 7) # Create a constant array
print c # Prints "[[ 7. 7.]
# [ 7. 7.]]"
d = np.eye(2) # Create a 2x2 identity matrix
print d # Prints "[[ 1. 0.]
# [ 0. 1.]]"
e = np.random.random((2,2)) # Create an array filled with random values
print e # Might print "[[ 0.91940167 0.08143941]
# [ 0.68744134 0.87236687]]"
import numpy as np
# 创建shape (3, 4)的二维数组
# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
b = a[:2, 1:3]
# [[2 3]
# [6 7]]
print a[0, 1] # Prints "2"
b[0, 0] = 77 # b[0, 0] 和 a[0, 1]是同一个数据
print a[0, 1] # Prints "77"
row_r1 = a[1, :]
row_r2 = a[1:2, :]
print row_r1, row_r1.shape # Prints "[5 6 7 8] (4,)"
print row_r2, row_r2.shape # Prints "[[5 6 7 8]] (1, 4)"
col_r1 = a[:, 1]
col_r2 = a[:, 1:2]
print col_r1, col_r1.shape # Prints "[ 2 6 10] (3,)"
print col_r2, col_r2.shape # Prints "[[ 2]
# [ 6]
# [10]] (3, 1)"
a = np.array([[1,2], [3, 4], [5, 6]])
print a[[0, 1, 2], [0, 1, 0]] # Prints "[1 4 5]"
# 等价于print np.array([a[0, 0], a[1, 1], a[2, 0]]) # Prints "[1 4 5]"
import numpy as np
x = np.array([1, 2])
print x.dtype # Prints "int64"
x = np.array([1.0, 2.0])
print x.dtype # Prints "float64"
x = np.array([1, 2], dtype=np.int64)
print x.dtype # Prints "int64"
import numpy as np
x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)
print x + y
print np.add(x, y)
# [[ 6.0 8.0]
# [10.0 12.0]]矩阵加
print x - y
print np.subtract(x, y)
# [[-4.0 -4.0]
# [-4.0 -4.0]]矩阵减
print x * y
print np.multiply(x, y)
# [[ 5.0 12.0]
# [21.0 32.0]]矩阵乘
print x / y
print np.divide(x, y)
# [[ 0.2 0.33333333]
# [ 0.42857143 0.5 ]]矩阵除
print np.sqrt(x)
# [[ 1. 1.41421356]
# [ 1.73205081 2. ]]矩阵开方
#和MATLAB不同,*是元素逐个相乘,而不是矩阵乘法。在Numpy中使用dot来进行矩阵乘法:
import numpy as np
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
v = np.array([9,10])
w = np.array([11, 12])
# 矩阵乘法,输出219
print v.dot(w)
print np.dot(v, w)
# 矩阵乘法,输出[29 67]
print x.dot(v)
print np.dot(x, v)
# 矩阵乘法,输出
# [[19 22]
# [43 50]]
print x.dot(y)
print np.dot(x, y)
#Numpy提供了很多计算数组的函数,其中最常用的一个是sum:
import numpy as np
x = np.array([[1,2],[3,4]])
print np.sum(x) # 计算所有元素和; prints "10"
print np.sum(x, axis=0) # 计算列元素和; prints "[4 6]"
print np.sum(x, axis=1) # 计算行元素和; prints "[3 7]"
#除了计算,我们还常常改变数组或者操作其中的元素。其中将矩阵转置是常用的一个,在Numpy中,使用T来转置矩阵:
import numpy as np
x = np.array([[1,2], [3,4]])
print x # Prints "[[1 2]
# [3 4]]"
print x.T # Prints "[[1 3]
# [2 4]]"
#如果我们想要把一个向量加到矩阵的每一行,我们可以这样做:
import numpy as np
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = np.empty_like(x) # 创建与x矩阵shape相同的空矩阵
for i in range(4):
y[i, :] = x[i, :] + v
#或者
vv = np.tile(v, (4, 1)) # Stack 4 copies of v on top of each other
print vv # Prints "[[1 0 1]
# [1 0 1]
# [1 0 1]
# [1 0 1]]"
y = x + v
#广播机制可以使我们直接运算
y = x + v
print y
# [[ 2 2 4]
# [ 5 5 7]
# [ 8 8 10]
# [11 11 13]]