机器学习&全栈我爱编程

python django开发教程 & 机器学习

2018-05-03  本文已影响63人  采香行处蹙连钱

title: python语法练习

参考阮一峰等多个文件用来练习python基本语法

[TOC]

import文件
  1. 参考文献

  2. 常规导入

    import sys
    import os, sys, time 
    import sys as system #重命名
    import urllib.error #某些子模块必须要使用点标记法才能导入。
    
    如果两个python文件位于同一文件夹下,可以直接通过import导入另一个.py文件,并且使用其中的函数
    
  3. 使用from语句导入

    很多时候你只想要导入一个模块或库中的某个部分:

    from functools import lru_cache 
    from os import path, walk, unlink  #从一个包中导入多项
    
  4. 相对导入

  5. 可选导入

  6. 本地导入

if name == "main": 作用
  1. 参考文献

  2. 上代码

    # Threading example
    import time, thread
    
    def myfunction(string, sleeptime, lock, *args):
        while 1:
            lock.acquire()
            time.sleep(sleeptime)
            lock.release()
            time.sleep(sleeptime)
    if __name__ == "__main__":
        lock = thread.allocate_lock()
        thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))
        thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
    
  3. if __name__ == '__main__' 我们简单的理解就是: 如果模块是被直接运行的,则代码块被运行,如果模块是被导入的,则代码块不被运行

  4. over

python格式化输出
  1. 打印

    print ("和 is name is %s"%("gaolong")) #整数
    print ("He is %d years old"%(25)) #字符串
    print ("His height is %f m"%(1.83))
    
  2. over

python多线程
  1. 参考文献

  2. 多线程的使用

    
    def move(func):
        for i in range(5):
            print ("I was at the %s! %s" %(func, ctime()))
    
    def move(func):
        for i in range(2):
            print ("I was at the %s ! %s" %(func, ctime()))
            sleep(4)
    threads = []
    
    t1 = threading.Thread(target=music, args=(u'aiqingmaima'))
    threads.append(t1)
    t2 = threading.Thread(target=move,args=(u'阿凡达',))
    threads.append(t2)
    
    if __name__ == '__main__':
        for t in threads:
            t.setDaemon(True)
            t.start()
    
        print ("all over %s" %ctime())
    
  3. over

python魔术方法
  1. 参考http://pycoders-weekly-chinese.readthedocs.io/en/latest/issue6/a-guide-to-pythons-magic-methods.html
python global
  1. 使用方法

    a = 5  
      
    def test():  
        global a  
     #此处声明,告诉执行引擎:我要用全局变量a,不要整成局部的了!  
        a = 1  
        print 'In test func: a = %d' % a  
      
    test()  
    print 'Global a = %d' % a  
    首先:python使用的变量,在默认情况下一定是用局部变量。
    其次:python如果想使用作用域之外的全局变量,则需要加global前缀。
    
  2. over

requests框架学习

这个框架的作用类似于iOS的post请求、get请求,即url请求,获取数据。数据类型可能是json,可能是html网页。

  1. 参考文献

    requests基本用法: https://zhuanlan.zhihu.com/p/26681429

  2. 基本用法

    使用pycharm+virtualenv, 导入requests框架。requests抓取网页。

    #made by schiller
    
    import requests
    
    payload = dict(catCircleCategoryId=2, sortType=1)
    r = requests.post("http://app.yirimao.com/cat-circle/list", data=payload)
    # print(r.text)
    
    def getHtmlText(url):
        try:
            r = requests.get(url, timeout=30)
            r.raise_for_status()
            r.encoding = r.apparent_encoding
            return r.text
        except:
            return "Something wrong!"
    

if name == 'main':
print(getHtmlText("http://www.aidu.com"))


3. **kwargs参数

4. 两个常用控制访问的参数

- 假设我们需要在GET请求里自定义一个header头文件:

  ```python
  import requests

  hd = {'User-agent':'123'}
  r = requests.get('http://www.baidu.com', headers=hd)
  print(r.request.headers)
  '''
  OUT:
  {'User-agent': '123', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive
  '}
  '''
  • 假设我们要自定义一个代理池

    pxs = { 'http': 'http://user:pass@10.10.10.1:1234',
            'https': 'https://10.10.10.1:4321' }
    r = requests.get('http://www.baidu.com', proxies=pxs)
    
  • over

  1. 详解response对象

    import requests
    r = requests.get("http://www.baidu.com")
    
    '''
    Response(self)
    
    The :class:Response <Response> object, which contains a server's response to an HTTP request.
    
    '''
    #HTTP请求的返回状态,比如,200表示成功,404表示失败
    print (r.status_code)
    #HTTP请求中的headers
    print (r.headers)
    #从header中猜测的响应的内容编码方式 
    print (r.encoding)
    #从内容中分析的编码方式(慢)
    print (r.apparent_encoding)
    #响应内容的二进制形式
    print (r.content)
    
    '''
    status_code:200 
    
    headers:
    {'Server': 'bfe/1.0.8.18', 'Date': 'Tue, 02 May 2017 12:01:47 GMT', 'Content-Type': 'text/html', 'La
    st-Modified': 'Mon, 23 Jan 2017 13:28:27 GMT', 'Transfer-Encoding': 'chunked', 'Connection': 'Keep-A
    live', 'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Pragma': 'no
    -cache', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Content-Encoding':
    'gzip'}
    
    encoding: ISO-8859-1
    
    apparent_encoding:utf-8
    '''
    
  2. over

beautifulsoup4 html 解析器

beautifulsoup4是一个强大的html解析器

  1. 参考文献

    https://zhuanlan.zhihu.com/p/26691931

  2. 基本用法

    from bs4 import BeautifulSoup
    import myrequest
    
    html = myrequest.getHtmlText('http://www.baidu.com')
    
    soup = BeautifulSoup(html, 'html.parser')
    
    print(soup.prettify())
    
    def getSoupValue():
        title = soup.title
        name = soup.title.name
        titleString = soup.title.string
        titleParentString = soup.title.parent.name
        ptag = soup.p
        aAll = soup.find_all('a')
        print("title = %s, name = %s, titlestring = %s,titlePrentstring = %s, ptag = %s, a = %s"
              % (title, name, titleString, titleParentString, ptag, aAll))
    

if name == 'main':
getSoupValue()


3. bs4库 是解析、遍历、维护、“标签树“的功能库。

4. over
bs4解析器 lxml

网络爬虫的最终目的就是过滤选取网络信息,最重要的部分可以说是解析器。解析器的优劣决定了爬虫的速度和效率。bs4库除了支持我们上文用过的‘html.parser’解析器外,还支持很多第三方的解析器,下面我们来对他们进行对比分析。

  1. 参考文献

    https://zhuanlan.zhihu.com/p/26691931

  2. 基本用法

    import bs4
    
    html_doc = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title"><b>The Dormouse's story</b></p>
    
    <p class="story">Once upon a time there were three little sisters; and their names were
    <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
    <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    and they lived at the bottom of a well.</p>
    
    <p class="story">...</p>
    """
    
    #soup = bs4.BeautifulSoup(open(''), 'lxml')
    soup = bs4.BeautifulSoup(html_doc, 'lxml')
    print(soup.prettify())
    
    def dealWithTags():
        head = soup.head
        title = soup.title
        body = soup.body.b
        tag = soup.find_all('a')
        nee = tag[1]
    
  3. over

爬虫实践:获取百度贴吧内容

over

python经典编码问题

参考文献:https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20

  1. http://www.jb51.net/article/17560.htm
  2. http://blog.csdn.net/zuyi532/article/details/8851316
python scrapy爬虫框架入门

参考文献:

  1. 使用virtaulenv和pycharm导入Scrapy
  2. 使用简书案例,新建jianshuSpider.py文件
  3. 配置setting.py
  4. 新建main.py用来执行python脚本
  5. 也可以通过virtaulenv的命令,cd到简书项目中,执行scrapy startproject jianshu 脚本来执行该项目
  6. over
爬取简书文档数据

参考文献:http://www.jianshu.com/p/61911e00abd0

over

使用scrapy框架爬取天气资讯

参考文献:https://zhuanlan.zhihu.com/p/26885412

步骤:

  1. 安装scrapy

    直接通过pycharm 导入该框架即可
    
  2. 生成 uuu scrapy项目

    $ cd .virtualenvs/py3env_test #进入该虚拟目录
    $ source bin/activate # 激活env
    $ cd 
    $ cd python_test 
    (py3env_test) ➜  python_test git:(master) ✗ scrapy startproject spiderDemo # 使用scrapy快速建立新项目,使用的是py3env_test虚拟目录的python框架
    $ cd spiderDemo 
    $ 再进入pycharm就能看到自己新建的scrapy项目了。
    
  3. 编写items.py

    
    import scrapy
    

class WeatherItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
date = scrapy.Field()
week = scrapy.Field()
img = scrapy.Field()
temperature = scrapy.Field()
weather = scrapy.Field()
wind = scrapy.Field()


4. 编写spider

```python
在spiders目录下新建:SZtianqi.py文件
# -*- coding: utf-8 -*-
import scrapy
from weather.items import WeatherItem


class SztianqiSpider(scrapy.Spider):
    name = "SZtianqi"
    # 我们修改一下host,使得Scrapy可以爬取除了苏州之外的天气
    allowed_domains = ["tianqi.com"]

    # 建立需要爬取信息的url列表
    start_urls = []

    # 需要爬的城市名称
    citys = ['nanjing', 'suzhou', 'shanghai']

    # 用一个很简答的循环来生成需要爬的链接:
    for city in citys:
        start_urls.append('http://' + city + '.tianqi.com')

    def parse(self, response):
        '''
        筛选信息的函数:
        date = 今日日期
        week = 星期几
        img = 表示天气的图标
        temperature = 当天的温度
        weather = 当天的天气
        wind = 当天的风向
        '''

        # 先建立一个列表,用来保存每天的信息
        items = []

        # 找到包裹着每天天气信息的div
        sixday = response.xpath('//div[@class="tqshow1"]')

        # 循环筛选出每天的信息:
        for day in sixday:
            # 先申请一个weatheritem 的类型来保存结果
            item = WeatherItem()

            # 观察网页,知道h3标签下的不单单是一行str,我们用trick的方式将它连接起来
            date = ''
            for datetitle in day.xpath('./h3//text()').extract():
                date += datetitle
            
            item['date'] = date

            item['week'] = day.xpath('./p//text()').extract()[0]
            item['img'] = day.xpath(
                './ul/li[@class="tqpng"]/img/@src').extract()[0]
            tq = day.xpath('./ul/li[2]//text()').extract()
            # 我们用第二种取巧的方式,将tq里找到的str连接
            item['temperature'] = ''.join(tq)
            item['weather'] = day.xpath('./ul/li[3]/text()').extract()[0]
            item['wind'] = day.xpath('./ul/li[4]/text()').extract()[0]
            items.append(item)
        return items

  1. 编写pipelines

    pipelines.py是用来处理收尾爬虫抓到的数据的,一般情况下,我们会将数据存到本地:
    1、文本形式:最基本
    2、json格式:方便调用
    3、数据库:数据量大
    
    此处使用json格式
    class W2json(object):
        def process_item(self, item, spider):
            '''
            讲爬取的信息保存到json
            方便其他程序员调用
            '''
            base_dir = os.getcwd()
            filename = base_dir + '/data/weather.json'
    
            # 打开json文件,向里面以dumps的方式吸入数据
            # 注意需要有一个参数ensure_ascii=False ,不然数据会直接为utf编码的方式存入比如:“/xe15”
            with codecs.open(filename, 'a') as f:
                line = json.dumps(dict(item), ensure_ascii=False) + '\n'
                f.write(line)
    
            return item
    
  2. 编写settings.py

    我们需要在Settings.py将我们写好的PIPELINE添加进去,
    scrapy才能够跑起来
    这里只需要增加一个dict格式的ITEM_PIPELINES,
    数字value可以自定义,数字越小的优先处理

    BOT_NAME = 'weather'
    
    SPIDER_MODULES = ['weather.spiders']
    NEWSPIDER_MODULE = 'weather.spiders'
    ITEM_PIPELINES = {'weather.pipelines.W2json': 400}
    
    ROBOTSTXT_OBEY = True
    
  3. 让项目跑起来

    $ cd weather 项目目录
    $ 在weather目录下新建一个 data/weather.json文件用来收录抓取的数据
    $ scrapy crawl SZtianqi    
    
  4. over

over

需要解决的问题
  1. virtaulenv 使用pip列出virtaulenv安装过的第三方库
  2. python爬虫框架scrapy
  3. python爬取有关于猫的一切资料
python序列化

python序列化,将字典转成json或者将json转成字典

参考:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143192607210600a668b5112e4a979dd20e4661cc9c97000

  1. import hashlib
    import json
    
    md5 = hashlib.md5()
    md5.update('how to use md5 in python'.encode('utf-8'))
    print(md5.hexdigest())
    
    d = dict(name='Bob', age=20,score=90)
    print(json.dumps(d)) #此处是dumps而不是dump
    
    json_str = '{"name": "Bob", "age": 20, "score": 90, "result":true}'
    print(json.loads(json_str))
    
    adict = json.loads(json_str)
    print(adict)
    
    #定义类
    class Student(object):
        def __init__(self, name, age, score):
            self.name = name
            self.age = age
            self.score = score
    
    def student2dict(std):
        return {
            'name': std.name,
            'age': std.age,
            'score': std.score
        }
    
    a = Student('Bob', 20, 88) 
    print(json.dumps(a, default=student2dict)) #将对象转成json
    
    print(json.dumps(a, default=lambda  obj: obj.__dict__)) #将任意对象转成json,定义一个匿名函数
    
    
  2. over

over

高阶函数

map、reduce、filter、sorted

  1. 参考文献

    http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014317852443934a86aa5bb5ea47fbbd5f35282b331335000

  2. 案例

    over
    
  3. over

python Django入门

采用virtualenv引入django。

参考文档:https://www.lijinlong.cc/django/djxs/1920.html

经典入门参考:https://www.jianshu.com/p/b0be1bc89f74?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

自学入门:https://code.ziqiangxuetang.com/django/django-intro.html

  1. 打开pycharm,打开工程tieba,本工程采用的env是virtualenv。来自 ~/.virtualenvs/ py3env_test

  2. 可以用Pycharm/Preferences/ 去下载最新的django。我下载的是2.1版本。下载好的django会存放到这个virtualenv的site-packages目录下。

  3. 然后进入到py3env_test , 运行source bin/activate,激活这个虚拟环境

  4. ok,激活后,执行pip3 install django。由于我用pycharm导入的这个框架,所以此处提醒我已经安装好了。

  5. 使用命令创建项目:django-admin.py startproject myfirstDjangoProject 。这个项目最后会存放到 ~/ 目录下。

  6. 可以指定创建目录到python_test

  7. cd 到 工程 myfirstDjangoProject,然后执行 python manage.py runserver

  8. 打开浏览器:

    Django version 2.0.1, using settings 'myfirstDjangoProject.settings'
    Starting development server at http://127.0.0.1:8000/
    
  9. 创建mydjango_app ,并且,创建views.py, 创建第一个django应用。

  10. 总结一句:第一步,打开虚拟环境;第二步,创建项目(如果已经有项目则cd到项目) ;第三步,到工程里运行项目

  11. 去掉提示 migration提醒

    
    #Django 1.7.x 以下版本
    python manage.py syncdb
     
    # Django 1.7.x 以及上要用
    python manage.py migrate
    

  12. over

Django视图与网址以及进阶

参考文档:https://code.ziqiangxuetang.com/django/django-views-urls.html

参考文档2:https://code.ziqiangxuetang.com/django/django-views-urls2.html

  1. 新建一个APP,名称为learn python manage.py startapp learn ``# learn 是一个app的名称

  2. 把我们新定义的app加到settings.py中的****INSTALL_APPS中

    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
     
        'learn',
    )
    
  3. 我们在learn这个目录中,把views.py打开,修改其中的源代码,改成下面的

    # coding:utf-8
    from django.http import HttpResponse
     
     
    def index(request):
        return HttpResponse(u"欢迎光临 自强学堂!")
    
  4. 定义视图函数相关的url

    from django.conf.urls import url
    from django.contrib import admin
    from learn import views as learn_views  # new
     
     
    urlpatterns = [
        url(r'^$', learn_views.index),  # new
        url(r'^admin/', admin.site.urls),
    ]
    
  5. 修改视图views.py文件为

    from django.shortcuts import render
    from django.http import HttpResponse
     
    def add(request):
        a = request.GET['a']
        b = request.GET['b']
        c = int(a)+int(b)
        return HttpResponse(str(c))
    
  6. 修改为

    from django.conf.urls import url
    from django.contrib import admin
    from calc import views as calc_views
     
     
    urlpatterns = [
        url(r'^add/$', calc_views.add, name='add'),  # 注意修改了这一行
        url(r'^admin/', admin.site.urls),
    ]
    
  7. 打开网址:http://127.0.0.1:8000/add/

    http://127.0.0.1:8000/add/?a=4&b=5

  8. 已经学到QuerySet API 这一章节

python细微语法总结
  1. 函数

    1. 函数、参数、高阶函数、None
    
    位置参数
    def add_end(L=None):
        if L is None:
            L = []
        L.append('END')
        return L
      
      可变参数
      def calc(*numbers):
        sum = 0
        for n in numbers:
            sum = sum + n * n
        return sum
      关键字参数 **args
      可变参数允许你传入0个或任意个参数,这些可变参数在函数调用时自动组装为一个tuple。而关键字参数允许你传入0个或任意个含参数名的参数,这些关键字参数在函数内部自动组装为一个dict
      
      命名关键字参数,如果要限制关键字参数的名字,就可以用命名关键字参数。用*分隔符 分割
      
      如果函数定义中已经有了一个可变参数,后面跟着的命名关键字参数就不再需要一个特殊分隔符*了
      
      命名关键字参数必须传入参数名,这和位置参数不同
      
     //总结  
      必选参数、默认参数、位置参数、可变参数、关键字参数、命名关键字参数、参数组合
      参数定义的顺序必须是:必选参数、默认参数、可变参数、命名关键字参数和关键字参数。
    2. list、tuple
    3. 高级特性
    切片
    L = list(range(100))
    L[0:3] 同L[:3]
    L[1:3]
    L[-2:] 
    L[:10:2] 前10个数,每两个取一个:[0, 2, 4, 6, 8]
    'ABCDEFG'[::2]
    迭代
    列表生成式
    生成器
    迭代器
    4. 函数式编程
    map
    reduce
    filter
    sorted
    返回函数
    匿名函数: list(map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
    
     装饰器
    def now():
    ...     print('2015-3-25')
    假设我们要增强now()函数的功能,比如,在函数调用前后自动打印日志,但又不希望修改now()函数的定义,这种在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator)。
    #装饰器
    def log(func):
        def wrapper(*arg, **kw):
            print('call %s():' % func.__name__)
            return func(*arg, **kw)
        return wrapper
    
    @log
    def now():
        print('2018-4-26')
    
    now()
    
    偏函数
    
  2. 模块

    1. 使用模块
    任何模块代码的第一个字符串都被视为模块的文档注释
    __author__ = 'Michael Liao'
    2. 运行
    if __name__=='__main__':
        test()
    当我们在命令行运行hello模块文件时,Python解释器把一个特殊变量__name__置为__main__,而如果在其他地方导入该hello模块时,if判断将失败。
    
    3. 作用域
    public:正常的函数和变量名是公开的(public),可以被直接引用,比如:abc,x123,PI等
    
    __xxx__这样的变量是特殊变量,可以被直接引用,但是有特殊用途,比如上面的__author__,__name__就是特殊变量
    
    类似_xxx和__xxx这样的函数或变量就是非公开的(private),不应该被直接引用,比如_abc,__abc等
    
  3. 面向对象

    1. __init__方法的第一个参数永远是self,表示创建的实例本身,因此,在__init__方法内部,就可以把各种属性绑定到self,因为self就指向创建的实例本身
    2.类: 要定义一个方法,除了第一个参数是self外,其他和普通函数一样。
    3. 访问限制
    4. 继承+多态
    5. 获取对象信息
    type()
    isinstance()
    
    6. __slots__
    Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该class实例能添加的属性
    class Student(object):
        __slots__ = ('name', 'age') # 用tuple定义允许绑定的属性名称
    7. 使用@property
    class Student(object):
    
        @property
        def score(self):
            return self._score
    
        @score.setter
        def score(self, value):
            if not isinstance(value, int):
                raise ValueError('score must be an integer!')
            if value < 0 or value > 100:
                raise ValueError('score must between 0 ~ 100!')
            self._score = value
            
            
    8. 多重继承、枚举类
    
  4. 正则表达式

    • \d可以匹配一个数字,\w可以匹配一个字母或数字,\s匹配空格

    • .可以匹配任意字符

    • *表示任意个字符(包括0个),用+表示至少一个字符,用?表示0个或1个字符,用{n}表示n个字符,用{n,m}表示n-m个字符

    • '-'是特殊字符,在正则表达式中,要用'\'转义

    • 要做更精确地匹配,可以用[]表示范围

    • [0-9a-zA-Z\_]可以匹配一个数字、字母或者下划线

    • A|B可以匹配A或B

    • ^表示行的开头,^\d表示必须以数字开头。

    • $表示行的结束,\d$表示必须以数字结束

    • re模块

    • 强烈建议使用Python的r前缀,就不用考虑转义的问题了

      def matchResult(test):
          if re.match(r'^\d{3}\-\d{3,8}$', test):
              print('ok,hhh,match ok')
          else:
              print('failed')
      
      matchResult('010-12345')
      
      分组:
      贪婪匹配:
      编译:
      
      
  5. over

python常用内建模块
  1. datetime & collections & base64 & struct & hashlib & hmac & zip

    zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表
    >>>a = [1,2,3]
    >>> b = [4,5,6]
    >>> zipped = zip(a,b)  
    [(1, 4), (2, 5), (3, 6)]
    
  2. itertools & contextlib & contextlib & urllib & XML & HTMLParser

    
    
  3. argparse

    该模块用于解析通过命令行运行的参数
    $ python a.py 10 20 
    解析10 20这些基本参数
    该模块有基本用法,
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--batch_size', default=100, type=int, help='batch size')
    #然后
    args = parser.parse_args()
    
  4. python内置函数:http://www.runoob.com/python/python-built-in-functions.html

    
    
  5. over

模块
  1. 连接数据库

    #MySQL官方提供了mysql-connector-python驱动,但是安装的时候需要给pip命令加上参数--allow-external
    import mysql.connector
    def connectMysql():
        conn = mysql.connector.connect(user='root', password='gaolong', database='yirimao_2018_4')
        cursor = conn.cursor()
        sql = 'select * from user where id = 144'
        cursor.execute(sql)
        print(cursor.fetchall())
    
    connectMysql()
    
    
  2. 异步IO

  3. over

pandas库学习
  1. 参考文献:http://codingpy.com/article/a-quick-intro-to-pandas/

    import pandas as pd
    
    def load_data(y_name='Species'):
        CSV_COLUMN = ['SepalLength', 'SepalWidth',
                        'PetalLength', 'PetalWidth', 'Species']
        path = '/Users/gl/Desktop/iris_training.csv'
        train = pd.read_csv(path, names=CSV_COLUMN, header=0)
        #pop删除某列,返回删除的那一列的数据,此处为简单赋值
        train_x, train_y = train, train.pop(y_name)
        return (train_x, train_y)
    
    print(load_data())
    
    接口:
    df.tail() 
    df.head()
    df['rain_octsep']或者df.rain_octsep
    布尔过滤:df.rain_octsep < 1000
    索引:df.iloc[30]
    对数据集应用函数:
    def base_year(year):
        base_year = year[:4]
        base_year= pd.to_datetime(base_year).year
        return base_year
    
    df['year'] = df.water_year.apply(base_year)
    df.head(5)
    数据集结构操作
    合并数据集:
    使用Pandas快速作图:
    
  2. ove

Numpy快速入门
  1. 参考文献:http://codingpy.com/article/an-introduction-to-numpy/

  2. Python 学习数据科学或者机器学习,就必须学习 NumPy

    1. 创建二位数组、矩阵
    2. 多维数组切片
    3. 数组属性
    4. 
    
  3. over

TensorFlow入门

机器学习新手入门:https://www.tensorflow.org/get_started/get_started_for_beginners

  1. 模型与训练

    模型即特征与标签之间的关系。对于鸢尾花问题,模型定义了花萼和花瓣测量值与鸢尾花品种之间的关系。一些简单的模型可以用几行代数进行描述;比较复杂的机器学习模型则包含大量的交错数学函数和参数,以至于难以从数学角度进行总结
    监督式学习
    非监督式学习
    
  2. 获取示例程序

    1. 安装TensorFlow(使用virtualenv直接导入该pachkage)
    2. 安装Pandas库
    3. 获取代码:git clone https://github.com/tensorflow/models
    4. cd models/samples/core/get_started/
    5. python premade_estimator.py
    
  3. 高阶API:EstimatorDataset

  4. 程序流程

    1. 导入和解析数据集。
    2. 创建特征列以描述数据。
    3. 选择模型类型。
    4. 训练模型。
    5. 评估模型的效果。
    6. 让经过训练的模型进行预测。
    

  5. over

上一篇下一篇

猜你喜欢

热点阅读