python - 我在使用逻辑回归实现降雨预测分类模型时遇到错误
问题描述
我的代码:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split
dataset = pd.read_csv('weatherAUS.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
imputer.fit(X[:, 1:15])
X[:, 1:15] = imputer.transform(X[:, 1:15])
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [0])], remainder='passthrough')
X = np.array(ct.fit_transform(X))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)
我得到的错误:
TypeError Traceback (most recent call last)
<ipython-input-16-c8b4cceb3113> in <module>()
1 from sklearn.model_selection import train_test_split
----> 2 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)
4 frames
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py in _num_samples(x)
150 if len(x.shape) == 0:
151 raise TypeError("Singleton array %r cannot be considered"
--> 152 " a valid collection." % x)
153 # Check that shape is returning an integer or default to len
154 # Dask dataframes may not return numeric shape[0] value
TypeError: Singleton array array(<145460x63 sparse matrix of type '<class 'numpy.float64'>'
with 1961771 stored elements in Compressed Sparse Row format>,
dtype=object) cannot be considered a valid collection.
数据集比较大(140000行,17列)。第一列是澳大利亚某个地方的位置,因此我必须使用单热编码对其进行编码。有很多黑色单元格,所以我不得不清理数据。
我的数据集的链接在这里。
解决方案
推荐阅读
- sql - 根据两行的数据创建一行
- python - 如何在python中的word docx中替换整个xml元素,就好像它们是字符串一样
- graphql - 考虑到 Apollo-Server,一个请求可以发送多少个 GraphQL 突变?
- c++ - 如何在 C 程序中使用 Boost 库
- azure - 搜索跨租户并获取所有 Workspace().tables 的问题
- javascript - 编辑谷歌表格应用脚本中过滤行中的单元格值
- java - JSP编译器的用途是什么?
- url - 当 Post 方法在出现错误时返回相同的视图时,URL 不显示编码的 Id
- python - 无法将微观策略报告转换为数据框。微策略 API
- javascript - 如何找到带有指示符的单词?javascript