
Vue element 先clonehttps://github.com/ElementUI/element-cooking-starter.git的例子在目录下运行 npm install 安装所需要的nodejs包再运行 npm run dev 可以看到 element的简单例子element 按钮 <el-button @click="visible = true">Button</el-button> element dialog <el-dialog :visible.sync="visible" title="Hello world"> <p>Try Element</p> </el-dialog> element 组件安装: npm i element-ui -S element组件导入在main.js中:完整引入: import Vue from 'vue'; import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; import App from './App.vue'; Vue.use(ElementUI); new Vue({ el: '#app', render: h => h(App) }); 按需引入 暂时不需要 element全局引入 Vue.use(Element, { size: 'small', zIndex: 3000 }); 主题位置: element-ui/lib/theme-chalk/ element带入淡出: el-fade-in-linear el-fade-in 通过transition标签来调用例子 <template> <div> <el-button @click="show = !show">Click Me</el-button> <div style="display: flex; margin-top: 20px; height: 100px;"> <transition name="el-fade-in-linear"> <div v-show="show" class="transition-box">.el-fade-in-linear</div> </transition> <transition name="el-fade-in"> <div v-show="show" class="transition-box">.el-fade-in</div> </transition> </div> </div> </template> <script> export default { data: () => ({ show: true }) } </script> <style> .transition-box { margin-bottom: 10px; width: 200px; height: 100px; border-radius: 4px; background-color: #409EFF; text-align: center; color: #fff; padding: 40px 20px; box-sizing: border-box; margin-right: 20px; } </style> zoom 缩放 el-zoom-in-center el-zoom-in-top el-zoom-in-bottom 通过transition标签来调用 <template> <div> <el-button @click="show2 = !show2">Click Me</el-button> <div style="display: flex; margin-top: 20px; height: 100px;"> <transition name="el-zoom-in-center"> <div v-show="show2" class="transition-box">.el-zoom-in-center</div> </transition> <transition name="el-zoom-in-top"> <div v-show="show2" class="transition-box">.el-zoom-in-top</div> </transition> <transition name="el-zoom-in-bottom"> <div v-show="show2" class="transition-box">.el-zoom-in-bottom</div> </transition> </div> </div> </template> <script> export default { data: () => ({ show2: true }) } </script> <style> .transition-box { margin-bottom: 10px; width: 200px; height: 100px; border-radius: 4px; background-color: #409EFF; text-align: center; color: #fff; padding: 40px 20px; box-sizing: border-box; margin-right: 20px; } </style> collapse展开 el-collapse-transition 直接使用el-collapse-transition 标签来调用 例子: <script> export default { data: () => ({ show3: true }) } </script> <style> .transition-box { margin-bottom: 10px; width: 200px; height: 100px; border-radius: 4px; background-color: #409EFF; text-align: center; color: #fff; padding: 40px 20px; box-sizing: border-box; margin-right: 20px; } </style> 主要学习了element 的引入和element的组件的使用全部的组件使用根据需求看官网就可以了https://element.eleme.cn/#/zh-CN/component/installation
唉被安排了一个Vue前端任务,菜鸟Vue教程学习笔记,基本半天能入门vue安装: npm install vue npm install --global vue-cli vue新建工程: vue install webpack test1 vue 运行: npm run dev 打开http://localhost:8080能看到vue页面 主要修改src文件实例化Vue: var vm= new Vue( {} ) 例子: <div id="vue_det"> <h1>site : {{site}}</h1> <h1>url : {{url}}</h1> <h1>{{details()}}</h1> </div> <script type="text/javascript"> var vm = new Vue({ el: '#vue_det', data: { site: "菜鸟教程", url: "www.runoob.com", alexa: "10000" }, methods: { details: function() { return this.site + " - 学的不仅是技术,更是梦想!"; } } }) </script> vue实例中的el 是dom元素的iddata 定义了属性methods 用于定义函数,return 来返回函数值{{}}用于输出对像的属性和返回值vue中数据的拷贝是浅拷贝 <div id="vue_det"> <h1>site : {{site}}</h1> <h1>url : {{url}}</h1> <h1>Alexa : {{alexa}}</h1> </div> <script type="text/javascript"> // 我们的数据对象 var data = { site: "菜鸟教程", url: "www.runoob.com", alexa: 10000} var vm = new Vue({ el: '#vue_det', data: data }) // 它们引用相同的对象! document.write(vm.site === data.site) // true document.write("<br>") // 设置属性也会影响到原始数据 vm.site = "Runoob" document.write(data.site + "<br>") // Runoob // ……反之亦然 data.alexa = 1234 document.write(vm.alexa) // 1234 </script> v-html 用于输出html代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title> <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script> </head> <body> <div id="app"> <div v-html="message"></div> </div> <script> new Vue({ el: '#app', data: { message: '<h1>菜鸟教程</h1>' } }) </script> </body> </html> v-bind 用于进行属性的绑定: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 测试实例 - 菜鸟教程(runoob.com)</title> </head> <style> .class1{ background: #444; color: #eea; } </style> <body> <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script> <div id="app"> <label for="r1">修改颜色</label><input type="checkbox" v-model="use1" id="r1"> <br><br> <div v-bind:class="{'class1': use1}"> v-bind:class 指令 </div> </div> <script> new Vue({ el: '#app', data:{ use1: false } }); </script> </body> 用vue实例中的use来判断div标签的属性是否绑定class1vue中判断用v-if <div id="app"> <p v-if="seen">现在你看到我了</p> </div> <script> new Vue({ el: '#app', data: { seen: true } }) </script> v-bind:+html属性能更新html的属性 <div id="app"> <pre><a v-bind:href="url">菜鸟教程</a></pre> </div> <script> new Vue({ el: '#app', data: { url: 'http://www.runoob.com' } }) </script> v-model 指令用来在 input、select、text、checkbox、radio 等表单控件元素上创建双向数据绑定,根据表单上的值,自动更新绑定的元素的值。按钮的事件我们可以使用 v-on 监听事件,并对用户的输入进行响应。在用户输入完后就能改变属性等 <div id="app"> <p>{{ message }}</p> <input v-model="message"> </div> <script> new Vue({ el: '#app', data: { message: 'Runoob!' } }) </script> 字符反转的操作在revermessage中定义了一个函数,用v-on:click监听是否点击 如果点击执行函数 <div id="app"> <p>{{ message }}</p> <button v-on:click="reverseMessage">反转字符串</button> </div> <script> new Vue({ el: '#app', data: { message: 'Runoob!' }, methods: { reverseMessage: function () { this.message = this.message.split('').reverse().join('') } } }) </script> Vue自定义过滤器方法:在大括号中{{message|capitalize}}在v-bind指令中 过滤器可以级联{{ message | filterA | filterB }}例子: <script> new Vue({ el: '#app', data: { message: 'runoob' }, filters: { capitalize: function (value) { if (!value) return '' value = value.toString() return value.charAt(0).toUpperCase() + value.slice(1) } } }) </script> Vue的if 用v-ifelse用v-elseelse-if用v-else-if例子: <div v-if="type === 'A'"> <div v-else-if="type === 'B'"> <div v-else-if="type === 'C'"> <div v-else> v-show用来判断是否展示元素: <h1 v-show="ok">Hello!</h1> Vue for循环:v-for 需要用site in sites写法,sites表示资源组,site表示资源迭代的别名使用实例: <div id="app"> <ol> <li v-for="site in sites"> {{ site.name }} </li> </ol> </div> <script> new Vue({ el: '#app', data: { sites: [ { name: 'Runoob' }, { name: 'Google' }, { name: 'Taobao' }, {name:"wahahahah"} ] } }) </script> v-for 中的site也可以是对象的属性: <div id="app"> <ul> <li v-for="value in object"> {{ value }} </li> </ul> </div> <script> new Vue({ el: '#app', data: { object: { name: '菜鸟教程', url: 'http://www.runoob.com', slogan: '学的不仅是技术,更是梦想!' } } }) </script> v-for用键加属性的方式: <li v-for="(value, key) in object"> (属性,键,索引) <li v-for="(value, key, index) in object"> 循环整数: <li v-for="n in 10"> Vue中的计算属性 computed,只有getter例子 <div id="app"> <p>原始字符串: {{ message }}</p> <p>计算后反转字符串: {{ reversedMessage }}</p> </div> <script> var vm = new Vue({ el: '#app', data: { message: 'Runoob!' }, computed: { // 计算属性的 getter reversedMessage: function () { // `this` 指向 vm 实例 return this.message.split('').reverse().join('') } } }) </script> computed vs methods我们可以使用 methods 来替代 computed,效果上两个都是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值。而使用 methods ,在重新渲染的时候,函数总会重新调用执行。 computer setter例子: var vm = new Vue({ el: '#app', data: { name: 'Google', url: 'http://www.google.com' }, computed: { site: { // getter get: function () { return this.name + ' ' + this.url }, // setter set: function (newValue) { var names = newValue.split(' ') this.name = names[0] this.url = names[names.length - 1] } } } }) // 调用 setter, vm.name 和 vm.url 也会被对应更新 vm.site = '菜鸟教程 http://www.runoob.com'; document.write('name: ' + vm.name); document.write('<br>'); document.write('url: ' + vm.url); vue 监听属性watch 用于监听数值变化,采取动作 <div id = "app"> <p style = "font-size:25px;">计数器: {{ counter }}</p> <button @click = "counter++" style = "font-size:25px;">点我</button> </div> <script type = "text/javascript"> var vm = new Vue({ el: '#app', data: { counter: 1 } }); vm.$watch('counter', function(nval, oval) { alert('计数器值的变化 :' + oval + ' 变为 ' + nval + '!'); }); </script> oval为原来的数值,nval为现在的数值watch 同时监听两个数据做出相应: <div id = "computed_props"> 千米 : <input type = "text" v-model = "kilometers"> 米 : <input type = "text" v-model = "meters"> </div> <p id="info"></p> <script type = "text/javascript"> var vm = new Vue({ el: '#computed_props', data: { kilometers : 0, meters:0 }, methods: { }, computed :{ }, watch : { kilometers:function(val) { this.kilometers = val; this.meters = this.kilometers * 1000 }, meters : function (val) { this.kilometers = val/ 1000; this.meters = val; } } }); // $watch 是一个实例方法 vm.$watch('kilometers', function (newValue, oldValue) { // 这个回调将在 vm.kilometers 改变后调用 document.getElementById ("info").innerHTML = "修改前值为: " + oldValue + ",修改后值为: " + newValue; }) </script> 事件监听 v-on例子: <div id="app"> <button v-on:click="counter += 1">增加 1</button> <p>这个按钮被点击了 {{ counter }} 次。</p> </div> <script> new Vue({ el: '#app', data: { counter: 0 } }) </script> v-on 可以接受一个定义的方法,使用javascript的内联语句v-on的一些处理事件细节的修饰符处理DOM事件,通过点摆事指令后最来调用修饰符。 <!-- 阻止单击事件冒泡 --> <a v-on:click.stop="doThis"></a> <!-- 提交事件不再重载页面 --> <form v-on:submit.prevent="onSubmit"></form> <!-- 修饰符可以串联 --> <a v-on:click.stop.prevent="doThat"></a> <!-- 只有修饰符 --> <form v-on:submit.prevent></form> <!-- 添加事件侦听器时使用事件捕获模式 --> <div v-on:click.capture="doThis">...</div> <!-- 只当事件在该元素本身(而不是子元素)触发时触发回调 --> <div v-on:click.self="doThat">...</div> <!-- click 事件只能点击一次,2.1.4版本新增 --> <a v-on:click.once="doThis"></a> v-on 在监听键盘事件时提供了按键的修饰符方法: <!-- 只有在 keyCode 是 13 时调用 vm.submit() --> <input v-on:keyup.13="submit"> 支持的按键别名: <!-- 同上 --> <input v-on:keyup.enter="submit"> <!-- 缩写语法 --> <input @keyup.enter="submit"> 全部的按键别名: .enter .tab .delete (捕获 "删除" 和 "退格" 键) .esc .space .up .down .left .right .ctrl .alt .shift .meta v-model 实现数据的双向绑定v-model: 实例中的属性数据,在组件中改变值会改变实例中的数据例子: <div id="app"> <p>单个复选框:</p> <input type="checkbox" id="checkbox" v-model="checked"> <label for="checkbox">{{ checked }}</label> <p>多个复选框:</p> <input type="checkbox" id="runoob" value="Runoob" v-model="checkedNames"> <label for="runoob">Runoob</label> <input type="checkbox" id="google" value="Google" v-model="checkedNames"> <label for="google">Google</label> <input type="checkbox" id="taobao" value="Taobao" v-model="checkedNames"> <label for="taobao">taobao</label> <br> <span>选择的值为: {{ checkedNames }}</span> </div> <script> new Vue({ el: '#app', data: { checked : false, checkedNames: [] } }) </script> 选择例子: <div id="app"> <select v-model="selected" name="fruit"> <option value="">选择一个网站</option> <option value="www.runoob.com">Runoob</option> <option value="www.google.com">Google</option> </select> <div id="output"> 选择的网站是: {{selected}} </div> </div> <script> new Vue({ el: '#app', data: { selected: '' } }) </script> v-model修饰符.lazy 在输入时就于后台的数据进行同部,在change事件同步 <!-- 在 "change" 而不是 "input" 事件中更新 --> <input v-model.lazy="msg" > .number 自动将输入值转换为number.trim 过滤输入中首尾的空格Vue组件注册:注册一个全局组件语法格式如下: Vue.component(tagName, options) tagName 为组件名,options 为配置选项。注册后,我们可以使用以下方式来调用组件: <tagName></tagName> 注册全局组件例子: <div id="app"> <runoob></runoob> </div> <script> // 注册 Vue.component('runoob', { template: '<h1>自定义组件!</h1>' }) // 创建根实例 new Vue({ el: '#app' }) </script> 注册局部组件: <div id="app"> <runoob></runoob> </div> <script> var Child = { template: '<h1>自定义组件!</h1>' } // 创建根实例 new Vue({ el: '#app', components: { // <runoob> 将只在父模板可用 'runoob': Child } }) </script> prop组件用于数据传递,父组件的数据需要通过 props 把数据传给子组件,子组件需要显式地用 props 选项声明 "prop":例子: <div id="app"> <child message="hello!"></child> </div> <script> // 注册 Vue.component('child', { // 声明 props props: ['message'], // 同样也可以在 vm 实例中像 "this.message" 这样使用 template: '<span>{{ message }}</span>' }) // 创建根实例 new Vue({ el: '#app' }) </script> 动态组件:类似于用 v-bind 绑定 HTML 特性到一个表达式,也可以用 v-bind 动态绑定 props 的值到父组件的数据中。每当父组件的数据变化时,该变化也会传导给子组件: <div id="app"> <div> <input v-model="parentMsg"> <br> <child v-bind:message="parentMsg"></child> </div> </div> <script> // 注册 Vue.component('child', { // 声明 props props: ['message'], // 同样也可以在 vm 实例中像 "this.message" 这样使用 template: '<span>{{ message }}</span>' }) // 创建根实例 new Vue({ el: '#app', data: { parentMsg: '父组件内容' } }) </script> 例子: <div id="app"> <ol> <todo-item v-for="item in sites" v-bind:todo="item"></todo-item> </ol> </div> <script> Vue.component('todo-item', { props: ['todo'], template: '<li>{{ todo.text }}</li>' }) new Vue({ el: '#app', data: { sites: [ { text: 'Runoob' }, { text: 'Google' }, { text: 'Taobao' } ] } }) </script> Prop指定验证 Vue.component('example', { props: { // 基础类型检测 (`null` 意思是任何类型都可以) propA: Number, // 多种类型 propB: [String, Number], // 必传且是字符串 propC: { type: String, required: true }, // 数字,有默认值 propD: { type: Number, default: 100 }, // 数组/对象的默认值应当由一个工厂函数返回 propE: { type: Object, default: function () { return { message: 'hello' } } }, // 自定义验证函数 propF: { validator: function (value) { return value > 10 } } } }) type 可以是下面原生构造器: StringNumberBooleanFunctionObjectArray 组件自定义函数:例子: <div id="app"> <div id="counter-event-example"> <p>{{ total }}</p> <button-counter v-on:increment="incrementTotal"></button-counter> <button-counter v-on:increment="incrementTotal"></button-counter> </div> </div> <script> Vue.component('button-counter', { template: '<button v-on:click="incrementHandler">{{ counter }}</button>', data: function () { return { counter: 0 } }, methods: { incrementHandler: function () { this.counter += 1 this.$emit('increment') } }, }) new Vue({ el: '#counter-event-example', data: { total: 0 }, methods: { incrementTotal: function () { this.total += 1 } } }) </script> 自定义中 data如果时函数会返回对象的一个独立的拷贝,使用对象时会影响到别的实例例子: <div id="components-demo3" class="demo"> <button-counter2></button-counter2> <button-counter2></button-counter2> <button-counter2></button-counter2> </div> <script> var buttonCounter2Data = { count: 0 } Vue.component('button-counter2', { /* data: function () { // data 选项是一个函数,组件不相互影响 return { count: 0 } }, */ data: function () { // data 选项是一个对象,会影响到其他实例 return buttonCounter2Data }, template: '<button v-on:click="count++">点击了 {{ count }} 次。</button>' }) new Vue({ el: '#components-demo3' }) </script> vue自定义指令: 注册一个全局指令 v-focus, 该指令的功能是在页面加载时,元素获得焦点: <script> // 注册一个全局自定义指令 v-focus Vue.directive('focus', { // 当绑定元素插入到 DOM 中。 inserted: function (el) { // 聚焦元素 el.focus() } }) // 创建根实例 new Vue({ el: '#app' }) </script> 局部指令: <div id="app"> <p>页面载入时,input 元素自动获取焦点:</p> <input v-focus> </div> <script> // 创建根实例 new Vue({ el: '#app', directives: { // 注册一个局部的自定义指令 v-focus focus: { // 指令的定义 inserted: function (el) { // 聚焦元素 el.focus() } } } }) </script> 指令定义函数提供了几个钩子函数(可选):bind: 只调用一次,指令第一次绑定到元素时调用,用这个钩子函数可以定义一个在绑定时执行一次的初始化动作。inserted: 被绑定元素插入父节点时调用(父节点存在即可调用,不必存在于 document 中)。update: 被绑定元素所在的模板更新时调用,而不论绑定值是否变化。通过比较更新前后的绑定值,可以忽略不必要的模板更新(详细的钩子函数参数见下)。componentUpdated: 被绑定元素所在模板完成一次更新周期时调用。unbind: 只调用一次, 指令与元素解绑时调用。钩子函数参数钩子函数的参数有:el: 指令所绑定的元素,可以用来直接操作 DOM 。binding: 一个对象,包含以下属性:name: 指令名,不包括 v- 前缀。value: 指令的绑定值, 例如: v-my-directive="1 + 1", value 的值是 2。oldValue: 指令绑定的前一个值,仅在 update 和 componentUpdated 钩子中可用。无论值是否改变都可用。expression: 绑定值的表达式或变量名。 例如 v-my-directive="1 + 1" , expression 的值是 "1 + 1"。arg: 传给指令的参数。例如 v-my-directive:foo, arg 的值是 "foo"。modifiers: 一个包含修饰符的对象。 例如: v-my-directive.foo.bar, 修饰符对象 modifiers 的值是 { foo: true, bar: true }。vnode: Vue 编译生成的虚拟节点。oldVnode: 上一个虚拟节点,仅在 update 和 componentUpdated 钩子中可用。vue内部设定的函数以下实例演示了这些参数的使用: <div id="app" v-runoob:hello.a.b="message"> </div> <script> Vue.directive('runoob', { bind: function (el, binding, vnode) { var s = JSON.stringify el.innerHTML = 'name: ' + s(binding.name) + '<br>' + 'value: ' + s(binding.value) + '<br>' + 'expression: ' + s(binding.expression) + '<br>' + 'argument: ' + s(binding.arg) + '<br>' + 'modifiers: ' + s(binding.modifiers) + '<br>' + 'vnode keys: ' + Object.keys(vnode).join(', ') } }) new Vue({ el: '#app', data: { message: '菜鸟教程!' } }) </script> Vue混入 可以在外部定义对象等信息,用extend的方式加入Vue的实例中,如果 methods 选项中有相同的函数名,则 Vue 实例优先级会较高 var mixin = { methods: { hellworld: function () { document.write('HelloWorld 方法' + '<br>'); }, samemethod: function () { document.write('Mixin:相同方法名' + '<br>'); } } }; var vm = new Vue({ mixins: [mixin], methods: { start: function () { document.write('start 方法' + '<br>'); }, samemethod: function () { document.write('Main:相同方法名' + '<br>'); } } }); vm.hellworld(); vm.start(); vm.samemethod(); get请求: window.onload = function(){ var vm = new Vue({ el:'#box', data:{ msg:'Hello World!', }, methods:{ get:function(){ //发送get请求 this.$http.get('/try/ajax/ajax_info.txt').then(function(res){ document.write(res.body); },function(){ console.log('请求失败处理'); }); } } }); } post请求: window.onload = function(){ var vm = new Vue({ el:'#box', data:{ msg:'Hello World!', }, methods:{ post:function(){ //发送 post 请求 this.$http.post('/try/ajax/demo_test_post.php',{name:"菜鸟教程",url:"http://www.runoob.com"},{emulateJSON:true}).then(function(res){ document.write(res.body); },function(res){ console.log(res.status); }); } } }); } vue动态添加属性 vue.setvue动态删除属性 vue.delete <div id = "app"> <p style = "font-size:25px;">计数器: {{ products.id }}</p> <button @click = "products.id++" style = "font-size:25px;">点我</button> </div> <script type = "text/javascript"> var myproduct = {"id":1, name:"book", "price":"20.00"}; var vm = new Vue({ el: '#app', data: { counter: 1, products: myproduct } }); Vue.set(myproduct, 'qty', 1); console.log(vm); vm.$watch('counter', function(nval, oval) { alert('计数器值的变化 :' + oval + ' 变为 ' + nval + '!'); }); </script> <div id = "app"> <p style = "font-size:25px;">计数器: {{ products.id }}</p> <button @click = "products.id++" style = "font-size:25px;">点我</button> </div> <script type = "text/javascript"> var myproduct = {"id":1, name:"book", "price":"20.00"}; var vm = new Vue({ el: '#app', data: { counter: 1, products: myproduct } }); Vue.delete(myproduct, 'price'); console.log(vm); vm.$watch('counter', function(nval, oval) { alert('计数器值的变化 :' + oval + ' 变为 ' + nval + '!'); }); </script> 不喜勿喷
继上一片的内容,这片来·讲一下sklearn来进行简单的人脸识别,这里用的方法是pca和svm 先导入必要的包和数据集 import numpy as np import matplotlib.pyplot as plt from scipy import stats from sklearn.decomposition import PCA from sklearn.svm import SVC from sklearn import datasets lfw_people = datasets.fetch_lfw_people(min_faces_per_person=70, \ resize=0.4) sklearn的人脸数据集包含5千多个不同人的人脸,但有些人的人脸只包含一张, n_samples, h, w = lfw_people.images.shape print('height and width of images:', h, w) # The images in X have been collapsed into a 1D array # just like for the handwritten digits X = lfw_people.data # X.shape[0] tells you the number of images (faces); # this is the same as n_samples ahove # X.shape[1] gives the number of pixels for each image # or, "features" print('X.shape', X.shape) n_features = X.shape[1] # the label/target to predict is the id of the person -- y is an integer y = lfw_people.target # target_names are actually names target_names = lfw_people.target_names print('target_names.shape', target_names.shape) print('target_names', target_names) # n_classes gives the number of people # Different from the number of faces (n_samples)!! n_classes = target_names.shape[0] print("Total dataset size:") print("n_samples (number of faces): {0}".format(n_samples)) # n_features = 1850, which is 50x37, the dimension of the images. print("n_features (number of pixels): {0}".format(n_features)) print("n_classes (number of people): {0}".format(n_classes)) 通过打印可以看到数据集人脸的尺寸为50x37,为7类共1288张人脸 pca = PCA(n_components=4,whiten = True) X_proj = pca.fit_transform(X[:500]) print("eigen vector",pca.components_) print("...") print('eigen value', pca.explained_variance_[:2]) print(np.var(X_proj[:,0])) print(np.var(X_proj[:,1])) 取500组数据将其降维为4个维度,并进行归一化处理 explained_variance_,它代表降维后的各主成分的方差值。方差值越大,则说明越是重要的主成分 from sklearn import svm def plot_faces(n_features): # nside = 1 X = lfw_people.data # fig, axes = plt.subplots(nside, nside, figsize=(8, 8)) plt.imshow(X[5].reshape(50,37)) plot_faces(n_features= 16) plt.show() 试着打一下其中的一幅图片 Xtrain = lfw_people.data[:1000] Xtest = lfw_people.data[1000:,] ytrain = lfw_people.target[:1000] ytest = lfw_people.target[1000:,] # Xtest = X[select_idx].reshape(1, -1) # test_img = X[select_idx] # ytest = y[select_idx] # n_comp = 50 pca = PCA(n_comp, whiten = True) pca.fit(Xtrain) # pca.fit(Xtest) Xtrain_proj = pca.transform(Xtrain) # projecting test data onto pca axes Xtest_proj = pca.transform(Xtest) print(Xtrain_proj.shape) print(Xtest_proj.shape) # ************************************* The SVM Section ******************************** # instantiating an SVM classifier clf = svm.SVC(gamma=0.001, C=100.) # apply SVM to training data and draw boundaries. clf.fit(Xtrain_proj, ytrain) # Use SVM-determined boundaries to make # a prediction for the test data point. ypred = clf.predict(Xtest_proj) correct = np.sum(ytest == ypred) print(correct/288*100) 接下来之前载入的数据用pca和svm进行训练识别,在1288个数据中取前1000组为训练集,后288个为测试集,pca将维为50维,并用训练集训练的模型对测试集进行预测,最后的测试精度为:81.25%,相对于现状流行的深度学习来说精度还是差了一点。
好久没写博客了 这里主要用python的sklearn包,来进行简单的svm的分类和pca的降维 svm是常用的分类器,其核心是在分类的时候找到一个最优的超平面,使得所有的样本与超平面之间的距离达到最小。 pca是常用的一种降维的方法,其核心是对去中心化后的数据,求得协方差矩阵,再对协方差矩阵进行特征分解,将最大的几个特征值作为这个样本的的新特征,达到降低数据特征维度的效果 这里用sklearn的digits数据集作为演示数据集 import numpy as np import matplotlib.pyplot as plt from scipy import stats # ***use seaborn plotting style defaults import seaborn as sns; sns.set() from sklearn import decomposition from sklearn.decomposition import PCA from sklearn.datasets import load_digits #********************* KEY IMPORT OF THIS LECTURE******************************** from sklearn import svm # loading handwritten digits dig_data = load_digits() X = dig_data.data # y: the values of the digits, or "ground truth" y = dig_data.target dig_img = dig_data.images print(type(X), X.dtype, X.shape) print(type(dig_img), dig_img.dtype, dig_img.shape) print(type(y), y.dtype, y.shape) 先导入所需要的包,并下载所需要的数据集 这里主要用到digits数据集中的.data,.target,.images属性 .data 为数据集的图像数据,用有1797个数据以一维的形式呈现,每个数据集的长度为64 .target为数据集的标签数据 .image位数据以8x8的形式呈现 dig_data = load_digits() X = dig_data.data y = dig_data.target # This is basically each array in X # getting reshaped into (8, 8). dig_img = dig_data.images print(type(X), X.dtype, X.shape) print(type(y), y.dtype, y.shape) select_idx = 2 # select_idx = 5 # ********************************Separating training data from testing data**************** Xtrain = np.delete(X, select_idx, axis = 0) ytrain = np.delete(y, select_idx) # if you don't do .reshape(1, -1), you get a warning. # B/c the data argument for classifier has to be an array, # even if it's a one-element array. Xtest = X[select_idx].reshape(1, -1) test_img = dig_img[select_idx] ytest = y[select_idx] print('Xtrain.shape, ytrain.shape', Xtrain.shape, ytrain.shape) print('Xtest.shape, ytest.shape', Xtest.shape, ytest.shape) plt.figure(figsize = (4, 4)) plt.imshow(test_img, cmap = 'binary') plt.grid('off') plt.axis('off') # ************************************* The PCA Section ******************************** n_comp = 10 pca = PCA(n_comp) # finding pca axes pca.fit(Xtrain) # projecting training data onto pca axes Xtrain_proj = pca.transform(Xtrain) # projecting test data onto pca axes Xtest_proj = pca.transform(Xtest) print(Xtrain_proj.shape) print(Xtest_proj.shape) # ************************************* The SVM Section ******************************** # instantiating an SVM classifier clf = svm.SVC(gamma=0.001, C=100.) # apply SVM to training data and draw boundaries. clf.fit(Xtrain_proj, ytrain) # Use SVM-determined boundaries to make # a prediction for the test data point. clf.predict(Xtest_proj) 将数据集分成训练集和测试集,将索引为2的数据从整个数据集中剔除作为测试数据集,剩下的数据作为训练数据集, n_comp为PCA降维后取得维数, 用训练集来训练PCA,再用训练集的模型来对训练集和测试集进行降维, 接着用训练集来训练SVM,并对测试集进行预测 def classify_dig_svm(X, y, dig_img, select_idx, n_comp, plot_test_img = False): dig_data = load_digits() X = dig_data.data y = dig_data.target dig_img = dig_data.images Xtrain = np.delete(X, select_idx, axis = 0) ytrain = np.delete(y, select_idx) Xtest = X[select_idx].reshape(1, -1) test_img = dig_img[select_idx] ytest = y[select_idx] if plot_test_img == True: plt.figure(figsize = (4, 4)) plt.imshow(test_img, cmap = 'binary') plt.grid('off') plt.axis('off') n_comp = 10 pca = PCA(n_comp) pca.fit(Xtrain) Xtrain_proj = pca.transform(Xtrain) # projecting test data onto pca axes Xtest_proj = pca.transform(Xtest) # print(Xtrain_proj.shape) # print(Xtest_proj.shape) # ************************************* The SVM Section ******************************** # instantiating an SVM classifier clf = svm.SVC(gamma=0.001, C=100.) # apply SVM to training data and draw boundaries. clf.fit(Xtrain_proj, ytrain) clf.predict(Xtest_proj) return clf.predict(Xtest_proj) X = dig_data.data y = dig_data.target n_comp = 30 # select_idx = # classify_dig_svm(X, y, dig_img, select_idx, n_comp, plot_test_img = False) counter = 0 tot = 1797 for i in range(tot): if classify_dig_svm(X, y, dig_img, i, n_comp, plot_test_img = False) == y[i]: counter += 1 rate = counter/tot print(rate) classify_dig_svm的方法是对每个数据进行测试看一下训练出来的pca模型和svm模型的准确性 其实效果还好
建议和最佳做法:整体性能优化策略:性能优化围绕三个基本策略展开: 最大限度地平行执行 优化内存使用量以实现最大内存带宽 优化指令使用率以实现最大指令吞吐量 最大化并行执行从构建算法开始,尽可能多地暴露数据并行。 一旦算法的并行性暴露出来,它就需要尽可能有效地映射到硬件。 这是通过仔细选择每个内核启动的执行配置来完成的。 应用程序还应该通过流显式公开设备上的并发执行以及最大化主机和设备之间的并发执行,从而最大限度地提高并行执行的水平。优化内存使用开始于最小化主机和设备之间的数据传输,因为这些传输比内部设备数据传输具有更低的带宽。 内核对全局内存的访问也应通过最大限度地使用设备上的共享内存来最小化。 有时候,最好的优化甚至可以首先通过简单地重新计算数据来避免任何数据传输。根据每种存储器的访问模式,有效带宽可以变化一个数量级。 因此,优化内存使用的下一步是根据最佳内存访问模式来组织内存访问。 这种优化对于全局内存访问尤其重要,因为访问延迟需要花费数百个时钟周期。 共同的内存访问通常只有在存在高度银行冲突时才值得优化。至于优化指令的使用,应该避免使用吞吐量低的算术指令。 这表明在不影响最终结果时速度的交易精度,例如使用内在函数而不是常规函数或单精度而不是双精度。 最后,由于设备的SIMT(单指令多线程)性质,必须特别注意控制流程指令。NVCC编译器开关:nvcc:NVIDIA nvcc编译器驱动程序将.cu文件转换为C,用于主机系统和CUDA程序集或设备的二进制指令。 它支持许多命令行参数,其中以下对于优化和相关的最佳实践特别有用:‣-maxrregcount = N指定内核在每个文件级别可以使用的最大寄存器数量。 见注册压力。 (另请参阅CUDA C编程指南的“执行配置”中讨论的__launch_bounds__限定符,以控制每个内核基础上使用的寄存器数量。)‣--ptxas-options = -v或-Xptxas = -v列出每个内核的寄存器,共享和常量内存使用情况。‣-ftz = true(非规格化数字被刷新为零)‣-prec-div = false(不太精确的划分)‣-prec-sqrt = false(不精确的平方根)n -use_fast_math nvcc的编译器选项强制每个functionName()调用等效的__functionName()调用。 这使得代码运行速度更快,代价是精度和准确性降低。 请参阅数学库。
-------------------------