我每年使用以下代码来聚集学生.目的是了解每年的学生总数.
from pyspark.sql.functions import col
import pyspark.sql.functions as fn
gr = Df2.groupby(['Year'])
df_grouped =
gr.agg(fn.count(col('Student_ID')).alias('total_student_by_year'))
Run Code Online (Sandbox Code Playgroud)
结果是:
[学生按年份] [1]
我发现有这么多ID重复的问题所以结果是错误的和巨大的.
我希望按年份对学生进行聚集,按年计算学生总数,并将ID重复计算.
我希望这个问题很清楚.我是新成员谢谢
当输入和返回是字典时,如何使用 numba 加速功能?
我熟悉将 numba 用于接受数字和返回数组的函数,如下所示:
@numba.jit('float64[:](int32,int32)',nopython=True)
def f(a, b):
# returns array 1d array
Run Code Online (Sandbox Code Playgroud)
现在我有一个接受并返回字典的函数。我如何在这里申请 numba?
def collocation(aeolus_data,val_data):
...
return sample_aeolus, sample_valdata
Run Code Online (Sandbox Code Playgroud) 如果我有自变量[x1,x2,x3]如果我在sklearn中拟合线性回归,它会给我这样的东西:
y = a*x1 + b*x2 + c*x3 + intercept
Run Code Online (Sandbox Code Playgroud)
poly = 2的多项式回归会给我类似的东西
y = a*x1^2 + b*x1*x2 ......
Run Code Online (Sandbox Code Playgroud)
我不希望像x1 ^ 2那样拥有二度学位.
我怎样才能得到
y = a*x1 + b*x2 + c*x3 + d*x1*x2
Run Code Online (Sandbox Code Playgroud)
如果x1和x2具有大于某个阈值j的高相关性.
我正在阅读有关交叉验证以及如何使用它来选择最佳模型和估计参数的内容,但我并没有真正理解它的含义。
假设我建立一个线性回归模型并进行 10 倍交叉验证,我认为这 10 个中的每一个都会有不同的系数值,现在我应该从 10 个不同的系数中选择哪个作为我的最终模型或估计参数。
或者我们使用交叉验证只是为了找到平均误差(在我们的例子中是 10 个模型的平均值)并与另一个模型进行比较?
validation statistics machine-learning cross-validation statistics-bootstrap
我正在尝试使用GridSearchCV来优化我正在进行的分析,并且我已经读过它支持多种评分方法,并且我在其他地方(示例)找到了此方法的示例,但是当我尝试运行具有多个评分的GridSearchCV时应该支持多种格式的度量标准,它会引发错误:
File "/home/graduate/adsherma/miniconda2/envs/testenv/lib/python2.7/site-packages/sklearn/model_selection/_validation.py", line 288, in _score
score = scorer(estimator, X_test, y_test)
TypeError: 'dict' object is not callable
Run Code Online (Sandbox Code Playgroud)
我的源代码是:
DF = pd.read_pickle("OutPut/from_ntuples/nominal.pkl")
X = DF[RC.FittableParameters]
y = DF['isSignal']
pipe = Pipeline([
('reduce_dim', SelectKBest()),
('classify', AdaBoostClassifier())
])
BASE_ESTIMATORS = [DecisionTreeClassifier(max_depth=i) for i in range(1, 4)]
N_ESTIMATORS = range(100, 600, 100)
param_grid = [
{
'reduce_dim': [PCA()],
'reduce_dim__n_components': [1,10,20,30,40,50],
'classify__base_estimator': BASE_ESTIMATORS,
'classify__n_estimators': N_ESTIMATORS,
} ,
]
scoring = {'Precision': make_scorer(precision_score),
'Accuracy': make_scorer(accuracy_score)} #does not work
# …Run Code Online (Sandbox Code Playgroud) 为了进行适当的CV,建议使用管道,以便可以对CV中的每个折叠应用相同的转换.我可以使用sklearn.preprocessing.FunctionTrasformer或通过使用或定义自定义转换subclassing sklearn.base.TransformerMixin.推荐的方法是哪一种?为什么?
我使用以下代码从早到晚计算实时网络摄像头的人数
people_list = []
while True:
_, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.3, 5)
detections = faceCascade.detectMultiScale(gray, 1.15, 5)
for i in range(len(detections)):
face_i = detections[i]
x, y, w, h = face_i
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 222, 0), 1)
font = cv2.FONT_HERSHEY_SIMPLEX
people_list.insert(len(people_list)+1,i)
cv2.putText(frame, "id: "+str ( people_list[i]), (x, y), font, 2, (255, 255, 255), 2, cv2.LINE_AA)
# Display the resulting frame
cv2.imshow('Video', frame)
Run Code Online (Sandbox Code Playgroud)
每次检测到新面孔时,people_list计数都会增加.但是,每个帧而不是每个新面都会增加people_list计数.我怎样才能解决这个问题?
我正在使用sklearn DBSCAN来集群我的数据,如下所示.
#Apply DBSCAN (sims == my data as list of lists)
db1 = DBSCAN(min_samples=1, metric='precomputed').fit(sims)
db1_labels = db1.labels_
db1n_clusters_ = len(set(db1_labels)) - (1 if -1 in db1_labels else 0)
#Returns the number of clusters (E.g., 10 clusters)
print('Estimated number of clusters: %d' % db1n_clusters_)
Run Code Online (Sandbox Code Playgroud)
现在,我希望从大小(每个群集中的数据点数)中排序前3个群集.请告诉我如何获取sklearn中的簇大小?
python cluster-analysis machine-learning dbscan scikit-learn
我有一个非常大的数据框,其中有 100 年的日期作为列标题(即 ~36500 列)和 100 年的日期作为索引(即 ~36500 行)。我有一个函数可以计算数据帧的每个元素的值,该函数需要运行 36500^2 次。
好吧,问题不在于该函数是否非常快,而在于向数据帧分配值。即使我以这种方式分配一个常量,每 6 次分配也需要大约 1 秒。显然,正如你所见,我很厚:
for i, row in df_mBase.iterrows():
for idx, val in enumerate(row):
df_mBase.ix[i][idx] = 1
print(i)
Run Code Online (Sandbox Code Playgroud)
通常在 C/Java 中,我会简单地循环遍历 36500x36500 双循环,并通过索引直接访问预先分配的内存,这可以在恒定时间内实现,几乎没有任何开销。但这似乎不是 python 中的一个选项?
将这些数据存储在数据框中的最快方法是什么?无论是否是Pythonian,我只追求速度——我不关心优雅。
假设我有2个数据框。我想基于列查找将数据框1的列添加到数据框2。如果无法进行联接,则需要在额外的列中使用某个常数(以便可以对此进行过滤)。
图形化:
码:
import pandas as pd
import numpy as np
data = np.array([['','Col1','Col2'],
['Row1','2','TWO'],
['Row2','1','ONE']]
)
data2 = np.array([['','Col3','Col4'],
['Row1','1','T1'],
['Row2','2','T2'],
['Row3','3','T3']]
)
df = pd.DataFrame(data=data[1:,1:],
index=data[1:,0],
columns=data[0,1:])
df2 = pd.DataFrame(data=data2[1:,1:],
index=data2[1:,0],
columns=data2[0,1:])
result_df = df2 + join Col2 based on df2.Col3 = df.Col1. Add certain string constant if join fails.
print(df)
print(df2)
print(result_df)
Run Code Online (Sandbox Code Playgroud) python ×9
scikit-learn ×4
dataframe ×2
pandas ×2
dbscan ×1
dictionary ×1
grid-search ×1
join ×1
numba ×1
opencv ×1
pyspark ×1
pyspark-sql ×1
regression ×1
statistics ×1
validation ×1
webcam ×1