Python threading allows you to have different parts of your program run concurrently and can simplify your design. If you’ve got some experience in Python and want to speed up your program using threads, then this tutorial is for you!

This article assumes you’ve got the Python basics down pat and that you’re using at least version 3.6 to run the examples.

What Is a Thread?

A thread is a separate flow of execution. This means that your program will have two things happening at once. …


Python is a unique language in that it is fairly easy to learn, given its straight-forward syntax, yet still extremely powerful. There are a lot more features under the hood than you might realize.

While I could be referring to quite a few different things with this statement, in this case I’m talking about the decorators @classmethod and @staticmethod. For many of your projects, you probably didn't need or encounter these features, but you may find that they come in handy quite a bit more than you'd expect. …


This article demonstrates the use of Python’s cursor class methods fetchall, fetchmany(), fetchone() to retrieve rows from a database table. This article applies to all the relational databases, for example, MySQL, PostgreSQL.

We generally use the following Python module to work with Database.

  • MySQL — — — — MySQL Connector Pytho
  • PostgreSQL — — — — Psycopg2
  • SQLite — — — — sqlite3

Above all, interfaces or modules are adhere to Python Database API Specification v2.0 (PEP 249). In this article, I will show how to use fetchall, fetchmany(), fetchone() to retrieve data from MySQL, PostgreSQL, SQLite database.

Before proceeding…


This article demonstrates how to use random.seed() function to initialize the pseudo-random number generator in Python to get the deterministic random data you want. By setting the custom seed value we can get the determined sequence of random numbers.

Goals of this lesson. In this lesson you’ll learn how to:

  • Use random.seed() to initialize the pseudo-random number generator.
  • Choose the same elements from the list randomly every time using random.seed()
  • Use a random.seed() function with other random module functions.

How to use random.seed() function

Let’s understand the working of a random module before proceeding further. The random number or data generated by Python’s random module…


Some like it, others hate it and many are afraid of the lambda operator. We are confident that you will like it, when you have finished with this chapter of our tutorial. If not, you can learn all about “List Comprehensions”, Guido van Rossums preferred way to do it, because he doesn’t like Lambda, map, filter and reduce either.

The lambda operator or lambda function is a way to create small anonymous functions, i.e. functions without a name.

These functions are throw-away functions, i.e. they are just needed where they have been created. Lambda functions are mainly used in combination…


Scikit-learn API provides a MulitOutputClassifier class that helps to classify multi-output data. In this tutorial, we’ll learn how to classify multi-output (multi-label) data with this method in Python. Multi-output data contains more than one y label data for a given X input data. The tutorial covers:

  1. Preparing the data
  2. Defining the model
  3. Predicting and accuracy check
  4. Source code listing

We’ll start by loading the required libraries for this tutorial.

Preparing the data

We can generate a multi-output data with a make_multilabel_classification function. The target dataset contains 20 features (x), 5 classes (y), and 10000 samples.

We’ll define them in the parameters of the function.

x, y = make_multilabel_classification(n_samples=10000, n_features=20, n_classes=5, random_state=88)

The…


在穿越法国的长途巴士中,我邻座的人正在观看YouTube,并在视频结尾观看了自动播放建议的视频。在其中一个视频中,主题是消灭世界四分之一的人口。我开玩笑说:“那谁要我们死?”他解释说:“政府有一个秘密计划。数百个视频这么说!媒体对您隐藏了它。转到YouTube,您将发现真相!”。他的激动同时感动和不安。

看来Youtu研究的算法已经学会了利用他的可信度。

过滤社交媒体的AI

人工智能(AI)是在社交媒体上起主要作用的一种算法。

在Facebook上,大多数人的朋友太多,无法看到他们的所有更新。因此,Facebook使用该领域最负盛名的研究人员开发的AI过滤帖子。在YouTube上,大多数观看意见来自于建议:每个视频右侧的视频。 YouTube将其描述为“现有规模最大,最复杂的工业推荐系统”。

最终,在社交媒体上消费的大多数信息都通过这样的AI筛选器。与其他媒体相比,这些AI为社交媒体平台提供了竞争优势,因为它可以。

“如果您的竞争对手急于构建人工智能,而您却没有,那么它会粉碎您”

–艾隆·马斯克(Elon Musk),2017年7月

这些AI的任何偏见都会对全球信息产生重大影响。因此,了解它们的设计方式并研究其偏见很重要。

AI旨在最大化观看时间

在YouTube,工程师们使用了复杂的AI来追求一个简单的目标:最大化观看时间。 Google在以下声明中解释了此重点:

如果观众正在观看更多的YouTube视频,则表明我们对找到的内容感到更加满意。这意味着创作者正在吸引更多参与的观众。它还为我们的合作伙伴创造了更多创造收入的机会。

其他AI可能会有其他最大化的目标,例如Facebook上的“喜欢”。在这种情况下,我们谈论最大化参与度。确切的公式因公司而异,但是目标是相似的:增加用户与平台的交互。

认可机构如何增强对其他媒体的反感

假设有一些YouTube视频可以说服我“媒体在撒谎”。我会通过减少消耗“媒体”的时间,并可能在YouTube上花费更多时间来做出回应。由于YouTube优化了观看时间,因此我们强烈建议您观看这些视频。

例如,YouTube返回了数以百万计的视频,用于搜索“地球是平的”(“the earth is flat”)。一些用户可能会出于好奇而点击这些声称“地球是平的“的视频。在推荐算法看来,这些视频可以有效吸引观看人群的注意力,因此,AI会继续推荐它们。一些用户将被推荐数十个这样的视频。一些人可能会相信他们;正如其中一位说:“ YouTube上有两百万个“地球是平的“的视频,这不能是假的!”

这些用户可能开始不信任“媒体”,因为后者隐藏了如此重要的信息。他们会在YouTube上花费更多的时间;因此,YouTube的AI学会了这些视频,从而增加了用户的参与度,并增加了对视频的推荐……我们正面临一个恶性循环。

“媒体在说谎”只是增加收看时间的一种说法。更普遍的说法:

任何优化自身参与度的智能AI都倾向于阻止与其他渠道的参与度。

Any smart AI that optimizes engagement with itself will have a tendency to discourage engagement to other channels.

人工智能如何影响内容创作者

“人工智能可以通过发布虚假新闻……以及操纵信息来发动战争”

-埃隆·马斯克,2017年7月

如果反媒体内容有更多的传播机会,许多内容创建者将注意到这一趋势,并制作更多此类内容以在网上获得关注。人工智能尚未自己创造虚假新闻并与媒体展开战争,但它正在激励内容创作者和公众人物这样做。

本质上,当内容创建者的信息推动AI目标实现时,他们就会获得“免费广告”的奖励。

YouTube上反媒体内容的示例

工程师们构建了一个工具,用于分析YouTube算法在科学和选举等主题上推荐最多的视频:YouTube recommended videos。我们注意到,在2016年美国大选中,最反对媒体的候选人的推荐率是对手的四倍。

在2017年法国大选期间,YouTube推荐的三名候选人是媒体最有力的批评家。

YouTube工程师曾经删除了在YouTube上搜索拉斯维加斯拍摄时推荐的1,050个视频。大多数最受推荐的视频都被指控“假冒主流媒体”故意撒谎。

拉斯维加斯射击? “虚假媒体”掩盖了Antifa的责任。

全球暖化?骗局!

米歇尔·奥巴马(Michelle Obama)?易装癖者(transvestite)。

教皇?撒旦主义者(Satanist)。

Zeynep Tufekci在一次出色的TED演讲中认为,人工智能会建立反乌托邦,使我们点击广告。在这些反乌托邦中,事实很奇怪,而且常常是矛盾的。但是有一种理论是反复出现的:媒体在撒谎。

假新闻和诽谤并不是什么新鲜事。但是,新功能是AI在其传播中的作用。 AI旨在最大限度地延长观看时间,这可能会产生偏爱内容的副作用,从而减少与其他渠道的互动。声称“媒体在撒谎”的频道可能会受益于大量的“免费广告”。

我们正在首次构建在某种程度上超过我们的工具。这些工具可能会产生我们无法完全理解并且复杂而且深远的影响。

AI越复杂,其副作用就越棘手。特别是,最大化观看时间的社交媒体AI可能会加剧对其他媒体的不满。

许多YouTube频道和Facebook页面已经通过宣传反媒体内容获得了数十亿的观看次数。 AI对抹黑媒体有何贡献?

为了找出答案,我们需要知道有关反媒体内容的观点主要来自人工推荐还是来自AI推荐。

只有社交媒体平台才有答案。


Given both weights and profits of N items, we want to put these items in a Knapsack which has a capacity C. The goal is to get the maximum profit from the items in the Knapsack. Each item can only be selected once, as we don’t have multiple quantities of any item.

Example:

  • Items: [A, B, C, D]
  • Weights: [2, 3, 1, 4]
  • Profits: [4, 5, 3, 7]
  • Capacity: 5

Let’s try some combination which total weight is less than the capacity, 5:

  • A + B (total weight: 5) = 9 profit
  • C + D (total weight: 5) = 10 profit


The scikit-learn library includes tools for data preprocessing and data mining. It is imported in Python via the statement import sklearn.

1. Standardizing

Data can contain all sorts of different values. It is hard to interpret when data take on any range of values. Therefore, we should convert the data into a standard format to make it easier to understand. The standard format of data refers to 0 means and unit variance. It is a simple process. For each data value, x, we subtract the overall mean of the data, μ, then divide by the overall standard deviation, σ.

The scikit-learn data…


The word Polymorphism is made by two Greek words, Poly meaning many and Morph meaning forms. In programming, it refers to the same object exhibiting different forms and behaviors.

For example, there is a parent class: Shape, and there are four child classes extended from the parent class: Rectangle, Circle, Polygon, and Diamond. Suppose you need methods to calculate the area of each specific shape. You could define separate methods in each class, getSquareArea(), getCircleArea(). But this makes it difficult to remember each method’s name. Is it possible to just define one method called getArea()?

The base class declares a…

Jack Dong

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store