sklearn中训练模型长时间无响应
import numpy as np
import random
from sklearn.feature_extraction.text import TfidfVectorizer
import pandas as pd
from sklearn import svm
from sklearn.cross_validation import train_test_split
from sklearn.linear_model import LogisticRegression
def getTokens(inputString): # custom tokenizer. ours tokens are characters rather than full words
tokens = []
for i in inputString:
tokens.append(i)
return tokens
filepath = '/data.csv' #path for password file
data = pd.read_csv(filepath, ',', error_bad_lines=False)
data = pd.DataFrame(data)
passwords = np.array(data)
random.shuffle(passwords) # shuffling randomly for robustness
y = [d[1] for d in passwords] # labels
allpasswords = [d[0] for d in passwords] # actual passwords
vectorizer = TfidfVectorizer(tokenizer=getTokens) # vectorizing
X = vectorizer.fit_transform(allpasswords)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42) #splitting
lgs = LogisticRegression(penalty='l2',multi_class='ovr')
lgs.fit(X_train, y_train) #线性回归
print(lgs.score(X_test, y_test))
# svr = svm.SVR()
# svr.fit(X_train, y_train) #SVM
# print(svr.score(X_test, y_test))
# from sklearn import neighbors
# knn = neighbors.KNeighborsRegressor()
# knn.fit(X_train,y_train) #KNN
# print(knn.score(X_test,y_test))
# from sklearn import ensemble
# rf =ensemble.RandomForestRegressor(n_estimators=20) #决策树
# rf.fit(X_train,y_train)
# print(rf.score(X_test,y_test))
#more testing
X_predict = ['faizanahmad','faizanahmad123','faizanahmad##','ajd1348#28t**','ffffffffff','kuiqwasdi','uiquiuiiuiuiuiuiuiuiuiuiui','mynameisfaizan','mynameis123faizan#','faizan','123456','abcdef']
X_predict = vectorizer.transform(X_predict)
y_Predict = lgs.predict(X_predict) #在这里更改模型名
print (y_Predict)
仿照github上一个项目多试几个训练方法,但是除了原本的线性回归别的都会很慢,长时间无响应。
代码一直到向量化数据都是一样且正确的。具体代码如上,就是不知道哪里有问题,该如何解决,求指教...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1、你要清楚这个是个什么任务。
这是分类学习,而不是回归
2、清楚之后,看你写的代码就明白了为什么会出错
代码全是回归
如下所示(其中KNN因为是训练集是稀疏的,超出了内存,所以会出错):
3、加油