我正在使用sklearn进行多分类任务.我需要将alldata拆分为train_set和test_set.我想从每个班级中随机抽取相同的样本编号.实际上,我有趣的是这个功能
X_train, X_test, y_train, y_test = cross_validation.train_test_split(Data, Target, test_size=0.3, random_state=0)
Run Code Online (Sandbox Code Playgroud)
但它给出了不平衡的数据集!任何建议.
我使用OpenMP编写并行代码.以下是我案例中使用的CMakeLists的一部分.
FIND_PACKAGE( OpenMP REQUIRED)
if(OPENMP_FOUND)
message("OPENMP FOUND")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
Run Code Online (Sandbox Code Playgroud)
但是,当我们启动cmake ..时,似乎它没有找到OpenMP包,我得到了这个错误
Try OpenMP C flag = [ ]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-fopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [/openmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-Qopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing …Run Code Online (Sandbox Code Playgroud) 这是嵌套的tqdm条的示例
import time
import tqdm
for i in tqdm.tqdm(range(3)):
for j in tqdm.tqdm(range(5)):
print(i," : ", j)
Run Code Online (Sandbox Code Playgroud)
我在jupyter笔记本上尝试过,但没有显示任何错误!但是,它可以在ipython控制台上协同工作!
我有两个不同大小的数据集.
1)数据集1具有高维度4500个样本(草图).
2)数据集2具有低维1000个样本(实际数据).我想"两个数据集具有相同的分布"
我想在第一个数据集上SVM使用非线性模型sklearn(作为预训练),之后我想在第二个数据集的一部分上更新模型(以适应模型).如何在sklearn上开发一种更新.如何更新SVM模型?
我正在使用skilearn进行SVM培训.我正在使用交叉验证来评估估算器并避免过度拟合模型.
我将数据分成两部分.训练数据和测试数据.这是代码:
import numpy as np
from sklearn import cross_validation
from sklearn import datasets
from sklearn import svm
X_train, X_test, y_train, y_test = cross_validation.train_test_split(
iris.data, iris.target, test_size=0.4, random_state=0
)
clf = svm.SVC(kernel='linear', C=1)
scores = cross_validation.cross_val_score(clf, X_train, y_train, cv=5)
print scores
# Now I need to evaluate the estimator *clf* on X_test.
clf.score(X_test,y_test)
# here, I get an error say that the model is not fitted using fit(), but normally,
# in cross_val_score function the model is fitted? What is …Run Code Online (Sandbox Code Playgroud) 我想在python上的.txt文件或.csv文件中保存一个3D二进制数组,并将其导入到mathematica中.
我用Google搜索,我找到了很多答案,我试试这个:
import numpy as np
a=np.zeros((2,3,4))
a[0,0,0]=10
cPickle.dump( a, open( "matrix.txt", "wb" ) )
Run Code Online (Sandbox Code Playgroud)
在mathematica中我使用了Import ["matrix.txt","Data"]而我没有得到我所期望的
IN[]:Import["matrix.txt", "Data"]
Out[]:{{"cnumpy.core.multiarray"}, {"_reconstruct"}, {"p1"}, {"(cnumpy"}, \
{"ndarray"}, {"p2"}, {"(I0"}, {"tS'b'"}, {"tRp3"}, {"(I1"}, {"(I2"}, \
{"I3"}, {"I4"}, {"tcnumpy"}, {"dtype"}, {"p4"}, {"(S'f8'"}, {"I0"}, \
{"I1"}, {"tRp5"}, {"(I3"}, {"S'<'"}, {"NNNI-1"}, {"I-1"}, {"I0"}, \
{"tbI00"}, \
{"S'\\x00\\x00\\x00\\x00\\x00\\x00$@\\x00\\x00\\x00\\x00\\x00\\x00\\\
x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\\
x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\\
x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\\
x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\\
x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\\
x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\\
x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\\
x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\\
x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\\
x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\\
x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\\
x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\\
x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'"}, {"tb."}}
Run Code Online (Sandbox Code Playgroud) 这是我将几个带有一些描述的熊猫数据框保存到文本文件中的代码:
import numy as np
import pandas as pd
rows=['row1','row2','row3', 'row4']
data=np.random.randn(18, 4,3)
with open(filename, "wb") as outfile:
house_num = 0
outfile.write(('Shape of house Array: {0} \n'.format(data.shape)).encode())
for data_slice in data:
outfile.write(('@ House: {0} \n'.format(house_num)).encode())
df=pd.DataFrame(data_slice,columns=list('XYZ'),dtype=float)
df=df.rename(index={j:k for k, j in zip(rows,range(0,4))})
text=df.to_string()
np.savetxt(filename, text)
house_num+=1
Run Code Online (Sandbox Code Playgroud)
在最后一行,我收到一个错误 IndexError: tuple index out of range
我想获得这样的文本文件格式:
Shape of house Array: (18,4,3)
house: 0
X Y Z
row1 1.376328 0.620332 -0.726298
row2 -0.671292 0.557585 -0.027483
row3 0.381491 1.798442 0.221806
row4 -0.223592 -0.297638 -0.258627 …Run Code Online (Sandbox Code Playgroud) 这是我使用非线性 SVM 进行数字分类的代码。我应用交叉验证方案来选择超参数c和gamma。但是,GridSearch 返回的模型没有n_support_获取支持向量数量的属性。
from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.svm import SVC
from sklearn.cross_validation import ShuffleSplit
# Loading the Digits dataset
digits = datasets.load_digits()
# To apply an classifier on this data, we need to flatten the image, to
# turn the data in a (samples, feature) matrix:
n_samples = len(digits.images)
X = digits.images.reshape((n_samples, -1))
y = digits.target
# Split the dataset …Run Code Online (Sandbox Code Playgroud) python ×4
scikit-learn ×4
numpy ×2
svm ×2
3d ×1
c++ ×1
cmake ×1
formatting ×1
openmp ×1
pandas ×1
pycharm ×1
python-2.7 ×1
save ×1
tqdm ×1