2014-02-22 42 views
0

我想知道如何將一列添加到一個numpy數組?假設我在.tsv格式如下:如何獲得網絡流量圖+如何將列添加到numpy數組?

from sklearn import metrics,preprocessing,cross_validation 
    from sklearn.feature_extraction.text import TfidfVectorizer 
    import sklearn.linear_model as lm 
    import pandas as p  
    print "loading data.." 
    traindata = np.array(p.read_table('train.tsv')) #here is where I am unsure what to do 

traindata的第一列包含每個網頁的URL。

在此之後,我想的邏輯是:

for each row in traindata 
      #run function to look up traffic webpage is getting, store this in a numpy array 
Add a new column to traindata numpy array, append on the data in the array created into our "for each" 

怎麼可以這樣一般實現,即使你只是使用檢索網絡流量「填充物」的方法? :)

謝謝!

Inputs and outputs : 
    Input : Numpy array of 26 columns. 
    We call a function on the value in the first column of each row, this function will return a number. 
    We append all these numbers into a numpy array with one column. 
    We append the Numpy array with 26 cols to the one made above to end up with a numpy array with 27 columns. 
Output : Numpy array of 26 columns. 
+0

問題,要求我們建議或找到一個工具,庫或喜愛的異地資源是題外話了堆棧溢出,因爲他們傾向於吸引自以爲是的答案和垃圾郵件。相反,請描述問題以及到目前爲止解決問題所做的工作。 –

+1

這裏有兩個截然不同的問題。請一次提出一個問題。 – msvalkon

+0

@JanDvorak感謝您的信息!我現在已經解決了這個問題 - 工具是不重要的部分,我只需要一種方法來獲得數字:) –

回答

1

您可以使用numpy.hstack追加列,就像這樣:

import numpy as np 

def some_function(x): 
    return 3*x 

input = np.ones([10,26]) 
input = np.hstack([input,np.empty([input.shape[0],1])]) 
for row in input: 
    row[-1] = some_function(row[0]) 

output = input 
+0

謝謝你。我可以在我的每個運行中追加一個值到我在這裏創建的numpy數組的列嗎?謝謝:) –

+0

你可以請舉一些你的輸入和輸出的例子嗎?謝謝。 – Tohotom

+0

我已將此信息添加到現在的問題。 –