【发布时间】:2020-12-24 13:24:05
【问题描述】:
我正在阅读一本名为“掌握 Python 数据分析”的书,但在数据建模练习中遇到了错误。我得到的错误是:
ValueError:零维数组无法连接
我不确定这个错误是什么意思或者是什么原因造成的。
代码如下:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
from pandas import Series, DataFrame
import numpy.random as rnd
import scipy.stats as st
mean = 0
sdev = 1
nvalues = 10
norm_variate = mean * sdev +rnd.randn(nvalues)
print(norm_variate)
for i, v in enumerate(sorted(norm_variate), start = 1):
print(('{0:2d} {1:+.4f}' .format(i,v)))
def plt_cdf(data, plot_range=None, scale_to=None, **kwargs):
num_bins = len(data)
sorted_data = np.array(sorted(data), dtype=np.float64)
data_range = sorted_data[-1] - sorted_data[0]
counts, bin_edges = np.histogram(sorted_data, bins=num_bins)
xvalues = bin_edges[:1]
yvalues = np.cumsum(counts)
if plot_range is None:
xmin = sorted_data[0]
xmax = sorted_data[-1]
else:
xmin, xmax = plot_range
# pad the arrays
xvalues = np.concatenate([xmin, xvalues[0], xvalues, [xmax]])
yvalues = np.concatenate([[0.0, 0.0], yvalues, [yvalues.max()]])
if scale_to is not NONE:
yvalues = yvalues / len(data) * scale_to
plt.axis([xmin, xmax, 0, yvalues.max()])
return plt.plt(xvalues, yvalues, **kwargs)
nvalues = 20
norm_variate = rnd.randn(nvalues)
plt_cdf(norm_variate, plot_range=[-3,3], scale_to=1.0, lw=2.5, color='Brown')
for v in [0.25, 0.5, 0.75]:
plt.axhline(v, lw=1, ls='--', color='black')
任何帮助将不胜感激。
【问题讨论】:
-
查看连接表达式中的变量。它们是数组吗?或标量(评估为零维数组?请将 numpy 文档放在手边,尤其是在您的书不注意基础知识的情况下。