【问题标题】:Logistic Regression in pythonpython中的逻辑回归
【发布时间】:2016-05-23 13:10:18
【问题描述】:

我目前正在为 python 进行机器学习中的逻辑回归。这是我写的代码。

import pandas as pd
from sklearn import linear_model
import numpy as np
from sklearn.utils import column_or_1d

logistic = linear_model.LogisticRegression()

data = pd.read_excel('/home/mick/PycharmProjects/project1/excel/Ron95_Price_Class.xlsx')

X = data[['Date']]
y = data[['Ron95_RM']]

y = np.ravel(y)

logistic.fit(X, y)

price = logistic.predict(42491)
print "The price for Ron95 in next month will be RM", np.array_str(price,1)

这是代码的输出

The price for Ron95 in next month will be RM [ u'B']

没有错误,但我的问题是输出中RM之后的字符应该是'B'或其他字符。我想知道这是因为我错误地执行了代码还是只是 numpy 数组的格式问题。

因为我今天基本上是刚开始接触 Python,如果我犯了一个愚蠢的错误,请见谅。

【问题讨论】:

  • 什么是42491,打印价格的结果是什么
  • 能否提供xlsx中的数据样本?
  • 如果只打印价格,则:[u'B']
  • drive.google.com/open?id=0BzvrBlV2c5P-bGt4VG85emNnbXc 这是 xlsx 文件。而对于 42491,只是一个日期值。我发现我使用的代码无法解析 xlsx 中的日期格式

标签: python machine-learning logistic-regression


【解决方案1】:

如果我没记错的话,'u' 只是表示该字符串是一个 unicode 字符串。我不确定您是如何运行代码的,但是当我在 ipython 笔记本或 Windows 命令提示符中进行测试时,我得到以下输出:

The price for Ron95 in next month will be RM [ 'B']

这可能是因为我在 python 3.5 中运行了它,而您似乎仍在使用 python

这不是你的答案是错误的,你只是得到有关数据格式的信息。有关此主题的其他问题,请参阅 herehere。 python how-to on unicode 也可能有帮助。

【讨论】:

    【解决方案2】:

    我认为当您从 Ron95_Price_Class.xlsx 发布一些数据时会更容易
    现在我看到,您没有从训练数据中删除目标变量 (y)。你可以这样做

    X = data['Date']             #you can use only one bracket if choose only
    y = data['Ron95_RM']         #column
    X = data.drop('Ron95_RM')
    

    【讨论】:

    • ValueError: 标签 ['Ron95_RM'] 不包含在轴中
    • 糟糕,对不起,我的错。对,火车数据中没有“Ron95_RM”。我要睡觉了 :) 你能从 Ron95_Price_Class.xlsx 或 print(X,y) 中发布一些行吗?
    【解决方案3】:

    scikit-learn 文档http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression.predict 中提到的 Predict 方法,提到 predict 方法的返回是数组,形状 = [n_samples]。所以对你来说,形状是 1x1 数组。要获得所需的输出,您可以尝试“price[0]”。

    【讨论】:

    • 很酷,如果您能进行 + 投票或选择我的答案作为正确答案,我将不胜感激。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2020-03-22
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 2011-11-22
    • 1970-01-01
    • 2016-03-06
    相关资源
    最近更新 更多