gpt4 book ai didi

python - 使用逻辑回归的泰坦尼克号机器学习问题

转载 作者:行者123 更新时间:2023-11-30 09:05:49 25 4
gpt4 key购买 nike

我是一名有抱负的数据科学家。我偶然发现了泰坦尼克号数据集。我尝试使用逻辑回归来解决该问题。然而,当我尝试在训练集上拟合逻辑回归模型时,我陷入了困境。下面是我的代码:

#importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

#importing the dataset
Titanic_train = pd.read_csv('train.csv').values
Titanic_test = pd.read_csv('test.csv').values

columns = ['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked']
Titanic_train = pd.DataFrame(Titanic_train, columns = columns )


#splitting the training data into dependent and independent variable
X = Titanic_train.loc[:,['Pclass', 'Sex','Age','SibSp','Parch','Fare']].values
Y = Titanic_train.loc[:, 'Survived'].values

X = pd.DataFrame(Titanic_train, columns = ['Pclass', 'Sex','Age','SibSp','Parch','Fare'])
Y = pd.DataFrame(Titanic_train, columns = ['Survived'])

#working with missing data
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[['Age']])
X[['Age']] = imputer.transform(X[['Age']])



#dealing with categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
LabelEncoder_X = LabelEncoder()
X['Sex'] = LabelEncoder_X.fit_transform(X['Sex'])

from sklearn.cross_validation import train_test_split
X_train, X_test, Y_train, y_test = train_test_split(X,Y,test_size = 0.4, random_state = 0)



from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state = 0)
classifier.fit(X_train, Y_train)


# Predicting the Test set results
y_pred = classifier.predict(X_test)

*****这是我不断收到的错误:

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py:547: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
Traceback (most recent call last):
File "<ipython-input-196-c1f2228de316>", line 3, in <module>
classifier.fit(X_train, Y_train)
File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\linear_model\logistic.py", line 1217, in fit
check_classification_targets(y)
File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\multiclass.py", line 172, in check_classification_targets
raise ValueError("Unknown label type: %r" % y_type)
ValueError: Unknown label type: 'unknown'*****

如何修复此错误?

最佳答案

您需要将标签结果 Y.Survived 转换为 float。以下代码刚刚运行:

Titanic_train = pd.read_csv('train.csv').values
Titanic_test = pd.read_csv('test.csv').values

columns = ['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked']
Titanic_train = pd.DataFrame(Titanic_train, columns = columns )


#splitting the training data into dependent and independent variable
X = Titanic_train.loc[:,['Pclass', 'Sex','Age','SibSp','Parch','Fare']].values
Y = Titanic_train.loc[:, 'Survived'].values

X = pd.DataFrame(Titanic_train, columns = ['Pclass', 'Sex','Age','SibSp','Parch','Fare'])
Y = pd.DataFrame(Titanic_train, columns = ['Survived'])
Y = Y.Survived.astype("float")

#working with missing data
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[['Age']])
X[['Age']] = imputer.transform(X[['Age']])

#dealing with categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
LabelEncoder_X = LabelEncoder()
X['Sex'] = LabelEncoder_X.fit_transform(X['Sex'])

from sklearn.cross_validation import train_test_split
X_train, X_test, Y_train, y_test = train_test_split(X,Y,test_size = 0.4, random_state = 0)

from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state = 0)
classifier.fit(X_train, Y_train)

# Predicting the Test set results
y_pred = classifier.predict(X_test)

寻找行:

Y = Y.Survived.astype("float")

关于python - 使用逻辑回归的泰坦尼克号机器学习问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52483935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com