重新处理scikit学习数据的简便方法。

时间:2018-11-19 04:14:58

标签: python scikit-learn analytics preprocessor

我有一个少于100万行40列的数据集。所有字段都是分类的。有些字段为true / false,有些则有数十种可能的值。这39列是功能。 1个true / false列是一个标签。使用适当的术语:我想找到一个分类器,可以对其进行训练以预测Label的值。 我想尝试在sklearn上运行文件。但是我不知道如何在没有专业程序员帮助的情况下将数据转换(重新处理)以供sklearn分类器接受。 有没有简单的出路? 谢谢

1 个答案:

答案 0 :(得分:0)

这是6列和n行数据集的样本分类器

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('name_of_your_dataset.csv')
X = dataset.iloc[:, [2, 3, 4]].values 
# syntax : dataset.iloc[nrows, ncols].values
# ':' depicts all the rows in the dataset
# ncols takes 3rd, 4th and 5th column of the dataset into X
# Change that to your respective columns
y = dataset.iloc[:, 5].values

# Splitting the dataset into the Training set and Test set
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Fitting SVM to the Training set
# You can use any classifier to predict
# This is just a sample program
from sklearn.svm import SVC
classifier = SVC(kernel = 'linear', random_state = 0)
classifier.fit(X_train, y_train)

# Predicting the Test set results
y_pred = classifier.predict(X_test)

上面是

的代码

对于编码分类数据,您需要创建dummy variables

请记住,dummy variables必须比一列中categorical variables的数目小1。否则,该程序可能会导致进入dummy variable trap

# Encoding categorical data
# Encoding the Independent Variable
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
onehotencoder = OneHotEncoder(categorical_features = [0])
X = onehotencoder.fit_transform(X).toarray()

这是1列encoding类别特征的代码。请将此部分代码放在splitting of datasets之前

您可以通过将0's更改为相应的列来对其他列执行相同的操作, 或使用for循环