当我运行代码时,我正在尝试使用从csv加载的数据来计算Pearson系数相关性-我遇到了不匹配错误,不确定如何消除它。
这是Python3。我尝试将值存储在新变量中
def correl(filename, header_one, header_two):
df =pd.read_csv(filename)
df.columns = [header_one, header_two]
x= np.asarray(df[header_one])
y= np.asarray(df[header_two])
n_times_sum_of_x_times_y = len(x) * np.sum(np.multiply(x,y))
sum_of_x = np.sum(x)
sum_of_y = np.sum(y)
n_times_sum_of_x_squared = len(x) * np.sum(np.multiply(x, x))
n_times_sum_of_y_squared = len(x) * np.sum(np.multiply(y, y))
sum_of_x_squared = sum_of_x ** 2
sum_of_y_squared = sum_of_y ** 2
numerator = n_times_sum_of_x_times_y - sum_of_x * sum_of_y
denominator_squared = (n_times_sum_of_x_squared - sum_of_x_squared) * (n_times_sum_of_y_squared - sum_of_y_squared)
denominator = np.sqrt(denominator_squared)
p_correl_coefficient = (numerator / denominator)
return(p_correl_coefficient)
#print(correl("imdb.csv", 'gross', 'budget'))
运行代码时出现实际错误:
文件“ C:\ Users \ ruthi \ Anaconda3 \ lib \ site-packages \ pandas \ core \ generic.py”,第638行,位于_set_axis self._data.set_axis(轴,标签)
set_axis中的第155行,文件“ C:\ Users \ ruthi \ Anaconda3 \ lib \ site-packages \ pandas \ core \ internals \ managers.py” '值具有{new}元素'.format(old = old_len,new = new_len)) ValueError:长度不匹配:预期轴有23个元素,新值有2个元素
答案 0 :(得分:1)
这意味着在文件中有23列,但是您尝试仅用2列重命名它们。如果header_one
和header_two
是现有列的名称,请执行df = df[[header_one, header_two]]
,如果不是,请首先选择您感兴趣的两列,然后使用df.columns = [header_one, header_two]
重命名它们>