单特征标记文件的读取

#神经网络l#chap2_linear_regression

从txt中读取数据,train_set:300 by 2, testset:200

函数:

string.split: 返回一个list,list内存分开的元素

map:   返回迭代器,该迭代器(map)也是可以迭代的;传参为一个函数和iterable,对iterable内的所有元素进行映射

zip: 返回迭代器,指向tuples;传参为多个argument sequence或者iterables, *与list结合可以Unpack这个list

单个参数时对每个元素生成一个tuple

1 d=zip(p)
2 list(d)
3 [([1, 2, 3],), ([4, 5, 6],)]

多参数时,对每个参数内的第 i 个元素生成一个tuple

1 d = zip(*p)
2 list(d)
3 [(1, 4), (2, 5), (3, 6)]

可以使用zip(*zipped)来unzip,如

1 zipped  = zip(a,b,c)   #a, b, c为维度相同的tuple
2 a1, b1, c1=zip(*zipped)
3 
4 a1 == a, b1==b, c1 ==c    

>> True True True

 

 

asarray: numpy的方法,参数为lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarray,可以用于更改一下ndarray的dtype

>>> a = np.array([1, 2], dtype=np.float32)
>>> np.asarray(a, dtype=np.float32) is a
True
>>> np.asarray(a, dtype=np.float64) is a
False

 

读数据的代码:关键部分相当于 map(float, list), zip(map1, map2, ..., map300), map是iterable(也是iterator,指向一系列的),并unpack zipped,xs和ys都是tuple

1 def load_data(filename):
2     """载入数据。"""
3     xys = []
4     with open(filename, 'r') as f:
5         for line in f:
6             xys.append(map(float, line.strip().split()))
7         xs, ys = zip(*xys)
8         return np.asarray(xs), np.asarray(ys)

 

posted on 2019-09-22 09:45  Vitavi  阅读(163)  评论(0编辑  收藏  举报

导航