将 CSV 索引到不一致的样本数量以进行逻辑回归
Posted
技术标签:
【中文标题】将 CSV 索引到不一致的样本数量以进行逻辑回归【英文标题】:Indexing a CSV running into inconsistent number of samples for logistic regression 【发布时间】:2017-11-09 19:22:48 【问题描述】:我目前正在索引具有以下值的 CSV 并遇到错误:
ValueError:发现输入变量的数量不一致 样本:[1, 514]
将其作为 1 行 514 列进行检查,强调我错误地调用了特定参数,或者是因为我删除了 NaN(大多数数据默认为?)
"Classification","DGMLEN","IPLEN","TTL","IP"
"1","0.000000","192.168.1.5","185.60.216.35","TLSv1.2"
"2","0.000160","192.168.1.5","185.60.216.35","TCP"
"3","0.000161","192.168.1.5","185.60.216.35","TLSv1.2"
import pandas
df = pandas.read_csv('wcdemo.csv', header=0,
names = ["Classification", "DGMLEN", "IPLEN", "TTL", "IP"],
na_values='.')
df = df.apply(pandas.to_numeric, errors='coerce')
#Data=pd.read_csv ('wcdemo.csv').reset_index()#index_col='false')
feature_cols=['Classification','DGMLEN','IPLEN','IP']
X=df[feature_cols]
#datanewframe = pandas.Series(['Classification', 'DGMLEN', 'IPLEN', 'TTL', 'IP'], dtype='object')
#df = pandas.read_csv('wcdemo.csv')
#indexed_df = df.set_index(['Classification', 'DGMLEN','IPLEN','TTL','IP']
df['IPLEN'] = pandas.to_numeric(df['IPLEN'], errors='coerce').fillna(0)
df['TTL'] = pandas.to_numeric(df['TTL'], errors='coerce').fillna(0)
#DEFINE X TRAIN
X_train = df['IPLEN']
y_train = df['TTL']
#s = pandas.Series(['Classification', 'DGMLEN', 'IPLEN', 'TTL', 'IP'])
Y=df['TTL']
from sklearn.linear_model import LogisticRegression
logreg=LogisticRegression()
logreg.fit(X_train,y_train,).fillna(0.0)
#with the error being triggered here
logreg.fit(X_train,y_train,).fillna(0.0)
【问题讨论】:
【参考方案1】:由于您的 X_train 中只有 1 个特征,其当前形状为 (n_samples,)
。但是 scikit 估计器要求 X 的形状为(n_samples, n_features)
。所以你需要重塑你的数据。
使用这个:
logreg.fit(X_train.reshape(-1,1), y_train).fillna(0.0)
【讨论】:
以上是关于将 CSV 索引到不一致的样本数量以进行逻辑回归的主要内容,如果未能解决你的问题,请参考以下文章
逻辑回归 ValueError:发现样本数量不一致的输入变量:[699,
尝试实现逻辑回归,但 gridsearchCV 显示输入变量的样本数量不一致:[60000, 60001]
Python 2.7/Scikit 学习 0.17 线性回归错误:ValueError:发现样本数量不一致的数组:[1 343]