Tuesday, December 15, 2015

寻找顺手的编辑工具

一直使用notepad++写东西,最近发现了sublime,真的很强大,继续学习中。继续下自己入门的东西 参考了http://loosky.net/2967.html
1.老外写东西还是比较严密的
安装packagecontrol
https://packagecontrol.io/installation#st3
2.https://github.com/wuub/SublimeREPL
3.自动补齐SublimeCodeIntel
4.代码检查 SublimeLinter    
5.https://realpython.com/blog/python/setting-up-sublime-text-3-for-full-stack-python-development/
6.https://www.zhihu.com/question/19976788
7.可用的licens https://www.shuax.com/archives/SublimeText3Crack.html

Monday, December 14, 2015

revolution R 和R studio server安装

上次折腾半天,server始终启动不起来,在stack overflow上找到很多和我一样类似的情况,最后的解决办法是R的版本太高了,3.2.2不能用,换回了3.2.0启动正常。有时间整理,具体方法参加官方文档和张丹 粉丝博客的博文

spyder无法启动解决办法

自己装的是python(x,y),习惯性的打开spyder写代码,突然发现打不开了
使用了如下方法解决了
1.进入C:\Python27\Scripts执行
python sypder --reset
初始化之后还是不行
2.cmd进入命令行,执行spyder还是没有启动,报了依赖确实得问题
3.执行卸载 easy_install -m package-name
或者pip uninstall package-name 卸载重装,还是不行
4.最后报的错误是配置文件出错
5.删除 C:\Users\dami\.spyder2 问题解决

在ubuntu下遇见:
Spyder is already running. If you want to open a new  instance, please pass to it the --new-instance option

参考这里https://github.com/spyder-ide/spyder/issues/3005提出的方法
cd .spyder2/
rm spyder.lock
就好了

Thursday, December 10, 2015

20151210python knn

一。好用的编辑器 sublime
设置tab
【将Tab键自动替换为4个空格】
// The number of spaces a tab is considered equal to
"tab_size": 4,
// Set to true to insert spaces when tab is pressed
"translate_tabs_to_spaces": true,
二。代码
from numpy import *
import operator

def createDataSet():
    group = array([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])
    labels =['A','A','B','B']
    return group,labels

def classify0(inX,dataSet,labels,k):

    dataSetSize=dataSet.shape[0]
    diffMat=tile(inX,(dataSetSize,1))-dataSet
    sqDiffMat=diffMat**2
    sqDistances=sqDiffMat.sum(axis=1)
    distances=sqDistances**0.5
    sortedDistIndicies=distances.argsort()
    classCount={}

    for i in range(k):

        voteIlabel = labels[sortedDistIndicies[i]]
        classCount[voteIlabel] =classCount.get(voteIlabel,0)+1
    sortedClassCount=sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True)
    return sortedClassCount[0][0]

三。遇到的错误
ndentationError:expected an indented block说明此处需要缩进,你只要在出现错误的那一行,按空格或Tab(但不能混用)键缩进就行。关键在于sublime改了设置,tab前面是tab,后面变成了四个空格
错误在于   diffMat=tile(inX,(dataSetSize,1))-dataSet中dataSetSize写成了dataSet
四。几个知识点

numpy.argsort >>> x = np.array([3, 1, 2])

>>> np.argsort(x)
array([1, 2, 0]) 返回排序数据下表

Python中的sorted函数以及operator.itemgetter函数

operator.itemgetter函数 operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子。
a = [1,2,3]  >>> b=operator.itemgetter(1)      //定义函数b,获取对象的第1个域的值 >>> b(a)  2  >>> b=operator.itemgetter(1,0)  //定义函数b,获取对象的第1个域和第0个的值 >>> b(a)  (2, 1)
要注意,operator.itemgetter函数获取的不是值,而是定义了一个函数,通过该函数作用到对象上才能获取值。
sorted函数 Python内置的排序函数sorted可以对list或者iterator进行排序,官网文档见:http://docs.python.org/2/library/functions.html?highlight=sorted#sorted,该函数原型为:
sorted(iterable[, cmp[, key[, reverse]]])
参数解释:
(1)iterable指定要排序的list或者iterable,不用多说;
(2)cmp为函数,指定排序时进行比较的函数,可以指定一个函数或者lambda函数,如:
      students为类对象的list,没个成员有三个域,用sorted进行比较时可以自己定cmp函数,例如这里要通过比较第三个数据成员来排序,代码可以这样写:       students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]       sorted(students, key=lambda student : student[2]) (3)key为函数,指定取待排序元素的哪一项进行排序,函数用上面的例子来说明,代码如下:       sorted(students, key=lambda student : student[2])
python字典 get()方法; 返回一个给定的key对应的值。如果key是没有用的,然后返回默认值None返回。
语法:
dict.get(key, default=None)
参数: 下面是详细参数:
  • key:key在字典中查找。
  • default:在key不存在的情况下返回值None。
1,广播:对 形状不同 的数组的运算采取的操作。但是这个输入的数组中必须有一个 某轴长度为1,或者缺 少了一个维度(这个时候会自动的在shape属性前面补上1)。例如:
>>> import numpy as np  >>> a=np.arange(10,50,10).reshape(-1,1)  >>> a.shape  (4, 1)  >>> b=np.arange(0,4)  >>> b  array([0, 1, 2, 3])  >>> b.shape  (4,)  >>> a+b  array([[10, 11, 12, 13],         [20, 21, 22, 23],         [30, 31, 32, 33],         [40, 41, 42, 43]]) 

numpy.sum

numpy.sum(aaxis=Nonedtype=Noneout=Nonekeepdims=False)[source]
Sum of array elements over a given axis.
Parameters:
a : array_like
Elements to sum.
axis : None or int or tuple of ints, optional
Axis or axes along which a sum is performed. The default (axis = None) is perform a sum over all the dimensions of the input array. axis may be negative, in which case it counts from the last to the first axis.
New in version 1.7.0.
If this is a tuple of ints, a sum is performed on multiple axes, instead of a single axis or all the axes as before.
dtype : dtype, optional
The type of the returned array and of the accumulator in which the elements are summed. By default, the dtype of a is used. An exception is when a has an integer type with less precision than the default platform integer. In that case, the default platform integer is used instead.
out : ndarray, optional
Array into which the output is placed. By default, a new array is created. If out is given, it must be of the appropriate shape (the shape of a with axis removed, i.e., numpy.delete(a.shape, axis)). Its type is preserved. See doc.ufuncs (Section “Output arguments”) for more details.
keepdims : bool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr.
Returns:
sum_along_axis : ndarray
An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.
See also
ndarray.sum
Equivalent method.
cumsum
Cumulative sum of array elements.
trapz
Integration of array values using the composite trapezoidal rule.
Notes
Arithmetic is modular when using integer types, and no error is raised on overflow.
The sum of an empty array is the neutral element 0:
>>>
>>> np.sum([])
0.0
Examples
>>>
>>> np.sum([0.5, 1.5])
2.0
>>> np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32)
1
>>> np.sum([[0, 1], [0, 5]])
6
>>> np.sum([[0, 1], [0, 5]], axis=0)
array([0, 6])
>>> np.sum([[0, 1], [0, 5]], axis=1)
array([1, 5])
If the accumulator is too small, overflow occurs:
>>>
>>> np.ones(128, dtype=np.int8).sum(dtype=np.int8)
-128