一个简单的方法来重新处理数据的scikitlearn。

2024-04-25 03:34:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个不到100万行40列的数据集。所有字段都是绝对的。有些字段是真/假,有些字段有几十个可能的值。这39列是功能。1 true/false列是一个标签。使用适当的行话:我想找到一个分类器,可以训练预测标签的价值。 我想试着在sklearn上运行这个文件..但是我不知道如何在没有专业程序员的帮助下将我的数据转换(重新处理?)为sklearn分类器所接受的数据。。 有什么简单的方法吗? 谢谢


Tags: 文件数据方法功能falsetrue专业分类器
1条回答
网友
1楼 · 发布于 2024-04-25 03:34:03

这是一个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()

这是一列的encoding分类特性的代码。请将这段代码放在splitting of datasets前面

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

相关问题 更多 >