我有一个相当大的数据集形式的数据集,我想知道如何将数据帧分成两个随机样本(80%和20%)进行训练和测试.
谢谢!
我最近看了一堆sklearn教程,这些教程都很相似,因为他们通过以下方式获得了合适的优点:
clf.fit(X_train, y_train)
clf.score(X_test, y_test)
Run Code Online (Sandbox Code Playgroud)
它会吐出来:
0.92345...
Run Code Online (Sandbox Code Playgroud)
或其他一些分数.
我很好奇clf.score函数的参数或它如何评分模型.我浏览了整个互联网,但似乎无法找到它的文档.有人知道吗?
我想知道如何通过其中一列中的值对整个数组进行排序.
我有 :
array([5,2,8,2,4])
Run Code Online (Sandbox Code Playgroud)
和:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
Run Code Online (Sandbox Code Playgroud)
我想将第一个数组附加到第二个数组,如下所示:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[5, 2, 8, 2, 4]])
Run Code Online (Sandbox Code Playgroud)
然后通过附加行对数组进行排序以获得以下结果:
array([[1, 3, 4, 0, 2],
[6, 8, 9, 5, 7],
[11, 13, 14, 10, …Run Code Online (Sandbox Code Playgroud) 我想打开一个新的文本文件,然后将numpy数组保存到该文件中.我写了这段代码:
foo = np.array([1,2,3])
abc = open('file'+'_2', 'w')
np.savetxt(abc, foo, delimiter=",")
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
TypeError Traceback (most recent call last)
<ipython-input-33-fea41927952b> in <module>()
2 model = cool
3 abc = open('file'+'_2', 'w')
----> 4 np.savetxt(abc, foo, delimiter=",")
/usr/local/lib/python3.4/site-packages/numpy/lib/npyio.py in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments)
1071 else:
1072 for row in X:
-> 1073 fh.write(asbytes(format % tuple(row) + newline))
1074 if len(footer) > 0:
1075 footer = footer.replace('\n', '\n' + comments)
TypeError: must be str, not bytes
Run Code Online (Sandbox Code Playgroud)
有谁知道什么是错的?
另外,我发现在终端中创建了一个名为file_2的空文件,但其中没有任何内容.
编辑:我正在使用Python3.4
我正在尝试对两个非线性 ODE 的系统进行数值求解。我正在使用 Scipy 的odeint功能。odeint需要一个参数y0来指定初始条件。然而,它似乎假设初始条件y0在同一时间点开始(即两个条件都在 t=0)。就我而言,我想指定为不同时间指定的两个不同边界条件(即 omega(t=0) = 0, theta(t=100) = 0)。我似乎无法弄清楚如何做到这一点,非常感谢任何帮助!
下面的一些示例代码:
from scipy.integrate import odeint
def pend(y, t, b, c):
theta, omega = y
dydt = [omega, -b*omega - c*np.sin(theta)]
return dydt
b = 0.25
c = 5.0
t = np.linspace(0, 100, 101)
# I want to make these initial conditions specified at different times
y0 = [0, 0]
sol = odeint(pend, y0, t, args=(b, c))
Run Code Online (Sandbox Code Playgroud) 因此,当我一夜之间离开时,我无法阻止我的桌面进入睡眠状态.我正在运行需要几个小时的计算,每当我的计算机进入睡眠状态时,计算都会中断.我正在运行CentOS 6.5.
我已经进入系统>首选项>电源管理,并在"交流电源"选项卡中将设置更改为"从不"和"从不".
我也通过终端尝试过:
[------]$ sudo setterm -powersave off -blank 0
cannot (un)set powersave mode
Run Code Online (Sandbox Code Playgroud)
然而,10-15分钟后,我的显示器仍然进入睡眠状态,我将不得不重新输入密码才能重新进入.任何帮助都将不胜感激!
所以我正在尝试构建一个分类器并对其性能进行评分.这是我的代码:
def svc(train_data, train_labels, test_data, test_labels):
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
svc = SVC(kernel='linear')
svc.fit(train_data, train_labels)
predicted = svc.predict(test_data)
actual = test_labels
score = svc.score(test_data, test_labels)
print ('svc score')
print (score)
print ('svc accuracy')
print (accuracy_score(predicted, actual))
Run Code Online (Sandbox Code Playgroud)
现在当我运行函数svc(X,x,Y,y)时:
X.shape = (1000, 150)
x.shape = (1000, )
Y.shape = (200, 150)
y.shape = (200, )
Run Code Online (Sandbox Code Playgroud)
我收到错误:
6 predicted = svc.predict(test_classed_data)
7 actual = test_classed_labels
----> 8 score = svc.score(test_classed_data, test_classed_labels)
9 print ('svc score')
10 print (score)
local/lib/python3.4/site-packages/sklearn/base.py …Run Code Online (Sandbox Code Playgroud) python ×6
arrays ×2
numpy ×2
scikit-learn ×2
centos ×1
csv ×1
dataframe ×1
ode ×1
pandas ×1
python-2.7 ×1
python-3.4 ×1
scipy ×1
sorting ×1