kaggle titanic 入门实例 基于性别的预测
2016-01-07 10:19
281 查看
#coding:utf-8 #https://www.kaggle.com/c/titanic/details/getting-started-with-python import csv as csv import numpy as np csv_file_object = csv.reader(open('./csv/train.csv', 'rb')) header = csv_file_object.next() data = [] for row in csv_file_object: data.append(row) data = np.array(data) #print data[0::,1] #第二列的全部 women_only_stats = data[0::,4] == "female" men_only_stats = data[0::,4] != "female" women_onboard = data[women_only_stats,1].astype(np.float) men_onboard = data[men_only_stats,1].astype(np.float) proportion_women_survived = \ np.sum(women_onboard) / np.size(women_onboard) proportion_men_survived = \ np.sum(men_onboard) / np.size(men_onboard) print 'Proportion of women who survived is %s' % proportion_women_survived print 'Proportion of men who survived is %s' % proportion_men_survived test_file = open('./csv/test.csv', 'rb') test_file_object = csv.reader(test_file) header = test_file_object.next() prediction_file = open("genderbasedmodel.csv", "wb") prediction_file_object = csv.writer(prediction_file) prediction_file_object.writerow(["PassengerId", "Survived"]) for row in test_file_object: # For each row in test.csv if row[3] == 'female': # is it a female, if yes then prediction_file_object.writerow([row[0],'1']) # predict 1 else: # or else if male, prediction_file_object.writerow([row[0],'0']) # predict 0 test_file.close() prediction_file.close()
相关文章推荐
- Android中的Handler机制
- 8. String to Integer (atoi)
- jQuery常用总结
- Python时间,日期,时间戳之间转换
- 时间服务器
- HAProxy的编译安装配置
- EasyUi心得
- TCP接受和发送程序以及长连接的处理方法
- 将博客搬至CSDN
- cookie显示上次访问时间
- Java 8:如何使用流方式查询数据库?
- 同志们再等等,三四月份跳槽加薪才最多
- android 任务栈启动模式
- 多线程的简单介绍与了解
- C/C++的就业,发展方向
- 把APK安装到SD卡和TF卡实现方案
- css3 transform, transition, animation区别和使用场景
- Understanding Steering Behaviors: Queue
- 邮件开发:复杂邮件的一个示例
- 给初学编程的人的新年干货