python 打印三维数据_你肯定想学习的顶级Python项目(附代码)

099e1ab7190d758e3f8fef637e9d06fb.png

作者:Anirudh Rao

翻译:张睿毅

校对:王婷

本文约4600字,建议阅读20分钟。

本文介绍了三种不同的阶段去开发Python项目,旨在提供适合各种难度层次Python项目。

Python项目–Python的初级、中级和高级

在这个“Python 项目”博客中,让我们来看3个级别的Python项目,通过这三个项目您将会学习掌握Python,以及从整体上测试项目的分析、开发和处理问题的技能。

如果我说Python的学习真的很有趣,很多人都会同意的。

我们先浏览下面的主题列表,之后开始阅读这篇Python项目博客:

  • Python简介
  • 如何学习Python项目?
  • 初级Python项目:用Python玩汉格曼游戏
  • 中级Python项目:在Python中使用图形
  • 高级Python项目:使用Python进行机器学习
  • 结论

我应当先向您简单介绍一下Python。

Python简介

Python是一种高级的、面向对象的、解释性的编程语言。它在世界上享有广泛关注。Stack Overflow发现38.8%的用户主要使用Python来完成项目。

Python是由一个名为Guido Van Rossum的开发人员创建的。

Python是而且一直是很容易学习和掌握的。它对初学者非常友好,语法非常简单,易于阅读和理解。这特别让我们高兴,而令人更高兴的是Python在全球拥有数百万快乐的学习者!

根据该网站的调查,在2018年,Python的人气超过了c#,就像它在2017年超过了php一样。在GitHub平台上,Python超越了Java成为第二个最常用的编程语言,在2017中比2016多获得了40%的申请。

这使得Python认证成为最受欢迎的编程认证之一。

如何学习Python项目?

答案相当简单直接:从学习Python的初级知识和所有基本知识开始。这是一个用于了解您使用Python舒适程度的评价指标。

下一个主要步骤是看一看基本、简单的代码,以熟悉代码中的语法和逻辑流。这是一个非常重要的步骤,有助于为以后的工作打下坚实的基础。

在这之后,您还要看看在现实生活中Python如何使用。这将成为您在开始就要学习Python的主要原因。

如果您不是刚入门Python,那么您将会学习Python项目,并对自己的项目实施一些策略。接下来一定要看看您可以利用当前关于Python的知识进行处理哪些项目。深入研究Python会帮助您在各个阶段评估自己。

项目基本上是用来解决眼下问题的。如果为各种简单或复杂的问题提供解决方案是您的特长,那么您一定要考虑学习Python的项目。

每当着手搞定几个项目之后,您距离掌握Python将更近一步。这一点很重要,因为这样您就能够自然地将所学的知识应用到项目中,从简单的程序如计算器,到辅助实现人工智能的学习。

让我们从第一级的Python项目开始学习。

https://www.edureka.co/Python-programming-certification-training

初级Python项目:用Python实现《Hangman》游戏

我们能想到的最好的入门项目是《Hangman》游戏。我敢肯定读过这篇Python项目博客的大多数人都曾在生活中某个时刻玩过《Hangman》。用一句话来解释,它的主要目标是创建一个“猜词”游戏。尽管听起来很简单,但有一些关键的东西需要注意。

  • 需要用户能够输入猜测的字母。
  • 需要限制他们的猜测次数。
  • 需要不停地告知用户剩余圈数。

这意味着你需要一种方法来获取一个用于猜测的单词。让我们用简单思维,使用文本文件输入。文本文件包含了我们必须猜测的单词。

您还需要一些函数去检查用户是否实际输入了单个字母,检查输入的字母是否出现在单词中(如果是,则检查出现多少次),以及打印字母;还有一个计数器变量限制猜测的次数。

这个Python项目中有一些关键的概念需要牢记:

  • 随机
  • 变量
  • 布尔值
  • 输入和输出
  • 整形值
  • 字符型值
  • 字符串
  • 字符串长度
  • 打印

代码:

1. Hangman.py

from string import ascii_lowercasefrom words import get_random_worddef get_num_attempts(): """Get user-inputted number of incorrect attempts for the game.""" while True: num_attempts = input( 'How many incorrect attempts do you want? [1-25] ') try: num_attempts = int(num_attempts) if 1 <= num_attempts <= 25: return num_attempts else: print('{0} is not between 1 and 25'.format(num_attempts)) except ValueError: print('{0} is not an integer between 1 and 25'.format( num_attempts))def get_min_word_length(): """Get user-inputted minimum word length for the game.""" while True: min_word_length = input( 'What minimum word length do you want? [4-16] ') try: min_word_length = int(min_word_length) if 4 <= min_word_length <= 16: return min_word_length else: print('{0} is not between 4 and 16'.format(min_word_length)) except ValueError: print('{0} is not an integer between 4 and 16'.format( min_word_length)) def get_display_word(word, idxs): """Get the word suitable for display.""" if len(word) != len(idxs): raise ValueError('Word length and indices length are not the same') displayed_word = ''.join( [letter if idxs[i] else '*' for i, letter in enumerate(word)]) return displayed_word.strip() def get_next_letter(remaining_letters): """Get the user-inputted next letter.""" if len(remaining_letters) == 0: raise ValueError('There are no remaining letters') while True: next_letter = input('Choose the next letter: ').lower() if len(next_letter) != 1: print('{0} is not a single character'.format(next_letter)) elif next_letter not in ascii_lowercase: print('{0} is not a letter'.format(next_letter)) elif next_letter not in remaining_letters: print('{0} has been guessed before'.format(next_letter)) else: remaining_letters.remove(next_letter) return next_letter def play_hangman(): """Play a game of hangman. At the end of the game, returns if the player wants to retry. """ # Let player specify difficulty print('Starting a game of Hangman...') attempts_remaining = get_num_attempts() min_word_length = get_min_word_length() # Randomly select a word print('Selecting a word...') word = get_random_word(min_word_length) print() # Initialize game state variables idxs = [letter not in ascii_lowercase for letter in word] remaining_letters = set(ascii_lowercase) wrong_letters = [] word_solved = False # Main game loop while attempts_remaining > 0 and not word_solved: # Print current game state print('Word: {0}'.format(get_display_word(word, idxs))) print('Attempts Remaining: {0}'.format(attempts_remaining)) print('Previous Guesses: {0}'.format(' '.join(wrong_letters)))# Get player's next letter guess next_letter = get_next_letter(remaining_letters)# Check if letter guess is in word if next_letter in word: # Guessed correctly print('{0} is in the word!'.format(next_letter))# Reveal matching letters for i in range(len(word)): if word[i] == next_letter: idxs[i] = True else: # Guessed incorrectly print('{0} is NOT in the word!'.format(next_letter))# Decrement num of attempts left and append guess to wrong guesses attempts_remaining -= 1 wrong_letters.append(next_letter)# Check if word is completely solved if False not in idxs: word_solved = True print() # The game is over: reveal the word print('The word is {0}'.format(word))# Notify player of victory or defeat if word_solved: print('Congratulations! You won!') else: print('Try again next time!') # Ask player if he/she wants to try again try_again = input('Would you like to try again? [y/Y] ') return try_again.lower() == 'y'if __name__ == '__main__': while play_hangman(): print()

2.Words.py

"""Function to fetch words."""import randomWORDLIST = 'wordlist.txt'def get_random_word(min_word_length): """Get a random word from the wordlist using no extra memory.""" num_words_processed = 0 curr_word = None with open(WORDLIST, 'r') as f: for word in f: if '(' in word or ')' in word: continue word = word.strip().lower() if len(word) < min_word_length: continue num_words_processed += 1 if random.randint(1, num_words_processed) == 1: curr_word = word return curr_word

结果如图

python 打印三维数据_你肯定想学习的顶级Python项目(附代码)_第1张图片

现在我们已经了解了如何处理像《hangman》这样的初级项目,那么让我们稍微升级一下,尝试一个中级的Python项目。

中级Python项目:在Python中使用图形

开始学习Python编程的中间阶段的最好方法绝对是开始使用Python支持的库。

在用Python进行编码时,可以使用真正意义上的“n”个库。有些库是非常容易直接的,而有些可能需要一些时间来理解和掌握。

下面是一些您可以考虑入门学习的顶级库:

  • NumPy
  • SciPy
  • Pandas
  • Matplotlib

NumPy总的来说是用于科学计算的。

SciPy使用数组,例如用于线性代数、微积分和其他类似概念的基本数据结构。

Pandas用于数据帧,而Matplotlib则以图形和符号的形式显示数据。

实现数据可视化可能是Python最好的应用之一。尽管数字化的数据输出很有用,但对数据的可视化表示也有许多要求。

它通过可视化展现,只是一种抽象概括。从创建前端或图形用户界面(GUI)到将数字化数据绘制为图上的点。

Matplotlib用于在图形上绘制数据点。Matplotlib是一个绘图库,可以用于Python编程语言及其数字化数学扩展库NumPy。它提供了一个面向对象的API,通过使用通用的GUI工具包(如Tkinter、wxPython、Qy或GTK+),将绘图嵌入到应用中。

在Python中有许多用于三维绘图的选项,但这里有一些使用Matplotlib的常见简单方法。

一般来说,第一步是创建一个三维坐标轴,然后绘制出最能说明特定需求的数据的三维图形。为了使用Matplotlib,必须导入Matplotlib安装中包含的mplot3d工具包:

from mpl_toolkits import mplot3d

然后,要创建三维轴,可以执行以下代码:

%matplotlib inlineimport numpy as npimport matplotlib.pyplot as pltfig = plt.figure()ax = plt.axes(projection=’3d’)
python 打印三维数据_你肯定想学习的顶级Python项目(附代码)_第2张图片

在这个三维坐标轴中,可以绘制一个图,重要的是要知道哪种类型的图(或图的组合)可以更好地描述数据。

此时,您需要注意的是,这一操作是我们进一步绘图的基础。

https://www.edureka.co/Python-programming-certification-training

点和线:

下图结合了两个绘图,一个图带有一条线,该线穿过数据的每个点,另一个图在本例中的每个特定1000个值上绘制一个点。

这个代码分析时实际上非常简单。我们利用标准三角函数绘制了一组随机值,并利用这些数据生成三维投影。

代码:

ax = plt.axes(projection=’3d’)# Data for a three-dimensional linezline = np.linspace(0, 15, 1000)xline = np.sin(zline)yline = np.cos(zline)ax.plot3D(xline, yline, zline, ‘gray’)# Data for three-dimensional scattered pointszdata = 15 * np.random.random(100)xdata = np.sin(zdata) + 0.1 * np.random.randn(100)ydata = np.cos(zdata) + 0.1 * np.random.randn(100)ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap=’Greens’);
python 打印三维数据_你肯定想学习的顶级Python项目(附代码)_第3张图片

三维等高线图

由于需要二维网格上的数据,因此轮廓图的输入与上一个绘图稍有不同。

请注意,在下面的示例中,在为x和y赋值之后,通过执行“np.meshgrid(x,y)”将它们组合到网格上,然后通过执行函数f(x,y)和网格值(z=f(x,y))创建z值。

再一次,基本的简化三维图为以下代码:

def f(x, y): return np.sin(np.sqrt(x ** 2 + y ** 2))x = np.linspace(-6, 6, 30)y = np.linspace(-6, 6, 30)X, Y = np.meshgrid(x, y)Z = f(X, Y)fig = plt.figure()ax = plt.axes(projection='3d')ax.contour3D(X, Y, Z, 50, cmap='binary')ax.set_xlabel('x')ax.set_ylabel('y')ax.set_zlabel('z');
python 打印三维数据_你肯定想学习的顶级Python项目(附代码)_第4张图片

在以前的图形中,数据是按顺序生成的,但在现实生活中,有时数据是不按顺序生成的,对于这些情况,三角网格曲面测量非常有用,因为它通过查找相邻点之间形成的三角形集来创建曲面。

表面三角测量:

theta = 2 * np.pi * np.random.random(1000)r = 6 * np.random.random(1000)x = np.ravel(r * np.sin(theta))y = np.ravel(r * np.cos(theta))z = f(x, y)ax = plt.axes(projection=’3d’)ax.plot_trisurf(x, y, z,cmap=’viridis’, edgecolor=’none’);
python 打印三维数据_你肯定想学习的顶级Python项目(附代码)_第5张图片

现在我们已经熟悉了如何通过查看外部库来扩展我们对Python的学习,那么我们就可以研究下一个高级级别的Python项目。

Python 高级项目

Python有着广泛的应用——从“Hello World”一路走到实现人工智能。

实际上,您可以使用Python进行无限多的项目,但如果您想深入了解Python的核心,可以考虑以下几个主要的项目。

  • 使用PyTorch、TensorFlow、Keras和您喜欢的任何机器学习库进行机器学习。
  • 使用OpenCV和PIL研究计算机视觉。
  • 使用测试和文档,创建和发布自己的pip模块。

在这些里面,我最喜欢的就是机器学习和深度学习。让我们看一个非常好的用例以便深入学习Python。

在Python中使用TensorFlow实现CIFAR10

让我们训练一个网络,对CIFAR10数据集中的图像进行分类。可以使用TensorFlow内置的卷积神经网络。

python 打印三维数据_你肯定想学习的顶级Python项目(附代码)_第6张图片

为理解用例的工作原理,我们考虑以下流程图:

python 打印三维数据_你肯定想学习的顶级Python项目(附代码)_第7张图片

我们把这个流程图分解成简单的组分:

  • 首先将图像加载到程序中
  • 这些图像存储在程序可以访问的位置
  • 将数据规范化,因为我们需要Python来理解当前的信息。
  • 定义神经网络的基础。
  • 定义损失函数以确保我们在数据集上获得最大精度
  • 训练实际模型,了解一些它所一直看到的数据
  • 对模型进行测试,以分析其准确性,并迭代整个训练过程,以获得更好的精度。

这个用例分为两个程序。一个是训练网络,另一个是测试网络。

我们先训练一下这个网络。

训练网络

import numpy as npimport tensorflow as tffrom time import timeimport mathfrom include.data import get_data_setfrom include.model import model, lrtrain_x, train_y = get_data_set("train")test_x, test_y = get_data_set("test")tf.set_random_seed(21)x, y, output, y_pred_cls, global_step, learning_rate = model()global_accuracy = 0epoch_start = 0# PARAMS_BATCH_SIZE = 128_EPOCH = 60_SAVE_PATH = "./tensorboard/cifar-10-v1.0.0/"# LOSS AND OPTIMIZERloss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=output, labels=y))optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08).minimize(loss, global_step=global_step)# PREDICTION AND ACCURACY CALCULATIONcorrect_prediction = tf.equal(y_pred_cls, tf.argmax(y, axis=1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))# SAVERmerged = tf.summary.merge_all()saver = tf.train.Saver() sess = tf.Session() train_writer = tf.summary.FileWriter(_SAVE_PATH, sess.graph)try:  print("Trying to restore last checkpoint ...") last_chk_path = tf.train.latest_checkpoint(checkpoint_dir=_SAVE_PATH) saver.restore(sess, save_path=last_chk_path) print("Restored checkpoint from:

你可能感兴趣的:(python,打印三维数据)