# Python：ValueError：输入必须是 1- 或 2-d

[英]Python: ValueError: Input must be 1- or 2-d

``````import numpy as np
from scipy.optimize import minimize

# define the dependent variable and independent variables
X = data.iloc[:, 1:]
y = data.iloc[:, 0]

# Add a column of ones to the independent variables for the constant term
X = np.c_[np.ones(X.shape[0]), X]

# Define the likelihood function for the Tobit model
def likelihood(params, y, X, lower, upper):
beta = params[:-1]
sigma = params[-1]
mu = X @ beta
prob = (1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(-0.5 * ((y - mu) / sigma)**2))
prob[y < lower] = 0
prob[y > upper] = 0
return -np.log(prob).sum()

# Set the initial values for the parameters and the lower and upper bounds for censoring
params_init = np.random.normal(size=X.shape[1] + 1)
bounds = [(None, None) for i in range(X.shape[1])] + [(1e-10, None)]

# Perform the MLE estimation
res = minimize(likelihood, params_init, args=(y, X, 0, 100), bounds=bounds, method='L-BFGS-B')

# Extract the estimated parameters and their standard errors
params = res.x
stderr = np.sqrt(np.diag(res.hess_inv))

# Print the results
print(f'Coefficients: {params[:-1]}')
print(f'Standard Errors: {stderr[:-1]}')
print(f'Sigma: {params[-1]:.4f}')
``````

``````---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-245-5f39f416cc07> in <module>
31 # Extract the estimated parameters and their standard errors
32 params = res.x
---> 33 stderr = np.sqrt(np.diag(res.hess_inv))
34
35 # Print the results

/opt/anaconda3/lib/python3.8/site-packages/numpy/core/overrides.py in diag(*args, **kwargs)

/opt/anaconda3/lib/python3.8/site-packages/numpy/lib/twodim_base.py in diag(v, k)
307         return diagonal(v, k)
308     else:
--> 309         raise ValueError("Input must be 1- or 2-d.")
310
311

ValueError: Input must be 1- or 2-d.
``````

``````data = pd.DataFrame()

# Append 'interview probabilities' for individuals with and without disabilities
interview_prob_disabled = np.random.normal(38.63, 28.72, 619)
interview_prob_enabled = np.random.normal(44.27, 28.19, 542)
interview_prob = np.append(interview_prob_disabled, interview_prob_enabled)

# Correct the variable by its mean and standard deviation, without it being negative, nor exceeding 100, nor a float
interview_prob = np.clip(interview_prob, 0, 100)
interview_prob = np.round(interview_prob)

# Add the 'interview probabilities' variable to the dataframe
data['Interview Probabilities'] = interview_prob

# Add other variables such as age, gender, employment status, education, etc.
data['Age'] = np.random.randint(18, 65, size=len(interview_prob))
data['Gender'] = np.random.choice(['Male', 'Female'], size=len(interview_prob))
data['Employment Status'] = np.random.choice(['Employed', 'Unemployed', 'Retired'], size=len(interview_prob))
data['Education Level'] = np.random.choice(['High School', 'College', 'Vocational', 'Graduate School'], size=len(interview_prob))

# Add a 'disability status' variable as a dummy
data['Disability Status'] = np.append(np.repeat('Disabled', 619), np.repeat('Non-disabled', 542))

# Categorical variables
data['Gender'] = data['Gender'].map({'Male': 0, 'Female': 1})
data['Employment Status'] = data['Employment Status'].map({'Employed': 0, 'Unemployed': 1})
data['Education Level'] = data['Education Level'].map({'High School': 0, 'College': 1, 'Vocational': 2, 'Graduate School': 3})
data['Disability Status'] = data['Disability Status'].map({'Disabled': 1, 'Non-disabled': 0})

# Print the df
data
``````

[英]Calculating cosine similarity: ValueError: Input must be 1- or 2-d

ValueError：必须通过二维输入。 形状=(1, 50, 2)

[英]ValueError: Must pass 2-d input. shape=(1, 50, 2)

[英]Unable to convert a list into a dataframe. Keep getting the error “ValueError: Must pass 2-d input. shape=(1, 4, 5)”

[英]I am getting the following error: "ValueError: Must pass 2-d input. shape=(1, 3, 1)" but I am passing a 2-d input. What is happening here?

scipy.sparse.hstack [ValueError：块必须为二维]

[英]scipy.sparse.hstack [ValueError: blocks must be 2-D]

a和b是第二个numpy数组，我想垂直堆叠并压缩为稀疏数组。 我只用： c = sp.hstack([a, b]) 但它抛出错误： ~/anaconda3/lib/python3.6/site-packages/scipy/sparse/construct.py in hstac ...

Python + 2-D数组切片+ ValueError：操作数不能一起广播

[英]Python + 2-D array slicing + valueerror: operands could not be broadcast together

2015-01-14 18:39:17 1 97   slice
RuntimeError：张量必须是二维的

[英]RuntimeError: tensors must be 2-D

2021-04-19 09:20:41 1 264   pytorch