无法将字符串转换为python中的float

问题描述 投票:0回答:1

我尝试从cvs文件分析主成分,但是当我运行代码时出现此错误

C:\ Users \ Lenovo \ Desktop> python pca.py

ValueError:无法将字符串转换为浮点型:Annee; NET; INT; SUB; LMT; DCT; IMM; EXP; VRD

这是我的cvs文件

<code>enter image description here</code>

我尝试删除任何空格和任何想法这是我的python脚本,我不知道我想念的是

注意:我在python2.7下运行此代码

from sklearn.externals import joblib  
import numpy as np  
import glob  
import os  
import time  
import numpy

my_matrix = numpy.loadtxt(open("pca.csv","rb"),delimiter= ",",skiprows=0)  
def pca(dataMat, r, autoset_r=False, autoset_rate=0.9): 
    """
    purpose: principal components analysis
    """  
    print("Start to do PCA...") 
    t1 = time.time() 
    meanVal = np.mean(dataMat, axis=0)  
    meanRemoved = dataMat - meanVal  
    # normData = meanRemoved / np.std(dataMat)  
    covMat = np.cov(meanRemoved, rowvar=0)    
    eigVals, eigVects = np.linalg.eig(np.mat(covMat)) 
    eigValIndex = np.argsort(-eigVals)  


    if autoset_r:
        r = autoset_eigNum(eigVals, autoset_rate)
        print("autoset: take top {} of {} features".format(r, meanRemoved.shape[1]))

    r_eigValIndex = eigValIndex[:r]  
    r_eigVect = eigVects[:, r_eigValIndex]  
    lowDDataMat = meanRemoved * r_eigVect  
    reconMat = (lowDDataMat * r_eigVect.T) + meanVal    
    t2 = time.time()   
    print("PCA takes %f seconds" %(t2-t1))
    joblib.dump(r_eigVect, './pca_args_save/r_eigVect.eig')    
    joblib.dump(meanVal, './pca_args_save/meanVal.mean')   
    return lowDDataMat, reconMat


def autoset_eigNum(eigValues, rate=0.99):

    eigValues_sorted = sorted(eigValues, reverse=True)
    eigVals_total = eigValues.sum()
    for i in range(1, len(eigValues_sorted)+1):
        eigVals_sum = sum(eigValues_sorted[:i])     
        if eigVals_sum / eigVals_total >= rate:
            break
    return i
pandas python-2.7 scikit-learn anaconda pca
1个回答
1
投票

似乎NumPy在解析索引行以浮动时遇到了一些问题。

© www.soinside.com 2019 - 2024. All rights reserved.