码迷,mamicode.com
首页 > 其他好文 > 详细

鸢尾花数据读取的总结

时间:2018-12-13 21:19:41      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:.data   end   []   基本   one   fit   strip   方法   tin   

1、手写最基本读取
f = open(‘8.iris.data‘,‘r‘,encoding=‘utf-8‘)
x = []
y = []
for d in f:
d = d.strip()
if not d:
continue
d = d.split(‘,‘)
x.append(list(map(float,d[:2])))
y.append((d[-1]))
y = np.array(list(map(iris_type,y)))
x = np.array(x)


2、使用python自带库csv
x = []
y = []
f = open(‘8.iris.data‘, ‘r‘, encoding=‘utf-8‘)
d = csv.reader(f)
for line in d:
if not d:
continue
x.append(list(map(float, line[:2])))
y.append(line[-1])
y = np.array(list(map(iris_type, y)))
x = np.array(x)


3、使用numpy的方法读入
data = np.loadtxt(‘8.iris.data‘, converters={4: iris_type},dtype=float, delimiter=‘,‘,encoding=‘utf-8‘)
print(type(data))
x, y = np.split(data, (4,), axis=1)
x = x[:, :2]
print(x)
print(y.shape)
print(y.ravel().shape)
#split返回的y的shape是(150, 1),但是训练时的函数fit用的y是(150,),所以需要拉平y.ravel()
#4、使用pandas的方法读入
data = pd.read_csv(‘8.iris.data‘,converters={4: iris_type},header=None)
x = data.loc[:,:1].values
y = data.loc[:,4].values
print(x.shape)
print(y.shape)
 

鸢尾花数据读取的总结

标签:.data   end   []   基本   one   fit   strip   方法   tin   

原文地址:https://www.cnblogs.com/liangzaikaituozhe/p/10116409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!