SKlearn给我一个ValueError:X和y的形状与异常不匹配

时间:2014-04-28 16:31:42

标签: python scikit-learn

SKlearn在项目上遇到了一些麻烦。我的目标是使用SKlearn来确定泰坦尼克号上有多少人相对于他们的班级而死。到目前为止,这是我搞砸了的代码。

import pandas as pd
import numpy as np
import pylab as P
from sklearn.datasets import load_iris
from sklearn.linear_model.stochastic_gradient import SGDClassifier
from sklearn import linear_model


#The goal of this project is to understand how many people on the Titanic died when    compared to their class.
#reading in the dataframe from train.csv
df = pd.read_csv('train.csv', header=0)
#grabbing Pclass and Survived for data bits. 
train = df['Pclass'].values
target = df['Survived'].values

#converting into a numpy array. 
train = np.array(train)
target = np.array(target)
print target.shape, train.shape

#testing
clf = SGDClassifier(loss='log')
#clf = SVC(kernel='linear', probability=True, random_state=33)
clf.fit(train, target)
test = [3, 1, 0, 2]
print("Result is: " + str(clf.predict(test)))
prob = clf.predict_proba(test)
print("\nProbabilities: ")
print(prob)
#display coefficients
print("\nCoefficients of the features:\n")
print(clf.coef_)
#display the intercept
print("\nIntercept: ")
print(clf.intercept_)

以下是我的输出:

C:\Python27\python.exe "C:/Users/Shock's lab/PycharmProjects/Bigdata/project.py"
(891,) (891,)
Traceback (most recent call last):
  File "C:/Users/Shock's lab/PycharmProjects/Bigdata/project.py", line 24, in <module>
    clf.fit(train, target)
  File "C:\Python27\lib\site-packages\sklearn\linear_model\stochastic_gradient.py", line 519, in fit
sample_weight=sample_weight)
  File "C:\Python27\lib\site-packages\sklearn\linear_model\stochastic_gradient.py", line 418, in _fit
classes, sample_weight, coef_init, intercept_init)
  File "C:\Python27\lib\site-packages\sklearn\linear_model\stochastic_gradient.py", line 345, in _partial_fit
_check_fit_data(X, y)
  File "C:\Python27\lib\site-packages\sklearn\linear_model\stochastic_gradient.py", line 229, in _check_fit_data
raise ValueError("Shapes of X and y do not match.")
ValueError: Shapes of X and y do not match.

任何有关解释此异常并绕过它的帮助将不胜感激。

编辑:这是我的火车和目标的输出:

(891,)(891,)

火车是[3 1 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