小编IMC*_*ins的帖子

如何在 qcut 之后在分类变量中添加新类别?

我创建了一个分类变量,我想为其他变量的特定值创建一个新类别

我有一个数据框,其中的变量的Score值范围为 0-100。我做了十分之一,但我想为特定值创建一个新类别

df['Score_pr']=pd.qcut(df['Score'] ,10,duplicates='drop')

df.loc[X_n['Score']==1,'Score_pr']='0'
Run Code Online (Sandbox Code Playgroud)

0我期望所有案例都有一个新类别,Score=1 但我收到了以下消息:

无法在具有新类别的类别上设置项目,请先设置类别

python pandas

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

我可以根据参数更改返回类型吗

我不确定如何提出我的问题,所以如果问题标题对您来说不正确,请耐心等待。

function uselessFunction(value: number): string | number {
    if (value > 5) {
        return "false"
    }
    return 2
}

const uselessVar1 = uselessFunction(1);
// type is `string | number`
// expected would be `number`

const uselessVar2 = uselessFunction(10);
// type is `string | number`
// expected would be `string`.
Run Code Online (Sandbox Code Playgroud)

我了解typeguard概念以及如何使用它,但这不是我的用例。

function isString(value: string | number): value is string {
    if (typeof value === "string") {
        return true
    }
    return false
}

if (isString(uselessVar1) {
    // uselessVar1 type is …
Run Code Online (Sandbox Code Playgroud)

typescript typescript-generics

3
推荐指数
1
解决办法
5756
查看次数

绘制回归线

我在绘制一些回归线时遇到了一些问题。我的问题可能是我没有正确理解这些函数所做的数学运算,所以我在这里要求确定。

from matplotlib import pyplot as plt
import numpy as np

def estimate_coef(x, y):
    # number of observations/points
    n = np.size(x)

    # mean of x and y vector
    m_x, m_y = np.mean(x), np.mean(y)

    # calculating cross-deviation and deviation about x
    SS_xy = np.sum(y*x - n*m_y*m_x)
    SS_xx = np.sum(x*x - n*m_x*m_x)

    # calculating regression coefficients
    b_1 = SS_xy / SS_xx
    b_0 = m_y - b_1 * m_x

    return (b_0, b_1)

def plot_regression_line(xs, ys):
    # dev stands for deviation
    dev = estimate_coef(xs, …
Run Code Online (Sandbox Code Playgroud)

python numpy matplotlib linear-regression

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

具有列表理解的多重分配

我想知道我是否可以使用一个列表理解分配多个列表.我在某种程度上无法理解语法.

所以,而不是......

xs = [item.km for item in data]
ys = [item.price for item in data]
Run Code Online (Sandbox Code Playgroud)

我想做...

xs, ys = [km, price for km, price in data]
# or...
xs, ys = [item.km, item.price for item in data]
Run Code Online (Sandbox Code Playgroud)

但这会引发一个语法错误,我似乎无法找到错误.

即使看起来很明显,数据如下......

for elem in data:
    print elem
# outputs (this is a namedtuple) :
# mileage(km=22899.0, price=7990.0)
# mileage(km=48235.0, price=6900.0)
# mileage(km=54000.0, price=7990.0)
# mileage(km=60949.0, price=7490.0)
...
Run Code Online (Sandbox Code Playgroud)

python list-comprehension python-2.7

0
推荐指数
1
解决办法
461
查看次数