私信  •  关注

Sheldore David Zwicker

Sheldore David Zwicker 最近创建的主题
Sheldore David Zwicker 最近回复了
7 年前
回复了 Sheldore David Zwicker 创建的主题 » 用python绘制带有2个参数的函数的绘图

除了函数名中的拼写错误外,在完成所有函数的追加之后,还应将列表转换为for循环之外的数组。此外,我不明白为什么你可以让代码这么复杂,当你只能在你的NUMPY数组上使用矢量化操作。 xs 作为 ys=xs**4

import numpy as np
from matplotlib import pylab as plt

def sqr(x, n=2):
    return float(x ** n)

def get_plot(func, xs, n):
    ys = []
    for x in xs:
        ys.append(func(x, n))
    ys = np.array(ys) # <-- moved outside the for loop
    plt.plot(xs, ys)

xs = np.arange(1.0, 30., 0.01)
get_plot(sqr, xs, 4)

enter image description here

7 年前
回复了 Sheldore David Zwicker 创建的主题 » python上的二元线性回归

好的,这是一个使用dataframe的解决方案。我跳过导入命令,只显示相关部分。如果你不知道他们是什么,给我一个评论。

我在用纽比的 polyfit 一阶线性回归。你可以打印适合的字体( fit )得到坡度和截距。 fit[0] 是截获和 fit[1] 是斜率(或系数,如你所说)

column_A= [132.54672, 201.3845717, 323.2654551]
column_B= [51.54671995, 96.38457166, 131.2654551]
df = pd.DataFrame({'A': column_A, 'B': column_B})

fit = np.poly1d(np.polyfit(df['A'], df['B'], 1))

A_mesh = np.linspace(min(df['A']), max(df['A']), 100)

plt.plot(df['A'], df['B'], 'bx', label='Data', ms=10)
plt.plot(A_mesh, fit(A_mesh), '-b', label='Linear fit')

print (fit)
# 0.4028 x + 4.833

enter image description here

7 年前
回复了 Sheldore David Zwicker 创建的主题 » 比较两个矢量python[duplicate]

你可以使用 sorted 然后比较。正如 blhsing ,这是 O(n log n) 操作而解决方案 Counter o(n) . 自从 n=3 在您的情况下,差异可以忽略不计,但差异将变得明显 n . 你可能有兴趣知道这个。

a = [1, 3, 6] 
b = [3, 1, 6]
sorted(a) == sorted(b)
# True

Here 你会发现关于这个话题的广泛讨论。

7 年前
回复了 Sheldore David Zwicker 创建的主题 » python,检查两个字符串是否逐个字符相等[关闭]

这是你自己的代码,稍加修改。希望你觉得有用。如果字符串的长度不相等,那么将它们进行比较是没有意义的。在这种情况下,您可以打印消息并简单地返回。否则,你会比较字符,如果其中任何字符不同,你 return False 否则继续检查下一个字符。一旦所有字符都相等,就可以打印字符串相等。

def check_equal(a, b):
    if len(a) != len(b):
        print ("String lengths not equal")
        return
    else:
        for i in range(len(b)):
            if a[i] != b[i]:
                return False

    print ("Strings are equal and same")     

check_equal("Donald", "Donald") 
# Strings are equal and same  

check_equal("Donald", "Trump")    
# String lengths not equal