标签: logistic-regression

如何使用matplotlib绘制回归的决策边界?

如何将逻辑回归结果的统计图添加到散点图中?我想要彩色的0/1区域,该区域描绘了分类器的决策边界。

import pandas as pd
import numpy as np
import pylab as pl
import statsmodels.api as sm

# Build X, Y from file
f = open('ex2data2.txt')
lines = f.readlines()
x1 = []
x2 = []
y = []
for line in lines:
    line = line.replace("\n", "")
    vals = line.split(",")
    x1.append(float(vals[0]))
    x2.append(float(vals[1]))
    y.append(int(vals[2]))

x1 = np.array(x1)
x2 = np.array(x2)
y = np.array(y)

x = np.vstack([x1, x2]).T

# Scatter plot 0/1s
pos_mask = y == 1
neg_mask = y == 0
pos_x1 …
Run Code Online (Sandbox Code Playgroud)

python matplotlib scatter-plot contour logistic-regression

2
推荐指数
1
解决办法
7386
查看次数

R中的gam函数问题

我正在尝试拟合广义加性逻辑回归模型,但出现了一个奇怪的错误:

gam_object = gam(event ~ s(time) + ., data = lapse_train, family = "binomial") 

Error in terms.formula(gf, specials = c("s", "te", "ti", "t2")) : '.' in formula and no 'data' argument
Run Code Online (Sandbox Code Playgroud)

为什么它会告诉我这里没有数据参数,而显然有数据参数?

r gam logistic-regression

2
推荐指数
1
解决办法
942
查看次数

Scikit Learn 中的交叉验证

我一直在使用http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.cross_val_score.html

为了交叉验证逻辑回归分类器。我得到的结果是:

[ 0.78571429  0.64285714  0.85714286  0.71428571  
0.78571429  0.64285714    0.84615385  0.53846154  
0.76923077  0.66666667]
Run Code Online (Sandbox Code Playgroud)

我的主要问题是如何找到哪个集合/折叠使我的分类器得分最大化并产生 0.857。

后续问题:用这组训练我的分类器是一个好习惯吗?

先感谢您。

python scikit-learn cross-validation logistic-regression

2
推荐指数
1
解决办法
1029
查看次数

SciKit学习中的多类Logistic回归

对于多类案件,我在正确调用Scikit的Logistic回归方面遇到了麻烦.我正在使用lbgfs解算器,我确实将multi_class参数设置为multinomial.

我不清楚如何在拟合模型时传递真正的类标签.我假设它与随机森林分类器多类相似/相同,在那里传递[n_samples,m_classes]数据帧.但是,在执行此操作时,我收到数据形状不佳的错误.ValueError:错误的输入形状(20,5) - 在这个小例子中,有5个类,20个样本.

在检查时,fit方法的文档说真值是作为[n_samples]传递的 - 它匹配我得到的错误 - 但是,我不知道如何训练具有多个类的模型.所以,这是我的问题:如何将完整的类标签传递给fit函数?

我一直无法在互联网上找到示例代码进行建模,也没有在StackOverflow上找到这个问题..但我确信有人必须知道该怎么做!

在下面的代码中,train_features = [n_samples,nn_features],truth_train = [n_samples,m_classes]

clf = LogisticRegressionCV(class_weight='balanced', multi_class='multinomial', solver='lbfgs')
clf.fit(train_features, truth_train)
pred = clf.predict(test_features)
Run Code Online (Sandbox Code Playgroud)

python machine-learning scikit-learn logistic-regression

2
推荐指数
1
解决办法
1万
查看次数

TensorFlow 在实现逻辑回归时返回 nan

我一直在尝试按照 MNIST 示例在 TensorFlow 中实现逻辑回归,但使用来自 CSV 的数据。每行是一个样本,有 12 个维度。我的代码如下:

batch_size = 5
learning_rate = .001
x = tf.placeholder(tf.float32,[None,12])
y = tf.placeholder(tf.float32,[None,2])
W = tf.Variable(tf.zeros([12,2]))
b = tf.Variable(tf.zeros([2]))
mult = tf.matmul(x,W)
pred = tf.nn.softmax(mult+b)
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

sess = tf.Session()
sess.run(tf.initialize_all_variables())
avg_cost = 0
total_batch = int(len(Xtrain)/batch_size)
for i in range(total_batch):
    batch_xs = Xtrain[i*batch_size:batch_size*i+batch_size]
    batch_ys = ytrain[i*batch_size:batch_size*i+batch_size]
    _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs,y: batch_ys})
    print(c)
Run Code Online (Sandbox Code Playgroud)

Xtrain 是一个 252x10 的 numpy 数组,而 ytrain 是一个 252x2 的单热 numpy …

python numpy logistic-regression tensorflow

2
推荐指数
1
解决办法
1348
查看次数

R中逻辑回归的交叉验证函数

我来自一个主要是python + scikit学习背景,我想知道如何获得R中逻辑回归模型的交叉验证准确度?我一直在寻找和惊讶,没有简单的方法.我正在寻找相应的:

import pandas as pd
from sklearn.cross_validation import cross_val_score
from sklearn.linear_model import LogisticRegression

## Assume pandas dataframe of dataset and target exist.

scores = cross_val_score(LogisticRegression(),dataset,target,cv=10)
print(scores)
Run Code Online (Sandbox Code Playgroud)

对于R:我有:

model = glm(df$Y~df$X,family=binomial')
summary(model) 
Run Code Online (Sandbox Code Playgroud)

而现在我被卡住了.原因是,我的R模型的偏差是1900,这意味着它不合适,但是python给了我85%10倍交叉验证的准确性......这意味着它很好.看起来有点奇怪......所以我想在R中运行cross val以查看它是否有相同的结果.

任何帮助表示赞赏!

r machine-learning logistic-regression

2
推荐指数
2
解决办法
1万
查看次数

仅具有一个数字特征的逻辑回归

当您只有一个数字特征时,使用scikit-learn's LogisticRegressionsolver的正确方法是什么?

我运行了一个我发现很难解释的简单示例。谁能解释一下我在这里做错了什么?

import pandas
import numpy as np
from sklearn.linear_model import LogisticRegression

X = [1, 2, 3, 10, 11, 12]
X = np.reshape(X, (6, 1))
Y = [0, 0, 0, 1, 1, 1]
Y = np.reshape(Y, (6, 1))

lr = LogisticRegression()

lr.fit(X, Y)
print ("2 --> {0}".format(lr.predict(2)))
print ("4 --> {0}".format(lr.predict(4)))
Run Code Online (Sandbox Code Playgroud)

这是我在脚本完成运行时得到的输出。4 的预测不应该是 0,因为根据高斯分布 4 更接近根据测试集分类为 0 的分布?

2 --> [0]
4 --> [1]
Run Code Online (Sandbox Code Playgroud)

当您只有一列包含数字数据时,Logistic 回归采用什么方法?

python machine-learning scikit-learn logistic-regression

2
推荐指数
1
解决办法
4107
查看次数

Statsmodels 抛出“exp 溢出”和“日志中除以零”警告,伪 R 平方是 -inf

我想使用 Statsmodels 在 Python 中进行逻辑回归。

X 和 y 各有 750 行,y 是二元结果,X 中是 10 个特征(包括截距)。

这是 X 的前 12 行(最后一列是截距):

      lngdp_      lnpop    sxp      sxp2    gy1    frac  etdo4590  geogia  \
0   7.367709  16.293980  0.190  0.036100 -1.682   132.0         1   0.916   
1   7.509883  16.436258  0.193  0.037249  2.843   132.0         1   0.916   
2   7.759187  16.589224  0.269  0.072361  4.986   132.0         1   0.916   
3   7.922261  16.742384  0.368  0.135424  3.261   132.0         1   0.916   
4   8.002359  16.901037  0.170  0.028900  1.602   132.0         1   0.916   
5   7.929126  17.034786  0.179  0.032041 -1.465 …
Run Code Online (Sandbox Code Playgroud)

python statistics statsmodels logistic-regression

2
推荐指数
1
解决办法
4272
查看次数

在没有 fminunc 的情况下执行 Andrew Ng 的逻辑回归执行

我一直在努力完成 Andrew Ng 的机器学习课程,我现在正在学习逻辑回归。我试图在不使用 MATLAB 函数的情况下发现参数并计算成本fminunc。但是,我并没有收敛到其他使用fminunc. 具体来说,我的问题是:

  • 参数theta不正确
  • 我的成本似乎要爆炸了
  • NaN我的成本向量中有很多s(我只是创建了一个成本向量来跟踪)

我试图通过梯度下降发现参数作为我理解内容的方式。但是,我的实现似乎仍然给我不正确的结果。

dataset = load('dataStuds.txt');
x = dataset(:,1:end-1);
y = dataset(:,end);
m = length(x);

% Padding the the 1's (intercept term, the call it?)
x = [ones(length(x),1), x];
thetas = zeros(size(x,2),1);

% Setting the learning rate to 0.1
alpha = 0.1;


for i = 1:100000

    % theta transpose x (tho why in MATLAB it needs to be done the other way
    % round? :) …
Run Code Online (Sandbox Code Playgroud)

matlab machine-learning logistic-regression

2
推荐指数
1
解决办法
808
查看次数

在 SKLearn Logistic Regression 中,class = Balanced 有助于运行具有不平衡数据的模型?此选项使用什么方法

在阅读了随机欠采样、随机过采样和SMOTE 之后,我试图了解 SKlearn 包中用于逻辑回归或随机森林的默认实现使用什么方法。我在这里检查了文档

所述均衡模式使用y的值来自动调节权重成反比的输入数据作为类频率N_SAMPLES次/(n_classes * np.bincount(Y))

我无法在样本多数类或样本少数类下理解它来创建平衡集

python sampling random-forest scikit-learn logistic-regression

2
推荐指数
1
解决办法
3103
查看次数