㈠ 怎麼用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