【问题标题】:Why is my y_pred model only close to zero?为什么我的 y_pred 模型只接近于零?
【发布时间】:2018-09-19 03:56:11
【问题描述】:

我是 python 新手,也学习机器学习。我得到了一个泰坦尼克号的数据集,并试图预测谁幸存下来,谁没有幸存。但我的代码似乎与y_pred 存在问题,因为它们都没有接近 1 或高于 1。另附上y_testy_pred 图片。

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

    # Importing the dataset
    dataset = pd.read_csv('train.csv')
    X = dataset.iloc[:, :-1].values
    y = dataset.iloc[:, 3].values

    # Taking care of missing data
    from sklearn.preprocessing import Imputer
    imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
    imputer = imputer.fit(X[:, 2:3])
    X[:, 2:3] = imputer.transform(X[:, 2:3])

    #Encoding Categorical variable
    from sklearn.preprocessing import LabelEncoder, OneHotEncoder
    labelencoder_X = LabelEncoder()
    X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
    onehotencoder = OneHotEncoder(categorical_features = [0])
    X = onehotencoder.fit_transform(X).toarray()

    # Dummy variable trap
    X = X[:, 1:]

    # Splitting the Dataset into Training Set and Test Set
    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

    # Split the dataset into training and test set
    from sklearn.model_selection import train_test_split
    X_train, X_test, y_tratin, y_test = train_test_split(X, y, test_size = 0.2,)

    # Fitting the Multiple Linear Regression to the training set
    """ regressor is an object of LinearRegression() class in line 36 """
    from sklearn.linear_model import LinearRegression
    regressor = LinearRegression()
    regressor.fit(X_train, y_train)

【问题讨论】:

  • 查看此数据集有助于确定您正在尝试做什么以及可能出了什么问题。
  • 还有什么理由你使用train_test_split 分裂两次?仅供参考,您在第二个实例中也有一个错字y_traitin 应该是y_train
  • 如果您想预测谁幸存下来,那不应该是classification 问题而不是regression 问题吗?我的意思是,为什么不使用返回 0 或 1 的模型(例如树?),而不是连续数字

标签: python-3.x pandas scikit-learn sklearn-pandas


【解决方案1】:

感谢大家的帮助,我已经能够解决了。 问题是导入数据集中的 y 被视为向量而不是矩阵

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

# Importing the dataset
dataset = pd.read_csv('train.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 3:].values

# Taking care of missing data
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[:, 2:3])
X[:, 2:3] = imputer.transform(X[:, 2:3])

#Encoding Categorical variable
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
onehotencoder = OneHotEncoder(categorical_features = [0])
X = onehotencoder.fit_transform(X).toarray()

# Dummy variable trap
X = X[:, 1:]

# Splitting the Dataset into Training Set and Test Set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

# Fitting the Multiple Linear Regression to the training set
""" regressor is an object of LinearRegression() class in line 36 """
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)

# Predicting the test set result
y_pred = regressor.predict(X_test)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多