2017-10-15 21 views
-3

現在、私はsklearn、numpy、scipyを使用している機械学習の一部をコーディングしています。私は自分のデータベースを解析し、データセットを準備することができます。TypeError: 'tuple'オブジェクトは呼び出し可能ではありませんPython

私のコードは以下の通りです「タプル」オブジェクトが呼び出すことはできません:

from sklearn.svm import SVC 
from sklearn.naive_bayes import GaussianNB 
from sklearn.naive_bayes import MultinomialNB 
from sklearn.linear_model import SGDClassifier 
from sklearn import tree 
from sklearn import gaussian_process 
from sklearn import neural_network 
from sklearn import preprocessing 
from time import time 
import numpy as np 

t0 = time() 

classifier = int(input(
""" 
Enter number corresponding to classifier you would like to use: 
1. Support Vector Machines 
2. Gaussian Naive Bayes 
3. Multinomial Naive Bayes 
4. Stochastic Gradient Descent with Logistic Regression loss function 
""")) 

dataset = int(input(
""" 
Enter number corresponding to data set you would like to use: 
1. First half and second half 
2. Alternating 
3. Alternating with attack type 
4. Alternating with attack type and target type 
""")) 

# Assign appropriate datasets 
input_files = ['half', 'alternating', 'alternating-with-attacktype', 'alternating-all'] 
filename = input_files[dataset-1] 

# Instantiate arrays for inputs and outputs 
train_inputs = [] 
train_outputs = np.array([]) 

test_inputs = [] 
test_expected_outputs = np.array([]) 
test_actual_outputs = [] 
X = np.array([]) 
# Read training file 
print ('Reading training file') 
t = time() 

for line in open('datasets/%s-train.txt' % filename): 
    inputs = line.split(' ') 
    outputs = inputs.pop() 
    train_outputs = np.append(train_outputs, int(outputs)) 
print ('Done. Time taken: %f secs.\n' % (time()-t)) 

# for line in open('datasets/%s-train.txt' % filename): 
# inputs = line.split(' ') 
# output = inputs.pop() 
# train_outputs = np.append(train_outputs, int(output)) 
# print ('Done. Time taken: %f secs.\n' % (time()-t)) 

print ('Create classifier') 
t = time() 
clf = None 

# No preprocessing for SVMs 
# Otherwise, scale inputs (preprocessing to make more amenable for machine learning) 
if classifier == 1: # Support vector machines 
    clf = SVC() 
elif classifier == 2: # Gaussian Naive Bayes 
    train_inputs = preprocessing.scale(np.array(train_inputs)) 
    clf = GaussianNB() 
elif classifier == 3: # Multinomial Naive Bayes 
    clf = MultinomialNB() 
elif classifier == 4: # Stochastic gradient descent with logistic regression 
    train_inputs = preprocessing.scale(np.array(train_inputs)) 
    clf = SGDClassifier(loss='log') 
print ('Done. Time taken: %f secs.\n' % (time()-t)) 

print ('Fit classifier') 
t = time() 
X.shape(1 -1) 
clf.fit(train_inputs, train_outputs) 
print ('Done. Time taken: %f secs.\n' % (time()-t)) 

# Read test file and scale inputs 
print ('Reading test file') 
t = time() 
for line in open('datasets/%s-test.txt' % filename): 
    inputs = line.split(' ') 
    output = inputs.pop() 
    test_expected_outputs = np.append(test_expected_outputs, int(output)) 
    test_inputs.append(map(float, inputs)) 

# Same here: no preprocessing for SVMs 
# Otherwise, scale inputs (preprocessing to make more amenable for machine learning) 
if classifier != 1: 
    test_inputs = preprocessing.scale(np.array(test_inputs)) 
print ('Done. Time taken: %f secs.\n' % (time()-t)) 

print ('Predict for test file') 
t = time() 
test_actual_outputs = [clf.predict(i)[0] for i in test_inputs] 
print ('Done. Time taken: %f secs.\n' % (time()-t)) 

print ('Compare outputs') 
t = time() 
right = sum(test_actual_outputs == test_expected_outputs) 
wrong = len(test_actual_outputs) - right 
print ('Done. Time taken: %f secs.\n' % (time()-t)) 

print ('Number right: %d\nNumber wrong: %d' % (right, wrong)) 
print ('Prediction rate: %.2f%%' % (100.0 * right/len(test_actual_outputs))) 
print ('Total time taken: %f secs.\n' % (time()-t0)) 

私が知っている

タイプエラー:予測に来て、その結果を出力するときしかし、私は次のエラーを取得していますarray.reshape(-1 1)またはarray.reshape(1 -1)を追加する必要がありますが、これが何をするかはわかりません。

これを解決する方法についてのアドバイスは大歓迎です。

+5

してください:[MCVE]。また、*あなたの質問には**完全なトレースバック**を提供する必要があります。 –

+1

完全なトレースバックは何ですか?コードを掘り下げずにエラーを発生させる原因は何ですか? – roganjosh

+1

1.これは長いプログラムです - エラーを再現する最小限の好ましくは自己完結型の例を作成してください。 2.完全なエラーメッセージを投稿してください(プログラムが失敗した行が明確ではありません)。 3.ステップインデバッガを使ってプログラムを実行すると、おそらく問題の内容を理解するのに役立ちます –

答えて

0

このエラーは、ラインから来ている:

X.shape(1 -1) 

ではなくX.shapeを使用してください。ここで

が問題の再現です:

X = pd.Series() 
X.shape(1 -1) 

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-105-1bb3332dc7d5> in <module>() 
     1 X = pd.Series() 
----> 2 X.shape(1 -1) 

TypeError: 'tuple' object is not callable 
関連する問題