读教程笔记http://cs231n.github.io/python-numpy-tutorial/
关于python基础知识:
1.string:
#~len(str):数组长度;
#~sprint style string formats:
hello="hello"
world='world'
str2="%s %s %d" %(hello,world,12)
print str2 #输出hello world 12
#~string可调用函数
s="hello"
print s.capitalize() #首字母大写
print s.upper()#将全部小写转化为大写
print s.rjust(9) #右对齐字符串,左边使用空格填充
print s.center(9) #给定字符串总数,string居中,其余用空格填充
print s.replace('l',"(ell)") #使用右边参数替代前面参数
输出结果为:
Hello
HELLO
hello
hello
he(ell)(ell)o
2.containers ;
#list
x=[3,2,"foo"] #create list
lastx=x.pop() #pop函数返回并删除list的最后一个元素
print lastx
print x
x.append("bar") #添加元素
print x
输出结果为:
foo
[3, 2]
[3, 2, 'bar']
#添加元素可以使用append和extend函数,但是两者用法不同:
列表是以类的形式实现的。“创建”列表实际上是将一个类实例化。因此,列表有多种方法可以操作。 1.列表可包含任何数据类型的元素,单个列表中的元素无须全为同一类型。 2.append()方法向列表的尾部添加一个新的元素。只接受一个参数。 3.extend()方法只接受一个列表作为参数,并将该参数的每个元素都添加到原有的列表中。
**********************************************************************************************************************
append()用法示例:
>>> mylist = [1,2,0,'abc']
>>> mylist
[1, 2, 0, 'abc']
>>> mylist.append(4)
>>> mylist
[1, 2, 0, 'abc', 4]
>>> mylist.append('haha')
>>> mylist
[1, 2, 0, 'abc', 4, 'haha']
>>>
**********************************************************************************************************************
extend()用法示例:
>>> mylist
[1, 2, 0, 'abc', 4, 'haha']
>>> mylist.extend(['lulu'])
>>> mylist
[1, 2, 0, 'abc', 4, 'haha', 'lulu']
>>> mylist.extend(['123123','lalalala'])
>>> mylist
[1, 2, 0, 'abc', 4, 'haha', 'lulu', '123123', 'lalalala']
>>> mylist.extend([111111,222])
>>> mylist
[1, 2, 0, 'abc', 4, 'haha', 'lulu', '123123', 'lalalala', 111111, 222]
animals=['cat','dog','monky'] #带索引序号的list遍历函数enumerate
for idx,animal in enumerate(animals):
print '索引%d %s' %(idx+1,animal)
输出结果为:
索引1 cat
索引2 dog
索引3 monky
nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print even_squares #输出[0, 4, 16]
#dictionary
<pre name="code" class="python">d = {'cat': 'cute', 'dog': 'furry'}
print d.get('monkey', 'N/A') # Get an element with a default; prints "N/A"
print d.get('fish', 'N/A') # Get an element with a default; prints "wet"
</pre>d={'person':2,'cat':4,'spider':8}for animal in d: print '%s has %d legs' %(animal,d[animal])<p></p><pre>
</pre><pre name="code" class="python"><pre name="code" class="python">输出结果为:
person has 2 legs
spider has 8 legs
cat has 4 legs
#如果想要遍历字典里面的索引和索引对应的值,使用iteritems函数
for animal,legs in d.iteritems():
print '%s has %d legs' %(animal,legs)
#将list生成dictionary
nums=[0,1,2,3,4]
even_num={x: x**x for x in nums if x%2==0}
#set
<pre name="code" class="python">animals = {'cat', 'dog'}
print 'cat' in animals # Check if an element is in a set; prints "True"
print len(animals) # Number of elements in a set; prints "3"
animals.add('cat') # Adding an element that is already in the set does nothing
print len(animals) # Prints "3"
animals.remove('cat') # Remove an element from a set
from math import sqrt
nums = {int(sqrt(x)) for x in range(30)}
print nums # Prints "set([0, 1, 2, 3, 4, 5])"
d = {(x, x + 1): x for x in range(10)} # Create a dictionary with tuple keys
t = (5, 6) # Create a tuple
print type(t) # Prints "<type 'tuple'>"
print d[t] # Prints "5"
print d[(1, 2)] # Prints "1"
Python常用函数:
1.关于随机函数
times = np.arange(0, duration_s, time_step_s) # start, end, step与matlab的参数顺序不同,这个函数允许输入为float类型的。
vt = np.random.rand(np.shape(times)[0])
这个函数是可以生成某种大小的0到1之间的随机数组(矩阵),参数可以是数组的大小。
np.random.random((2, 3))array([[ 0.58941311, 0.00996924, 0.30159729],
[ 0.64386549, 0.94079774, 0.92460379]])
生成0到1之间的大小为2*3的矩阵
我也是傻了,浮点数的话可以先生成随机数然后再乘以最大值即可。
比如要生成0-200之间的随机数,先生成0-1之间的随机数然后再乘以200即可。
2. 关于符号函数
spikes[i, :] = np.signbit(vt - (spikes_pers * time_step_s))
这个函数是将数组中的负数置为false, 其他置为true。
而sign函数却是负数为-1, 0 为0 , 正数为1.
此外,上面一行代码中括号中的最后一项乘项是两个实数项相乘,vt是向量,虽然numpy的array可以实现“向量-实数=向量”,但是python的list不行,list必须采取上面的signbit函数。当操作对象为numpy的array时,上面一行代码与下面的一行代码是等价的。
spikes[i, :] = (spikes_pers * time_step_s) > vt