毕设知识点总结——part2.聚类

来源:互联网 发布:splice软件安卓 编辑:程序博客网 时间:2024/05/18 00:26
  • 聚类结果出现
    [ nan nan nan nan
    nan nan]
    [ nan nan nan nan
    nan nan]]
    解决:在计算欧式距离的函数里添加
    vecA = nan_to_num(vecA)
    vecB = nan_to_num(vecB)
  • 把numpy的数组(矩阵)输出到txt文件里
    datMat_mat_file = open(‘./datMat.txt’, ‘wb’)
    savetxt(‘./datMat.txt’, datMat, fmt=’%.18e’, delimiter=’ ‘, newline=’\n’, header=”, footer=”,
    comments=’#’)
    缺省按照’%.18e’格式保存数值,以空格分隔,若fmt=”%d”则改为保存为整数。
    delimiter=’ ‘,表示以空格分隔
    读写1维和2维数组的文本文件
  • exec语句用来执行储存在字符串或文件中的Python语句。例如,我们可以在运行时生成一个包含Python代码的字符串,然后使用exec语句执行这些语句。下面是一个简单的例子。
>>> exec 'print "Hello World"'Hello Worldfor i in range(4):    exec 'word_set'+str(i+1)+'=[]'#或#for i in range(4):#    exec 'word_set%s = []' %i
  • range(1,5) #代表从1到5(不包含5)
    [1, 2, 3, 4]
    range(1,5,2) #代表从1到5,间隔2(不包含5)
    [1, 3]
    range(5) #代表从0到5(不包含5)
    [0, 1, 2, 3, 4]
  • python numpy矩阵和数组的转换
    数组转换矩阵:
    A = mat(s[])
    矩阵转换数组:
    s[]= A.getA()
  • 报错:IndexError: tuple index out of range
word_set0 = open('./word_set0.txt', 'wb') savetxt('./word_set0.txt', word_set0, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='',        comments='#')

原因:word_set0为list类型而非numpy的matrix,所以不能用savetxt读写

  • 安装scipy
    安装scikit-learn
    pip install -U scikit-learn
    -U就是 –upgrade,意思是如果已安装就升级到最新版。可pip install -h查看说明
    安装scikit-learn错误:ImportError: No module named Cython.Distutils
    解决: 安装Cpython 的包, pip就可以安装了
    安装Cython后依然报错error: Unable to find vcvarsall.bat
    重新下载0.18.1版本的scipy(原版本为0.13.1)和0.18.1版本的sklearn后,安装成功。
  • 导入Scipy子模块时报错,出现的问题都是提示
    No module named ‘numpy._distributor_init’
    原因:https://www.zhihu.com/question/48377516
    大意是使用pip安装成功numpy而不是numpy+mkl
    来这里(http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy)下载numpy whl文件,安装。
  • python中clf.fit
    fit()可以说是调用的通用方法。fit(X),表示用数据X来训练某种模型。 函数返回值一般为调用fit方法的对象本身。fit(X,y=None)为无监督学习算法,fit(X,Y)为监督学习算法
0 0