这是我第一次发布问题,我是python的新手。我试图在下面的公式中将“ list1”和“ list2”插入“ x”和“ y”,并且会发生错误,例如
('无法转换 1.16041.16001.16351.17491.17661.17501.17461.17471.1779转换为数字')
import numpy as np
import pandas as pd
from scipy import stats
from pandas import *
list1 = ['1.1575', '1.1604', '1.1600', '1.1635', '1.1749', '1.1766', '1.1750', '1.1746', '1.1747', '1.1779']
list2 = ['6604.11341382', '6688.01480364', '6668.72146384', '6553.56452794', '6499.18728419', '6629.18122154', '6724.42744078', '6737.98000228', '6755.31691870', '6556.66000350']
# Method 2 (Correlation, p-value)
def pcc(x,y):
x = x - x.mean(0)
y = y - y.mean(0)
x /= x.std(0)
y /= y.std(0)
return np.mean(x*y)
x = np.array(x)
y = np.array(y)
print(linregress(x,y))
答案 0 :(得分:0)
尝试在此添加以下列表1和2的初始化:
list1 = [ float(z) for z in list1 ]
list2 = [ float(z) for z in list2 ]
答案 1 :(得分:0)
[float(i) for i in list1]
它使用浮点值创建一个新列表。
希望它的帮助。
答案 2 :(得分:0)
SELECT id, cc.name as clientName, ci.name as intermediaryName, cco.name as contractorName
FROM contract co LEFT JOIN
company cc
ON c.id = co.client LEFT JOIN
company ci
ON ci.id = co.intermediary LEFT JOIN
company cco
ON cco.id = co.contractor;
输出:
import numpy as np
list1 = ['1.1575', '1.1604', '1.1600', '1.1635', '1.1749', '1.1766', '1.1750', '1.1746', '1.1747', '1.1779']
list2 = ['6604.11341382', '6688.01480364', '6668.72146384', '6553.56452794', '6499.18728419', '6629.18122154', '6724.42744078', '6737.98000228', '6755.31691870', '6556.66000350']
list1=list(map(float,list1)) #convert into float
list2=list(map(float,list2))
# Method 2 (Correlation, p-value)
def pcc(x,y):
x = x - x.mean(0)
y = y - y.mean(0)
x /= x.std(0)
y /= y.std(0)
return np.mean(x*y)
print(pcc(np.array(list1),np.array(list2))) # pass numpy array
或 最好将所有内容保留在numpy数组中
0.04183992052616166
输出
import numpy as np
list1 = ['1.1575', '1.1604', '1.1600', '1.1635', '1.1749', '1.1766', '1.1750', '1.1746', '1.1747', '1.1779']
list2 = ['6604.11341382', '6688.01480364', '6668.72146384', '6553.56452794', '6499.18728419', '6629.18122154', '6724.42744078', '6737.98000228', '6755.31691870', '6556.66000350']
list1 = np.array(list1).astype(np.float) # or np.asarray(list1, dtype=np.float64)
list2 = np.array(list2).astype(np.float)
# Method 2 (Correlation, p-value)
def pcc(x,y):
x = x - x.mean(0)
y = y - y.mean(0)
x /= x.std(0)
y /= y.std(0)
return np.mean(x*y)
print(pcc(list1,list2)) # directly pass numpy float arrays