I want to find the number of regression coefficients in the first column of my dataframe. My code raised
TypeError: object of type 'numpy.float64' has no len()
from sklearn.linear_model import LinearRegression
df = pd.read_csv("master.csv")
# Drop redundant features
X = df.drop(['suicides/100k pop', 'country-year', 'suicides_no'], axis=1)
y = df['suicides/100k pop']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression(n_jobs=4, normalize=True, copy_X=True)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(f"There are {len(model.coef_[0])} regression coefficients:")
print(model.coef_[0])
X_train
print(type(X_train))
> <class 'scipy.sparse.csr.csr_matrix'>
y_train
print(type(y_train))
> <class 'pandas.core.series.Series'>
Traceback:
> --------------------------------------------------------------------------- TypeError Traceback (most recent call
> last) /tmp/ipykernel_6232/341058392.py in <module>
> 1 # Check number of and values of coefficients
> ----> 2 print(f"There are {len(model.coef_[0])} regression coefficients:")
> 3 print(model.coef_[0])
>
> TypeError: object of type 'numpy.float64' has no len()
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…