我正在使用scikit的逻辑回归,但我不断收到消息:
Found input variables with inconsistent numbers of samples: [90000, 5625]
在下面的代码中,我删除了带有文本的列,然后将日期分为训练和测试集。
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from scipy import stats
from sklearn import datasets, linear_model
from sklearn.model_selection import train_test_split
dataset = pd.read_csv("/Users/An/Desktop/data/telco.csv", na_values = ' ')
dataset = dataset.dropna(axis = 0)
dataset = dataset.replace({'Yes':1, 'Fiber optic': 1, 'DSL':1, 'No':0, 'No phone service':0, 'No internet service':0})
dataset = dataset.drop('Contract', axis =1)
dataset = dataset.drop('PaymentMethod',axis =1)
dataset = dataset.drop('customerID',axis =1)
dataset = dataset.drop('gender',axis =1)
for i in list(['tenure', 'MonthlyCharges', 'TotalCharges']):
sd = np.std(dataset[i])
mean = np.mean(dataset[i])
dataset[i] = (dataset[i] - mean) / sd
total = pd.DataFrame(dataset)
data_train, data_test = train_test_split(total, test_size=0.2)
data_train = data_train.values
data_test = data_test.values
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(C=1e9)
clf = clf.fit(data_train[:,0:16], data_train[:,16])
print clf.intercept_, clf.coef_
有人可以解释该错误消息的含义,并帮助我弄清楚为什么得到它吗?
答案 0 :(得分:1)
在倒数第二行,data_train.reshape(-1, 1)
引起了您的问题。删除reshape
会对您有所帮助。
LogisticRegression.fit
期望x
和y
具有相同的shape[0]
,但是您正在将x
从(n, m)
改成{{1 }}。
以下是复制的形状:
(n*m, 1)