python学习

“人生苦短,我用python”

这几日折腾搭建了python平台,主要安装了numpy,scipy,matplotlib,scikit-learn这几个包。

先来一个小程序练练手。

 

from numpy import *
import sklearn
import random
from sklearn import linear_model

# load dataset
infile = "C:\\Users\\Administrator\\Desktop\\1.txt"
fr = open(infile)
lines = fr.readlines()
numberOfLines = len(lines)
src = []
result = []
for line in lines:
    listFromLine = line.strip().split('\t')
    # map all elements to float
    floatLine = map(float, listFromLine)
    src.append(floatLine)
    
src = array(src)
row, col = src.shape
for loop in range(0, 10):
    
    # shuffle the sample
    random.shuffle(src)
    
    trainFeature = src[0 : row / 2, 0 : -1]
    trainLabel = src[0 : row / 2, -1]
    testFeature = src[row / 2 : row, 0 : -1]
    testLabel = src[row / 2 : row, -1]
    testNumber = len(testLabel)
    
    # train
    clf = linear_model.LogisticRegression()
    clf.fit(trainFeature, trainLabel)
    
    # test
    Y = clf.predict(testFeature)

    # predict answer
    right = 0
    for i in range(0, testNumber):
        if Y[i] == testLabel[i]:
            right = right + 1
    result.append(float(right) / float(testNumber) * 100)
    
print sum(result) / len(result)

 

  

 

整理的一些python学习资料

一.python入门

1.Python教程,廖雪峰

2.Python初学者,GithHub上的PythonShare

3.学习Python的11个资源

4.Python正则表达式指南

二.爬虫

1.Python爬虫教程大全

2.零基础自学用Python3开发网络爬虫(一)(2,3,4)

三.机器学习

1.scilit-learn主页

 

 

 

posted @ 2014-11-11 19:57  七年之后  阅读(502)  评论(0编辑  收藏  举报