我使用python-pandas数据帧,我有一个包含用户及其数据的大型数据帧.每个用户可以有多行.我想为每个用户采样1行.我目前的解决方案效率似乎不高
df1 = pd.DataFrame({'User': ['user1', 'user1', 'user2', 'user3', 'user2', 'user3'],
'B': ['B', 'B1', 'B2', 'B3','B4','B5'],
'C': ['C', 'C1', 'C2', 'C3','C4','C5'],
'D': ['D', 'D1', 'D2', 'D3','D4','D5'],
'E': ['E', 'E1', 'E2', 'E3','E4','E5']},
index=[0, 1, 2, 3,4,5])
df1
>> B C D E User
0 B C D E user1
1 B1 C1 D1 E1 user1
2 B2 C2 D2 E2 user2
3 B3 C3 D3 E3 user3
4 B4 C4 D4 E4 user2
5 B5 C5 D5 E5 user3
userList = list(df1.User.unique()) …Run Code Online (Sandbox Code Playgroud) 是否有一种简短的方法来测试多列的身份?例如,通过这个输入
data=data.table(one=c(1,2,3,4), two=c(7,8,9,10), three=c(1,2,3,4), four=c(1,2,3,4) )
Run Code Online (Sandbox Code Playgroud)
有没有什么东西可以返回与 data$one 相同的所有列?就像是
allcolumnsidentity(data$one, data) # compares all columns with respect to data$one
Run Code Online (Sandbox Code Playgroud)
应返回 (TRUE, FALSE, TRUE, TRUE),因为 data$3 和 data$four 与 data$one 相同。
我看到了 same() 和 comapre() 命令,但它们处理两列之间的比较。有没有通用的方法来做到这一点?
最好的祝愿
我是新来的火花(使用pyspark).我尝试从这里运行决策树教程(链接).我执行代码:
from pyspark.ml import Pipeline
from pyspark.ml.classification import DecisionTreeClassifier
from pyspark.ml.feature import StringIndexer, VectorIndexer
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
from pyspark.mllib.util import MLUtils
# Load and parse the data file, converting it to a DataFrame.
data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt").toDF()
labelIndexer = StringIndexer(inputCol="label", outputCol="indexedLabel").fit(data)
# Now this line fails
featureIndexer =\
VectorIndexer(inputCol="features", outputCol="indexedFeatures", maxCategories=4).fit(data)
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:IllegalArgumentException:u'requirement failed:列功能必须是org.apache.spark.ml.linalg.VectorUDT@3bfc3ba7类型,但实际上是org.apache.spark.mllib.linalg.VectorUDT@f71b0bce.
当谷歌搜索这个错误时,我找到了一个答案:
use from pyspark.ml.linalg import Vectors, VectorUDT
instead of
from pyspark.mllib.linalg import Vectors, VectorUDT
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为我还没有用过它.此外,将此导入添加到我的代码解决了什么,我仍然得到相同的错误.
我不太清楚如何调试这种情况.在查看原始数据时,我看到:
data.show()
+--------------------+-----+
| features|label|
+--------------------+-----+
|(692,[127,128,129...| …Run Code Online (Sandbox Code Playgroud) decision-tree dataframe apache-spark apache-spark-sql pyspark
我一直在使用 sklearn 的随机森林,并尝试比较了几种模型。然后我注意到随机森林即使使用相同的种子也会给出不同的结果。我尝试了两种方法: random.seed(1234) 以及使用内置的随机森林 random_state = 1234 在这两种情况下,我都得到了不可重复的结果。我错过了什么......?
# 1
random.seed(1234)
RandomForestClassifier(max_depth=5, max_features=5, criterion='gini', min_samples_leaf = 10)
# or 2
RandomForestClassifier(max_depth=5, max_features=5, criterion='gini', min_samples_leaf = 10, random_state=1234)
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢!!
编辑:添加我的代码的更完整版本
clf = RandomForestClassifier(max_depth=60, max_features=60, \
criterion='entropy', \
min_samples_leaf = 3, random_state=seed)
# As describe, I tried random_state in several ways, still diff results
clf = clf.fit(X_train, y_train)
predicted = clf.predict(X_test)
predicted_prob = clf.predict_proba(X_test)[:, 1]
fpr, tpr, thresholds = metrics.roc_curve(np.array(y_test), predicted_prob)
auc = metrics.auc(fpr,tpr)
print (auc)
Run Code Online (Sandbox Code Playgroud)
编辑:已经有一段时间了,但我认为使用 …
我想将 matplotlib 图形保存为泡菜,以使我的团队能够轻松加载它们并调查异常事件。使用简单的图像格式会失去放大和研究图形的能力。所以我试图腌制我的身材:
fig, axarr = plt.subplots(2, sharex='all') # creating the figure
... # plotting things
...
pickle.dump(fig, open('myfigfile.p'), 'wb'))
Run Code Online (Sandbox Code Playgroud)
然后我加载它。看起来挺好的。首先。
fig = pickle.load(open('myfigfile.p', 'rb'))
plt.show()
Run Code Online (Sandbox Code Playgroud)
但是后来我发现 sharex 不起作用。当我放大一个子图时,另一个保持静态。在原始图中,这很好用,并且在两个 x 轴上都进行了放大,但是在我腌制加载图形后,这不再起作用。如何保存绘图以便在加载后继续共享-x?或者,如何在从泡菜加载图形后恢复 share-x ?
谢谢!