2014-09-21 24 views
0

我正在準備對csv文檔進行一些預測,比較工作描述與工資結果。我已經將數據集分解爲訓練和測試,其中的特徵是我正在處理的內容,目標是我所預測的。當我去打印並確認這些記錄進行了適當分開後,我收到以下錯誤:ValueError異常:不一致的形狀ValueError:Scikit學習中的形狀不一致錯誤Train_Test拆分

我的代碼和所產生的誤差如下:

import csv 
import numpy as np 

# create posting & label list 
postList = [] 
labelList = [] 
filename = '\Users\yantezia.patrick\Downloads\Postings.csv' 
csvFile = csv.reader(open(filename, 'r'), delimiter=",") 
for row in csvFile: 
    postList.append(row[2]) 
    labelList.append(row[10]) #appending specific columns to specific list #these willbe labels 

# remove first row 
postList = postList[1:] #clearing out the header rows 
labelList = labelList[1:] 

temp = np.array([float(i) for i in labelList]) 
med = np.median(temp) 
for i, val in enumerate(labelList): 
    if float(val) >= med: 
     labelList[i] = 1 
    else: 
     labelList[i] = 0 

# subset list 
postList = postList[:100] 
labelList = labelList[:100] 
print postList[:2] 

from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer 
import pandas as pd 

# create term matrix 
cv = CountVectorizer(lowercase=True, stop_words='english', ngram_range=(1,3), min_df=10) 
tfidf = TfidfVectorizer(lowercase=True, stop_words='english', ngram_range=(1,3), min_df=10) 

tf_dm = cv.fit_transform(postList) 
tfidf_dm = tfidf.fit_transform(postList) 

pd.DataFrame(tfidf_dm.toarray(),index=postList,columns=tfidf.get_feature_names()).head(10) 

tfidf.get_feature_names() 
tm = tm.toarray() 
print tf_dm 
tm = cv.fit(postList) 
print tm.vocabulary_ 
print tf_dm.shape 
print tfidf_dm.shape 

#add labels to word vector 
from sklearn.cross_validation import train_test_split 

features_train1 = train_test_split(tf_dm, labels, test_size=0.33, random_state=42) 
features_test1 = train_test_split(tf_dm, labels, test_size=0.33, random_state=42) 
target_train1 = train_test_split(tf_dm, labels, test_size=0.33, random_state=42) 
target_test1 = train_test_split(tf_dm, labels, test_size=0.33, random_state=42) 

features_train2 = train_test_split(tfidf_dm, labels, test_size=0.33, random_state=7) 
features_test2 = train_test_split(tfidf_dm, labels, test_size=0.33, random_state=7) 
target_train2 = train_test_split(tfidf_dm, labels, test_size=0.33, random_state=7) 
target_test2 = train_test_split(tfidf_dm, labels, test_size=0.33, random_state=7) 

print np.sum(target_train1) 
print np.sum(target_test1) 
print target_train1 
print target_test1 
--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-82-53ecd8559f48> in <module>() 
----> 1 print np.sum(target_train1) 
     2 print np.sum(target_test1) 
     3 print target_train1 
     4 print target_test1 

C:\Users\yantezia.patrick\AppData\Local\Continuum\Anaconda\lib\site-packages\numpy\core\fromnumeric.pyc in sum(a, axis, dtype, out, keepdims) 
    1707   except AttributeError: 
    1708    return _methods._sum(a, axis=axis, dtype=dtype, 
-> 1709         out=out, keepdims=keepdims) 
    1710   # NOTE: Dropping the keepdims parameters here... 
    1711   return sum(axis=axis, dtype=dtype, out=out) 

C:\Users\yantezia.patrick\AppData\Local\Continuum\Anaconda\lib\site-packages\numpy\core\_methods.pyc in _sum(a, axis, dtype, out, keepdims) 
    23 def _sum(a, axis=None, dtype=None, out=None, keepdims=False): 
    24  return um.add.reduce(a, axis=axis, dtype=dtype, 
---> 25        out=out, keepdims=keepdims) 
    26 
    27 def _prod(a, axis=None, dtype=None, out=None, keepdims=False): 

C:\Users\yantezia.patrick\AppData\Local\Continuum\Anaconda\lib\site-packages\scipy\sparse\compressed.pyc in __add__(self, other) 
    340   elif isspmatrix(other): 
    341    if (other.shape != self.shape): 
--> 342     raise ValueError("inconsistent shapes") 
    343 
    344    return self._binopt(other,'_plus_') 

ValueError: inconsistent shapes 

回答

1

能否請你解釋一下你想什麼去做? features_train1,features_test1,target_train1target_test的定義是相同的。當你修改隨機狀態時,它們都將具有相同的內容。 你有沒有試圖看看這些是什麼? 請看the documentation of train_test_splitfeatures_train1是一個元組,其中前兩個元素是tf_dm的分割,而後兩個元素是labels的分割。

什麼是你最有可能的意思做了

features_train1, features_test1, labels_train1, labels_test1 = train_test_split(tf_dm, labels, test_size=0.33, random_state=42)