㈠ 怎么用python画个“回”字图案
㈡ 用python输出‘*’写‘回’字
我在想,你会不会说我回耍赖呢答
#-*-coding:utf-8-*-
__author__='lpe234'
__date__='2015-01-19'
print"""
***********
**
******
****
******
**
***********
"""
㈢ 用python写个函数,返回值是列表
for i in range(参数):
dic["index"] = str(i)
dic["itemName"] = 'test'
以此类推...
lis.append(dic)
㈣ 请问怎么用Python画栅格地图,类似下面这样的图,激光会返回障碍物的位置信息
你用的是激光雷达吧。 雷达应该本身可以形成周围物体的反馈。
所以需要一个图像识别算法识别出障碍物是什么。然后标志出来。
这个没有做过。不过都是现成的算法。 找一找就可以找到。
如果没有找到可以按下面的思路去做:
雷达应该可以返回目标物的距离,以及反射强度。可以使用PIL,制作一个IMAGE。根据距离角度计算出点的位置,根据反射强度计算出它的灰度值。0-255的范围。
下面是计算障碍物。 通常可以简化算法。 比如连续灰度值过10且超过2-3个像素则为目标障碍物。还可以加上距离判断。比如距离15厘米以内的才算是障碍物。
另外灰度值与范围可以做一个判断矩阵。低灰度值。
有了障碍物,只需要计算图像中心点。然后在周围画个矩形。
最后把IMAGE画出来。可以用PIL,也可以用opencv的函数。
㈤ 用python画一个圆
###################################
# coding=utf-8
# !/usr/bin/env python
# __author__ = 'pipi'
# ctime 2014.10.11
# 绘制椭圆和圆形
###################################
from matplotlib.patches import Ellipse, Circle
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ell1 = Ellipse(xy = (0.0, 0.0), width = 4, height = 8, angle = 30.0, facecolor= 'yellow', alpha=0.3)
cir1 = Circle(xy = (0.0, 0.0), radius=2, alpha=0.5)
ax.add_patch(ell1)
ax.add_patch(cir1)
x, y = 0, 0
ax.plot(x, y, 'ro')
plt.axis('scaled')
# ax.set_xlim(-4, 4)
# ax.set_ylim(-4, 4)
plt.axis('equal') #changes limits of x or y axis so that equal increments of x and y have the same length
plt.show()
你可以试试,谢谢。
㈥ 如何用python实现含有虚拟自变量的回归
参考资料:
DataRobot | Ordinary Least Squares in Python
DataRoboe | Multiple Regression using Statsmodels
AnalyticsVidhya | 7 Types of Regression Techniques you should know!
㈦ 用Python写一个测试回文,是从左到右,从右到左都读着这个函数
defisPalindrome(mstr):
l=len(mstr)
flag=True
foriinrange(l/2):
if(mstr[i]!=mstr[l-i-1]):
flag=False
break
returnflag
㈧ 如何用Python进行线性回归以及误差分析
数据挖掘中的预测问题通常分为2类:回归与分类。
简单的说回归就是预测数值,而分类是给数据打上标签归类。
本文讲述如何用Python进行基本的数据拟合,以及如何对拟合结果的误差进行分析。
本例中使用一个2次函数加上随机的扰动来生成500个点,然后尝试用1、2、100次方的多项式对该数据进行拟合。
拟合的目的是使得根据训练数据能够拟合出一个多项式函数,这个函数能够很好的拟合现有数据,并且能对未知的数据进行预测。
代码如下:
importmatplotlib.pyplot as plt
importnumpy as np
importscipy as sp
fromscipy.statsimportnorm
fromsklearn.pipelineimportPipeline
fromsklearn.linear_modelimportLinearRegression
fromsklearn.
fromsklearnimportlinear_model
''''' 数据生成 '''
x = np.arange(0,1,0.002)
y = norm.rvs(0, size=500, scale=0.1)
y = y + x**2
''''' 均方误差根 '''
defrmse(y_test, y):
returnsp.sqrt(sp.mean((y_test - y) **2))
''''' 与均值相比的优秀程度,介于[0~1]。0表示不如均值。1表示完美预测.这个版本的实现是参考scikit-learn官网文档 '''
defR2(y_test, y_true):
return1- ((y_test - y_true)**2).sum() / ((y_true - y_true.mean())**2).sum()
''''' 这是Conway&White《机器学习使用案例解析》里的版本 '''
defR22(y_test, y_true):
y_mean = np.array(y_true)
y_mean[:] = y_mean.mean()
return1- rmse(y_test, y_true) / rmse(y_mean, y_true)
plt.scatter(x, y, s=5)
degree = [1,2,100]
y_test = []
y_test = np.array(y_test)
fordindegree:
clf = Pipeline([('poly', PolynomialFeatures(degree=d)),
('linear', LinearRegression(fit_intercept=False))])
clf.fit(x[:, np.newaxis], y)
y_test = clf.predict(x[:, np.newaxis])
print(clf.named_steps['linear'].coef_)
print('rmse=%.2f, R2=%.2f, R22=%.2f, clf.score=%.2f'%
(rmse(y_test, y),
R2(y_test, y),
R22(y_test, y),
clf.score(x[:, np.newaxis], y)))
plt.plot(x, y_test, linewidth=2)
plt.grid()
plt.legend(['1','2','100'], loc='upper left')
plt.show()
该程序运行的显示结果如下:
[ 0. 0.75873781]
rmse=0.15, R2=0.78, R22=0.53, clf.score=0.78
[ 0. 0.35936882 0.52392172]
rmse=0.11, R2=0.87, R22=0.64, clf.score=0.87
[ 0.00000000e+00 2.63903249e-01 3.14973328e-01 2.43389461e-01
1.67075328e-01 1.10674280e-01 7.30672237e-02 4.88605804e-02
......
3.70018540e-11 2.93631291e-11 2.32992690e-11 1.84860002e-11
1.46657377e-11]
rmse=0.10, R2=0.90, R22=0.68, clf.score=0.90
㈨ python的返回值
1、简单介绍 print 和 return 的区别:print 仅仅是打印在控制台,而 return 则是将 return 后面的部分作为返回值(作为函数的输出,可以用变量接走,继续使用该返回值做其它事)
2、函数需要先定义后调用,函数体中 return 语句的结果就是返回值。如果一个函数没有 reutrn 语句,其实它有一个隐含的 return 语句,返回值是 None,类型也是'NoneType'
㈩ 用python写的函数判断一个数是否是回数
可以直接把函数放到if后面当作条件,如果为空的话判断结果是false,例如: def test(): return None if test(): print Trueelse: print False