2016-04-12 33 views
0

私はこのソリューション(右下)をパンダを使用せずに取得しようとしています。基本的には、健康な患者と病気の患者の平均をCSVファイルに入れています。このファイルには、14のカテゴリー(13行)の303人の患者があります。データの一部が失われていますので、その代わりに?の行13は、0より大きい何かが病気で、何かが0以下で健常な患者を分けます。私はそれらを分割する方法を見つけましたが、健康な患者と病気の患者を別々に平均化するための線をどのように追加するかを知っています。続行する方法上の任意のアイデアはCSV Pythonの列/行の平均

Please enter a training file name: train.csv 
    Total Lines Processed: 303 
    Total Healthy Count: 164 
    Total Ill Count: 139 
    Averages of Healthy Patients: 
    [52.59, 0.56, 2.79, 129.25, 242.64, 0.14, 0.84, 158.38, 0.14, 0.59, 1.41, 0.27, 3.77, 0.00] 
    Averages of Ill Patients: 
    [56.63, 0.82, 3.59, 134.57, 251.47, 0.16, 1.17, 139.26, 0.55, 1.57, 1.83, 1.13, 5.80, 2.04] 
    Seperation Values are: 
    [54.61, 0.69, 3.19, 131.91, 247.06, 0.15, 1.00, 148.82, 0.34, 1.08, 1.62, 0.70, 4.79, 1.02] 

私はまだ私のコードの上に行くには長い道のりを持って、私は患者の平均値を取得する単純な方法を探しています素晴らしいだろう。私の現在のメソッドは13列しか取得しませんが、上記のように13列すべてが必要です。どのような方法でこれを解決しようとするべきかについての助けは素晴らしいものであり、非常に感謝しています。

import csv 
#turn csv files into a list of lists 
with open('train.csv') as csvfile: 
    reader = csv.reader(csvfile, delimiter=',') 
    csv_data = list(reader) 

i_list = [] 
for row in csv_data: 
    if (row and int(row[13]) > 0): 
     i_list.append(int(row[13])) 
H_list = [] 
for row in csv_data: 
    if (row and int(row[13]) <= 0): 
     H_list.append(int(row[13])) 
for row in reader: 

Icount = len(i_list) 
IPavg = sum(i_list)/len(i_list) 
Hcount = len(H_list) 
HPavg = sum(H_list)/len(H_list) 
file = open("train.csv") 
numline = len(file.readlines()) 

print(numline) 
print("Total amount of healthy patients " + str(Icount)) 
print("Total amount of ill patients " + str(Hcount)) 
print("Averages of healthy patients " + str(HPavg)) 
print("Averages of ill patients " + str(IPavg) 

CVS File 
A   B  C  D N(so on to column 13) 
10  .50  ?  44 0 
4   4.5  20  34 0 
12   ?  33  23 3 (this one would be Ill patient) 
11   3.2  32  33 0 
[![CSVfile][1]][1] 

コメントで依頼されたものの画面は、ここで Screenshot

+0

あなたはnumpyのの平均を試してみてください:http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.average.htmlは、統計的な作業を行うには、それははるかに容易になります:) – Adib

+2

これで展開できますか:「私の現在のメソッドは13列しか取得できませんが、上記のように13が必要です。」 – Adib

+0

@Adibはい私はパンダと同じように私のクラスでまだそれを通過していないという事実のために、私はこの問題のためにそれを使用することはできませんが、numpyの範囲を知っています。 13列の部分について現在のコードは、患者が健康であるかどうかを示すものであるため、必要な種類の列13の情報のみを取得します。しかし、健康であるかどうかを知った後、各列の健康な患者の数を広告する必要があるので、私はそれらの平均を得ることができます。 – David

答えて

1

ショットチュートリアル(コメント)との完全なことです。 Pythonをマスターする方法を理解したい場合は、それらを読んでください。

import csv 

#turn csv files into a list of lists 
with open('train.csv','rU') as csvfile: 
    reader = csv.reader(csvfile) 
    csv_data = list(reader) 

# Create two lists to handle the patients 
# And two more lists to collect the 'sum' of the columns 
# The one that needs to hold the sum 'must' have 0 so we 
# can work with them more easily 
iList = [] 
iList_sum = [0,0,0,0,0,0,0,0,0,0,0,0,0] 

hList = [] 
hList_sum = [0,0,0,0,0,0,0,0,0,0,0,0,0] 

# Only use one loop to make the process mega faster 
for row in csv_data: 
    # If row 13 is greater than 0, then place them as unhealthy 
    if (row and int(row[13]) > 0): 
     # This appends the whole 'line'/'row' for storing :) 
     # That's what you want (instead of saving only one cell at a time) 
     iList.append(row) 

    # If it failed the initial condition (greater than 0), then row 13 
    # is either less than or equal to 0. That's simply the logical outcome 
    else: 
     hList.append(row) 

# Use these to verify the data and make sure we collected the right thing 
# print iList 
# [['67', '1', '4', '160', '286', '0', '2', '108', '1', '1.5', '2', '3', '3', '2'], ['67', '1', '4', '120', '229', '0', '2', '129', '1', '2.6', '2', '2', '7', '1']] 
# print hList 
# [['63', '1', '1', '145', '233', '1', '2', '150', '0', '2.3', '3', '0', '6', '0'], ['37', '1', '3', '130', '250', '0', '0', '187', '0', '3.5', '3', '0', '3', '0']] 

# We can use list comprehension, but since this is a beginner task, let's go with basics: 

# Loop through all the 'rows' of the ill patient 
for ill_data in iList: 

    # Loop through the data within each row, and sum them up 
    for i in range(0,len(ill_data) - 1): 
     iList_sum[i] += float(ill_data[i]) 


# Now repeat the process for healthy patient 
# Loop through all the 'rows' of the healthy patient 
for healthy_data in hList: 

    # Loop through the data within each row, and sum them up 
    for i in range(0,len(healthy_data) - 1): 
     hList_sum[i] += float(ill_data[i]) 

# Using list comprehension, I basically go through each number 
# In ill list (sum of all columns), and divide it by the lenght of iList that 
# I found from the csv file. So, if there are 22 ill patients, then len(iList) will 
# be 22. You can see that the whole thing is wrapped in brackets, so it would show 
# as a python list 

ill_avg = [ ill/len(iList) for ill in iList_sum] 
hlt_avg = [ hlt/len(hList) for hlt in hList_sum] 

# Do whatever.... 
+0

コード内の何かについて質問がありましたら、ありがとうございます。 私はこれをもっとうまくマストする方法を探しています。私は実際の割り当てがありますが、これを行う方法を理解する必要がありました。もう一度ありがとう、本当にありがとうございました。 – David

+0

@David確かに。私には頼らないでください:)私は他人を助けて自分のコーディングを練習するのが好きです – Adib

+0

笑こんにちは私はあなたに頼るつもりはありません私はそれを完全に理解するために私が時間を取ることを意味した私は徹底したい私はそれについて何か質問があったら、私はそれについてあなたの脳を突き進めることができるかどうか疑問に思っていた。それは本当によく説明されているようだが – David